python-fastbencode-0.0.5/.github/0000755000000000000000000000000014107310426013574 5ustar00python-fastbencode-0.0.5/.gitignore0000644000000000000000000000015514107310426014225 0ustar00*.so build fastbencode/_bencode_pyx.c fastbencode/_bencode_pyx.h __pycache__ fastbencode.egg-info *.pyc dist python-fastbencode-0.0.5/MANIFEST.in0000644000000000000000000000010514107310426013766 0ustar00include README.md recursive-include fastbencode *.py *.pyx *.pxd *.h python-fastbencode-0.0.5/README.md0000644000000000000000000000074714107310426013523 0ustar00fastbencode =========== fastbencode is an implementation of the bencode serialization format originally used by BitTorrent. The package includes both a pure-Python version and an optional C extension based on Cython. Both provide the same functionality, but the C extension provides significantly better performance. Copyright ========= Original Pure-Python bencoder (c) Petru Paler Cython version and modifications (c) Canonical Ltd Split out from Bazaar/Breezy by Jelmer Vernooij python-fastbencode-0.0.5/fastbencode/0000755000000000000000000000000014107310426014511 5ustar00python-fastbencode-0.0.5/releaser.conf0000644000000000000000000000035114107310426014704 0ustar00# See https://github.com/jelmer/fastbencode timeout_days: 5 tag_name: "v$VERSION" verify_command: "python3 setup.py test" update_version { path: "setup.py" match: "^ version=\"(.*)\",$" new_line: ' version="$VERSION",' } python-fastbencode-0.0.5/setup.py0000755000000000000000000001011714107310426013751 0ustar00#!/usr/bin/python3 import os import sys from setuptools import setup try: from Cython.Distutils import build_ext except ImportError: have_cython = False # try to build the extension from the prior generated source. print("") print("The python package 'Cython' is not available." " If the .c files are available,") print("they will be built," " but modifying the .pyx files will not rebuild them.") print("") from distutils.command.build_ext import build_ext else: have_cython = True from distutils import log from distutils.errors import CCompilerError, DistutilsPlatformError from distutils.extension import Extension class build_ext_if_possible(build_ext): user_options = build_ext.user_options + [ ('allow-python-fallback', None, "When an extension cannot be built, allow falling" " back to the pure-python implementation.") ] def initialize_options(self): build_ext.initialize_options(self) self.allow_python_fallback = False def run(self): try: build_ext.run(self) except DistutilsPlatformError: e = sys.exc_info()[1] if not self.allow_python_fallback: log.warn('\n Cannot build extensions.\n' ' Use "build_ext --allow-python-fallback" to use' ' slower python implementations instead.\n') raise log.warn(str(e)) log.warn('\n Extensions cannot be built.\n' ' Using the slower Python implementations instead.\n') def build_extension(self, ext): try: build_ext.build_extension(self, ext) except CCompilerError: if not self.allow_python_fallback: log.warn('\n Cannot build extension "%s".\n' ' Use "build_ext --allow-python-fallback" to use' ' slower python implementations instead.\n' % (ext.name,)) raise log.warn('\n Building of "%s" extension failed.\n' ' Using the slower Python implementation instead.' % (ext.name,)) # Override the build_ext if we have Cython available ext_modules = [] def add_cython_extension(module_name, libraries=None, extra_source=[]): """Add a cython module to build. This will use Cython to auto-generate the .c file if it is available. Otherwise it will fall back on the .c file. If the .c file is not available, it will warn, and not add anything. You can pass any extra options to Extension through kwargs. One example is 'libraries = []'. :param module_name: The python path to the module. This will be used to determine the .pyx and .c files to use. """ path = module_name.replace('.', '/') cython_name = path + '.pyx' c_name = path + '.c' define_macros = [] if sys.platform == 'win32': # cython uses the macro WIN32 to detect the platform, even though it # should be using something like _WIN32 or MS_WINDOWS, oh well, we can # give it the right value. define_macros.append(('WIN32', None)) if have_cython: source = [cython_name] else: if not os.path.isfile(c_name): return else: source = [c_name] source.extend(extra_source) include_dirs = ['fastbencode'] ext_modules.append( Extension( module_name, source, define_macros=define_macros, libraries=libraries, include_dirs=include_dirs)) add_cython_extension('fastbencode._bencode_pyx') setup( name="fastbencode", description="Implementation of bencode with optional fast C extensions", version="0.0.5", maintainer="Breezy Developers", maintainer_email="breezy-core@googlegroups.com", url="https://github.com/breezy-team/fastbencode", ext_modules=ext_modules, extras_require={'cext': ['cython>=0.29']}, cmdclass={'build_ext': build_ext_if_possible}, test_suite="fastbencode.tests.test_suite", packages=["fastbencode"]) python-fastbencode-0.0.5/.github/workflows/0000755000000000000000000000000014107310426015631 5ustar00python-fastbencode-0.0.5/.github/workflows/pythonpackage.yml0000644000000000000000000000141414107310426021211 0ustar00name: Python package on: [push, pull_request] jobs: build: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] python-version: [3.6, 3.7, 3.8, 3.9, pypy3] fail-fast: false steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install --upgrade pip pip install -U pip flake8 - name: Style checks run: | python -m flake8 - name: Test suite run run: | python -m unittest fastbencode.tests.test_suite env: PYTHONHASHSEED: random python-fastbencode-0.0.5/fastbencode/__init__.py0000644000000000000000000000440214107310426016622 0ustar00# Copyright (C) 2007, 2009 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """Wrapper around the bencode pyrex and python implementation""" _extension_load_failures = [] def failed_to_load_extension(exception): """Handle failing to load a binary extension. This should be called from the ImportError block guarding the attempt to import the native extension. If this function returns, the pure-Python implementation should be loaded instead:: >>> try: >>> import _fictional_extension_pyx >>> except ImportError, e: >>> failed_to_load_extension(e) >>> import _fictional_extension_py """ # NB: This docstring is just an example, not a doctest, because doctest # currently can't cope with the use of lazy imports in this namespace -- # mbp 20090729 # This currently doesn't report the failure at the time it occurs, because # they tend to happen very early in startup when we can't check config # files etc, and also we want to report all failures but not spam the user # with 10 warnings. exception_str = str(exception) if exception_str not in _extension_load_failures: import warnings warnings.warn( 'failed to load compiled extension: %s' % exception_str, UserWarning) _extension_load_failures.append(exception_str) try: from ._bencode_pyx import bdecode, bdecode_as_tuple, bencode, Bencached except ImportError as e: failed_to_load_extension(e) from ._bencode_py import ( # noqa: F401 bdecode, bdecode_as_tuple, bencode, Bencached, ) python-fastbencode-0.0.5/fastbencode/_bencode_py.py0000644000000000000000000001025514107310426017334 0ustar00# bencode structured encoding # # Written by Petru Paler # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files # (the "Software"), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # Modifications copyright (C) 2008 Canonical Ltd class BDecoder(object): def __init__(self, yield_tuples=False): """Constructor. :param yield_tuples: if true, decode "l" elements as tuples rather than lists. """ self.yield_tuples = yield_tuples decode_func = {} decode_func[b'l'] = self.decode_list decode_func[b'd'] = self.decode_dict decode_func[b'i'] = self.decode_int decode_func[b'0'] = self.decode_string decode_func[b'1'] = self.decode_string decode_func[b'2'] = self.decode_string decode_func[b'3'] = self.decode_string decode_func[b'4'] = self.decode_string decode_func[b'5'] = self.decode_string decode_func[b'6'] = self.decode_string decode_func[b'7'] = self.decode_string decode_func[b'8'] = self.decode_string decode_func[b'9'] = self.decode_string self.decode_func = decode_func def decode_int(self, x, f): f += 1 newf = x.index(b'e', f) n = int(x[f:newf]) if x[f:f + 2] == b'-0': raise ValueError elif x[f:f + 1] == b'0' and newf != f + 1: raise ValueError return (n, newf + 1) def decode_string(self, x, f): colon = x.index(b':', f) n = int(x[f:colon]) if x[f:f + 1] == b'0' and colon != f + 1: raise ValueError colon += 1 return (x[colon:colon + n], colon + n) def decode_list(self, x, f): r, f = [], f + 1 while x[f:f + 1] != b'e': v, f = self.decode_func[x[f:f + 1]](x, f) r.append(v) if self.yield_tuples: r = tuple(r) return (r, f + 1) def decode_dict(self, x, f): r, f = {}, f + 1 lastkey = None while x[f:f + 1] != b'e': k, f = self.decode_string(x, f) if lastkey is not None and lastkey >= k: raise ValueError lastkey = k r[k], f = self.decode_func[x[f:f + 1]](x, f) return (r, f + 1) def bdecode(self, x): if not isinstance(x, bytes): raise TypeError try: r, l = self.decode_func[x[:1]](x, 0) # noqa: E741 except (IndexError, KeyError, OverflowError) as e: raise ValueError(str(e)) if l != len(x): # noqa: E741 raise ValueError return r _decoder = BDecoder() bdecode = _decoder.bdecode _tuple_decoder = BDecoder(True) bdecode_as_tuple = _tuple_decoder.bdecode class Bencached(object): __slots__ = ['bencoded'] def __init__(self, s): self.bencoded = s def encode_bencached(x, r): r.append(x.bencoded) def encode_bool(x, r): encode_int(int(x), r) def encode_int(x, r): r.extend((b'i', int_to_bytes(x), b'e')) def encode_string(x, r): r.extend((int_to_bytes(len(x)), b':', x)) def encode_list(x, r): r.append(b'l') for i in x: encode_func[type(i)](i, r) r.append(b'e') def encode_dict(x, r): r.append(b'd') ilist = sorted(x.items()) for k, v in ilist: r.extend((int_to_bytes(len(k)), b':', k)) encode_func[type(v)](v, r) r.append(b'e') encode_func = {} encode_func[type(Bencached(0))] = encode_bencached encode_func[int] = encode_int def int_to_bytes(n): return b'%d' % n encode_func[bytes] = encode_string encode_func[list] = encode_list encode_func[tuple] = encode_list encode_func[dict] = encode_dict encode_func[bool] = encode_bool def bencode(x): r = [] encode_func[type(x)](x, r) return b''.join(r) python-fastbencode-0.0.5/fastbencode/_bencode_pyx.h0000644000000000000000000000173114107310426017322 0ustar00/* Copyright (C) 2009 Canonical Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Simple header providing some macro definitions for _bencode_pyx.pyx */ #define D_UPDATE_TAIL(self, n) (((self)->size -= (n), (self)->tail += (n))) #define E_UPDATE_TAIL(self, n) (((self)->size += (n), (self)->tail += (n))) python-fastbencode-0.0.5/fastbencode/_bencode_pyx.pyx0000644000000000000000000002733514107310426017723 0ustar00# Copyright (C) 2007, 2009, 2010 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # cython: language_level=3 """Pyrex implementation for bencode coder/decoder""" from cpython.bool cimport ( PyBool_Check, ) from cpython.bytes cimport ( PyBytes_CheckExact, PyBytes_FromStringAndSize, PyBytes_AS_STRING, PyBytes_GET_SIZE, ) from cpython.dict cimport ( PyDict_CheckExact, ) from cpython.int cimport ( PyInt_CheckExact, PyInt_FromString, ) from cpython.list cimport ( PyList_CheckExact, PyList_Append, ) from cpython.long cimport ( PyLong_CheckExact, ) from cpython.mem cimport ( PyMem_Free, PyMem_Malloc, PyMem_Realloc, ) from cpython.tuple cimport ( PyTuple_CheckExact, ) from libc.stdlib cimport ( strtol, ) from libc.string cimport ( memcpy, ) cdef extern from "python-compat.h": int snprintf(char* buffer, size_t nsize, char* fmt, ...) # Use wrapper with inverted error return so Cython can propogate int BrzPy_EnterRecursiveCall(char *) except 0 cdef extern from "Python.h": void Py_LeaveRecursiveCall() cdef class Decoder cdef class Encoder cdef extern from "_bencode_pyx.h": void D_UPDATE_TAIL(Decoder, int n) void E_UPDATE_TAIL(Encoder, int n) cdef class Decoder: """Bencode decoder""" cdef readonly char *tail cdef readonly int size cdef readonly int _yield_tuples cdef object text def __init__(self, s, yield_tuples=0): """Initialize decoder engine. @param s: Python string. """ if not PyBytes_CheckExact(s): raise TypeError("bytes required") self.text = s self.tail = PyBytes_AS_STRING(s) self.size = PyBytes_GET_SIZE(s) self._yield_tuples = int(yield_tuples) def decode(self): result = self._decode_object() if self.size != 0: raise ValueError('junk in stream') return result def decode_object(self): return self._decode_object() cdef object _decode_object(self): cdef char ch if 0 == self.size: raise ValueError('stream underflow') BrzPy_EnterRecursiveCall(" while bencode decoding") try: ch = self.tail[0] if c'0' <= ch <= c'9': return self._decode_string() elif ch == c'l': D_UPDATE_TAIL(self, 1) return self._decode_list() elif ch == c'i': D_UPDATE_TAIL(self, 1) return self._decode_int() elif ch == c'd': D_UPDATE_TAIL(self, 1) return self._decode_dict() finally: Py_LeaveRecursiveCall() raise ValueError('unknown object type identifier %r' % ch) cdef int _read_digits(self, char stop_char) except -1: cdef int i i = 0 while ((self.tail[i] >= c'0' and self.tail[i] <= c'9') or self.tail[i] == c'-') and i < self.size: i = i + 1 if self.tail[i] != stop_char: raise ValueError("Stop character %c not found: %c" % (stop_char, self.tail[i])) if (self.tail[0] == c'0' or (self.tail[0] == c'-' and self.tail[1] == c'0')): if i == 1: return i else: raise ValueError # leading zeroes are not allowed return i cdef object _decode_int(self): cdef int i i = self._read_digits(c'e') self.tail[i] = 0 try: ret = PyInt_FromString(self.tail, NULL, 10) finally: self.tail[i] = c'e' D_UPDATE_TAIL(self, i+1) return ret cdef object _decode_string(self): cdef int n cdef char *next_tail # strtol allows leading whitespace, negatives, and leading zeros # however, all callers have already checked that '0' <= tail[0] <= '9' # or they wouldn't have called _decode_string # strtol will stop at trailing whitespace, etc n = strtol(self.tail, &next_tail, 10) if next_tail == NULL or next_tail[0] != c':': raise ValueError('string len not terminated by ":"') # strtol allows leading zeros, so validate that we don't have that if (self.tail[0] == c'0' and (n != 0 or (next_tail - self.tail != 1))): raise ValueError('leading zeros are not allowed') D_UPDATE_TAIL(self, next_tail - self.tail + 1) if n == 0: return b'' if n > self.size: raise ValueError('stream underflow') if n < 0: raise ValueError('string size below zero: %d' % n) result = PyBytes_FromStringAndSize(self.tail, n) D_UPDATE_TAIL(self, n) return result cdef object _decode_list(self): result = [] while self.size > 0: if self.tail[0] == c'e': D_UPDATE_TAIL(self, 1) if self._yield_tuples: return tuple(result) else: return result else: # As a quick shortcut, check to see if the next object is a # string, since we know that won't be creating recursion # if self.tail[0] >= c'0' and self.tail[0] <= c'9': PyList_Append(result, self._decode_object()) raise ValueError('malformed list') cdef object _decode_dict(self): cdef char ch result = {} lastkey = None while self.size > 0: ch = self.tail[0] if ch == c'e': D_UPDATE_TAIL(self, 1) return result else: # keys should be strings only if self.tail[0] < c'0' or self.tail[0] > c'9': raise ValueError('key was not a simple string.') key = self._decode_string() if lastkey is not None and lastkey >= key: raise ValueError('dict keys disordered') else: lastkey = key value = self._decode_object() result[key] = value raise ValueError('malformed dict') def bdecode(object s): """Decode string x to Python object""" return Decoder(s).decode() def bdecode_as_tuple(object s): """Decode string x to Python object, using tuples rather than lists.""" return Decoder(s, True).decode() class Bencached(object): __slots__ = ['bencoded'] def __init__(self, s): self.bencoded = s cdef enum: INITSIZE = 1024 # initial size for encoder buffer INT_BUF_SIZE = 32 cdef class Encoder: """Bencode encoder""" cdef readonly char *tail cdef readonly int size cdef readonly char *buffer cdef readonly int maxsize def __init__(self, int maxsize=INITSIZE): """Initialize encoder engine @param maxsize: initial size of internal char buffer """ cdef char *p self.maxsize = 0 self.size = 0 self.tail = NULL p = PyMem_Malloc(maxsize) if p == NULL: raise MemoryError('Not enough memory to allocate buffer ' 'for encoder') self.buffer = p self.maxsize = maxsize self.tail = p def __dealloc__(self): PyMem_Free(self.buffer) self.buffer = NULL self.maxsize = 0 def to_bytes(self): if self.buffer != NULL and self.size != 0: return PyBytes_FromStringAndSize(self.buffer, self.size) return b'' cdef int _ensure_buffer(self, int required) except 0: """Ensure that tail of CharTail buffer has enough size. If buffer is not big enough then function try to realloc buffer. """ cdef char *new_buffer cdef int new_size if self.size + required < self.maxsize: return 1 new_size = self.maxsize while new_size < self.size + required: new_size = new_size * 2 new_buffer = PyMem_Realloc(self.buffer, new_size) if new_buffer == NULL: raise MemoryError('Cannot realloc buffer for encoder') self.buffer = new_buffer self.maxsize = new_size self.tail = &new_buffer[self.size] return 1 cdef int _encode_int(self, int x) except 0: """Encode int to bencode string iNNNe @param x: value to encode """ cdef int n self._ensure_buffer(INT_BUF_SIZE) n = snprintf(self.tail, INT_BUF_SIZE, b"i%de", x) if n < 0: raise MemoryError('int %d too big to encode' % x) E_UPDATE_TAIL(self, n) return 1 cdef int _encode_long(self, x) except 0: return self._append_string(b'i%de' % x) cdef int _append_string(self, s) except 0: cdef Py_ssize_t n n = PyBytes_GET_SIZE(s) self._ensure_buffer(n) memcpy(self.tail, PyBytes_AS_STRING(s), n) E_UPDATE_TAIL(self, n) return 1 cdef int _encode_string(self, x) except 0: cdef int n cdef Py_ssize_t x_len x_len = PyBytes_GET_SIZE(x) self._ensure_buffer(x_len + INT_BUF_SIZE) n = snprintf(self.tail, INT_BUF_SIZE, b'%ld:', x_len) if n < 0: raise MemoryError('string %s too big to encode' % x) memcpy((self.tail+n), PyBytes_AS_STRING(x), x_len) E_UPDATE_TAIL(self, n + x_len) return 1 cdef int _encode_list(self, x) except 0: self._ensure_buffer(1) self.tail[0] = c'l' E_UPDATE_TAIL(self, 1) for i in x: self.process(i) self._ensure_buffer(1) self.tail[0] = c'e' E_UPDATE_TAIL(self, 1) return 1 cdef int _encode_dict(self, x) except 0: self._ensure_buffer(1) self.tail[0] = c'd' E_UPDATE_TAIL(self, 1) for k in sorted(x): if not PyBytes_CheckExact(k): raise TypeError('key in dict should be string') self._encode_string(k) self.process(x[k]) self._ensure_buffer(1) self.tail[0] = c'e' E_UPDATE_TAIL(self, 1) return 1 cpdef object process(self, object x): BrzPy_EnterRecursiveCall(" while bencode encoding") try: if PyBytes_CheckExact(x): self._encode_string(x) elif PyInt_CheckExact(x) and x.bit_length() < 32: self._encode_int(x) elif PyLong_CheckExact(x): self._encode_long(x) elif PyList_CheckExact(x) or PyTuple_CheckExact(x): self._encode_list(x) elif PyDict_CheckExact(x): self._encode_dict(x) elif PyBool_Check(x): self._encode_int(int(x)) elif isinstance(x, Bencached): self._append_string(x.bencoded) else: raise TypeError('unsupported type %r' % x) finally: Py_LeaveRecursiveCall() def bencode(x): """Encode Python object x to string""" encoder = Encoder() encoder.process(x) return encoder.to_bytes() python-fastbencode-0.0.5/fastbencode/python-compat.h0000644000000000000000000000730014107310426017464 0ustar00/* * Bazaar -- distributed version control * * Copyright (C) 2008 by Canonical Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /* Provide the typedefs that pyrex does automatically in newer versions, to * allow older versions to build our extensions. */ #ifndef _BZR_PYTHON_COMPAT_H #define _BZR_PYTHON_COMPAT_H #ifdef _MSC_VER #define inline __inline #endif #if PY_MAJOR_VERSION >= 3 #define PyInt_FromSsize_t PyLong_FromSsize_t /* On Python 3 just don't intern bytes for now */ #define PyBytes_InternFromStringAndSize PyBytes_FromStringAndSize /* In Python 3 the Py_TPFLAGS_CHECKTYPES behaviour is on by default */ #define Py_TPFLAGS_CHECKTYPES 0 #define PYMOD_ERROR NULL #define PYMOD_SUCCESS(val) val #define PYMOD_INIT_FUNC(name) PyMODINIT_FUNC PyInit_##name(void) #define PYMOD_CREATE(ob, name, doc, methods) do { \ static struct PyModuleDef moduledef = { \ PyModuleDef_HEAD_INIT, name, doc, -1, methods \ }; \ ob = PyModule_Create(&moduledef); \ } while(0) #else #define PyBytes_Type PyString_Type #define PyBytes_CheckExact PyString_CheckExact #define PyBytes_FromStringAndSize PyString_FromStringAndSize inline PyObject* PyBytes_InternFromStringAndSize(const char *v, Py_ssize_t len) { PyObject *obj = PyString_FromStringAndSize(v, len); if (obj != NULL) { PyString_InternInPlace(&obj); } return obj; } /* Lazy hide Python 3.3 only functions, callers must avoid on 2.7 anyway */ #define PyUnicode_AsUTF8AndSize(u, size) NULL #define PYMOD_ERROR #define PYMOD_SUCCESS(val) #define PYMOD_INIT_FUNC(name) void init##name(void) #define PYMOD_CREATE(ob, name, doc, methods) do { \ ob = Py_InitModule3(name, methods, doc); \ } while(0) #endif #define BrzPy_EnterRecursiveCall(where) (Py_EnterRecursiveCall(where) == 0) #if defined(_WIN32) || defined(WIN32) /* Defining WIN32_LEAN_AND_MEAN makes including windows quite a bit * lighter weight. */ #define WIN32_LEAN_AND_MEAN #include /* Needed for htonl */ #include "Winsock2.h" /* sys/stat.h doesn't have any of these macro definitions for MSVC, so * we'll define whatever is missing that we actually use. */ #if !defined(S_ISDIR) #define S_ISDIR(m) (((m) & 0170000) == 0040000) #endif #if !defined(S_ISREG) #define S_ISREG(m) (((m) & 0170000) == 0100000) #endif #if !defined(S_IXUSR) #define S_IXUSR 0000100/* execute/search permission, owner */ #endif /* sys/stat.h doesn't have S_ISLNK on win32, so we fake it by just always * returning False */ #if !defined(S_ISLNK) #define S_ISLNK(mode) (0) #endif #else /* Not win32 */ /* For htonl */ #include "arpa/inet.h" #endif #include #ifdef _MSC_VER #define snprintf _snprintf /* gcc (mingw32) has strtoll, while the MSVC compiler uses _strtoi64 */ #define strtoll _strtoi64 #define strtoull _strtoui64 #endif #if PY_VERSION_HEX < 0x030900A4 # define Py_SET_REFCNT(obj, refcnt) ((Py_REFCNT(obj) = (refcnt)), (void)0) #endif #endif /* _BZR_PYTHON_COMPAT_H */ python-fastbencode-0.0.5/fastbencode/tests/0000755000000000000000000000000014107310426015653 5ustar00python-fastbencode-0.0.5/fastbencode/tests/__init__.py0000644000000000000000000000211714107310426017765 0ustar00# Copyright (C) 2007, 2009, 2010 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # """Tests for fastbencode.""" import unittest def test_suite(): names = [ 'test_bencode', ] module_names = ['fastbencode.tests.' + name for name in names] result = unittest.TestSuite() loader = unittest.TestLoader() suite = loader.loadTestsFromNames(module_names) result.addTests(suite) return result python-fastbencode-0.0.5/fastbencode/tests/test_bencode.py0000644000000000000000000003610014107310426020663 0ustar00# Copyright (C) 2007, 2009, 2010, 2016 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """Tests for bencode structured encoding""" import copy import sys from unittest import TestCase, TestSuite def get_named_object(module_name, member_name=None): """Get the Python object named by a given module and member name. This is usually much more convenient than dealing with ``__import__`` directly:: >>> doc = get_named_object('pyutils', 'get_named_object.__doc__') >>> doc.splitlines()[0] 'Get the Python object named by a given module and member name.' :param module_name: a module name, as would be found in sys.modules if the module is already imported. It may contain dots. e.g. 'sys' or 'os.path'. :param member_name: (optional) a name of an attribute in that module to return. It may contain dots. e.g. 'MyClass.some_method'. If not given, the named module will be returned instead. :raises: ImportError or AttributeError. """ # We may have just a module name, or a module name and a member name, # and either may contain dots. __import__'s return value is a bit # unintuitive, so we need to take care to always return the object # specified by the full combination of module name + member name. if member_name: # Give __import__ a from_list. It will return the last module in # the dotted module name. attr_chain = member_name.split('.') from_list = attr_chain[:1] obj = __import__(module_name, {}, {}, from_list) for attr in attr_chain: obj = getattr(obj, attr) else: # We're just importing a module, no attributes, so we have no # from_list. __import__ will return the first module in the dotted # module name, so we look up the module from sys.modules. __import__(module_name, globals(), locals(), []) obj = sys.modules[module_name] return obj def iter_suite_tests(suite): """Return all tests in a suite, recursing through nested suites""" if isinstance(suite, TestCase): yield suite elif isinstance(suite, TestSuite): for item in suite: for r in iter_suite_tests(item): yield r else: raise Exception('unknown type %r for object %r' % (type(suite), suite)) def clone_test(test, new_id): """Clone a test giving it a new id. :param test: The test to clone. :param new_id: The id to assign to it. :return: The new test. """ new_test = copy.copy(test) new_test.id = lambda: new_id # XXX: Workaround , which # causes cloned tests to share the 'details' dict. This makes it hard to # read the test output for parameterized tests, because tracebacks will be # associated with irrelevant tests. try: new_test._TestCase__details except AttributeError: # must be a different version of testtools than expected. Do nothing. pass else: # Reset the '__details' dict. new_test._TestCase__details = {} return new_test def apply_scenario(test, scenario): """Copy test and apply scenario to it. :param test: A test to adapt. :param scenario: A tuple describing the scenario. The first element of the tuple is the new test id. The second element is a dict containing attributes to set on the test. :return: The adapted test. """ new_id = "%s(%s)" % (test.id(), scenario[0]) new_test = clone_test(test, new_id) for name, value in scenario[1].items(): setattr(new_test, name, value) return new_test def apply_scenarios(test, scenarios, result): """Apply the scenarios in scenarios to test and add to result. :param test: The test to apply scenarios to. :param scenarios: An iterable of scenarios to apply to test. :return: result :seealso: apply_scenario """ for scenario in scenarios: result.addTest(apply_scenario(test, scenario)) return result def multiply_tests(tests, scenarios, result): """Multiply tests_list by scenarios into result. This is the core workhorse for test parameterisation. Typically the load_tests() method for a per-implementation test suite will call multiply_tests and return the result. :param tests: The tests to parameterise. :param scenarios: The scenarios to apply: pairs of (scenario_name, scenario_param_dict). :param result: A TestSuite to add created tests to. This returns the passed in result TestSuite with the cross product of all the tests repeated once for each scenario. Each test is adapted by adding the scenario name at the end of its id(), and updating the test object's __dict__ with the scenario_param_dict. >>> import tests.test_sampler >>> r = multiply_tests( ... tests.test_sampler.DemoTest('test_nothing'), ... [('one', dict(param=1)), ... ('two', dict(param=2))], ... TestUtil.TestSuite()) >>> tests = list(iter_suite_tests(r)) >>> len(tests) 2 >>> tests[0].id() 'tests.test_sampler.DemoTest.test_nothing(one)' >>> tests[0].param 1 >>> tests[1].param 2 """ for test in iter_suite_tests(tests): apply_scenarios(test, scenarios, result) return result def permute_tests_for_extension(standard_tests, loader, py_module_name, ext_module_name): """Helper for permutating tests against an extension module. This is meant to be used inside a modules 'load_tests()' function. It will create 2 scenarios, and cause all tests in the 'standard_tests' to be run against both implementations. Setting 'test.module' to the appropriate module. See tests.test__chk_map.load_tests as an example. :param standard_tests: A test suite to permute :param loader: A TestLoader :param py_module_name: The python path to a python module that can always be loaded, and will be considered the 'python' implementation. (eg '_chk_map_py') :param ext_module_name: The python path to an extension module. If the module cannot be loaded, a single test will be added, which notes that the module is not available. If it can be loaded, all standard_tests will be run against that module. :return: (suite, feature) suite is a test-suite that has all the permuted tests. feature is the Feature object that can be used to determine if the module is available. """ py_module = get_named_object(py_module_name) scenarios = [ ('python', {'module': py_module}), ] suite = loader.suiteClass() try: __import__(ext_module_name) except ModuleNotFoundError: pass else: scenarios.append(('C', {'module': get_named_object(ext_module_name)})) result = multiply_tests(standard_tests, scenarios, suite) return result def load_tests(loader, standard_tests, pattern): return permute_tests_for_extension( standard_tests, loader, 'fastbencode._bencode_py', 'fastbencode._bencode_pyx') class RecursionLimit(object): """Context manager that lowers recursion limit for testing.""" def __init__(self, limit=100): self._new_limit = limit self._old_limit = sys.getrecursionlimit() def __enter__(self): sys.setrecursionlimit(self._new_limit) return self def __exit__(self, *exc_info): sys.setrecursionlimit(self._old_limit) class TestBencodeDecode(TestCase): module = None def _check(self, expected, source): self.assertEqual(expected, self.module.bdecode(source)) def _run_check_error(self, exc, bad): """Check that bdecoding a string raises a particular exception.""" self.assertRaises(exc, self.module.bdecode, bad) def test_int(self): self._check(0, b'i0e') self._check(4, b'i4e') self._check(123456789, b'i123456789e') self._check(-10, b'i-10e') self._check(int('1' * 1000), b'i' + (b'1' * 1000) + b'e') def test_long(self): self._check(12345678901234567890, b'i12345678901234567890e') self._check(-12345678901234567890, b'i-12345678901234567890e') def test_malformed_int(self): self._run_check_error(ValueError, b'ie') self._run_check_error(ValueError, b'i-e') self._run_check_error(ValueError, b'i-010e') self._run_check_error(ValueError, b'i-0e') self._run_check_error(ValueError, b'i00e') self._run_check_error(ValueError, b'i01e') self._run_check_error(ValueError, b'i-03e') self._run_check_error(ValueError, b'i') self._run_check_error(ValueError, b'i123') self._run_check_error(ValueError, b'i341foo382e') def test_string(self): self._check(b'', b'0:') self._check(b'abc', b'3:abc') self._check(b'1234567890', b'10:1234567890') def test_large_string(self): self.assertRaises(ValueError, self.module.bdecode, b"2147483639:foo") def test_malformed_string(self): self._run_check_error(ValueError, b'10:x') self._run_check_error(ValueError, b'10:') self._run_check_error(ValueError, b'10') self._run_check_error(ValueError, b'01:x') self._run_check_error(ValueError, b'00:') self._run_check_error(ValueError, b'35208734823ljdahflajhdf') self._run_check_error(ValueError, b'432432432432432:foo') self._run_check_error(ValueError, b' 1:x') # leading whitespace self._run_check_error(ValueError, b'-1:x') # negative self._run_check_error(ValueError, b'1 x') # space vs colon self._run_check_error(ValueError, b'1x') # missing colon self._run_check_error(ValueError, (b'1' * 1000) + b':') def test_list(self): self._check([], b'le') self._check([b'', b'', b''], b'l0:0:0:e') self._check([1, 2, 3], b'li1ei2ei3ee') self._check([b'asd', b'xy'], b'l3:asd2:xye') self._check([[b'Alice', b'Bob'], [2, 3]], b'll5:Alice3:Bobeli2ei3eee') def test_list_deepnested(self): import platform if platform.python_implementation() == 'PyPy': self.skipTest('recursion not an issue on pypy') with RecursionLimit(): self._run_check_error(RuntimeError, (b"l" * 100) + (b"e" * 100)) def test_malformed_list(self): self._run_check_error(ValueError, b'l') self._run_check_error(ValueError, b'l01:ae') self._run_check_error(ValueError, b'l0:') self._run_check_error(ValueError, b'li1e') self._run_check_error(ValueError, b'l-3:e') def test_dict(self): self._check({}, b'de') self._check({b'': 3}, b'd0:i3ee') self._check({b'age': 25, b'eyes': b'blue'}, b'd3:agei25e4:eyes4:bluee') self._check({b'spam.mp3': {b'author': b'Alice', b'length': 100000}}, b'd8:spam.mp3d6:author5:Alice6:lengthi100000eee') def test_dict_deepnested(self): with RecursionLimit(): self._run_check_error( RuntimeError, (b"d0:" * 1000) + b'i1e' + (b"e" * 1000)) def test_malformed_dict(self): self._run_check_error(ValueError, b'd') self._run_check_error(ValueError, b'defoobar') self._run_check_error(ValueError, b'd3:fooe') self._run_check_error(ValueError, b'di1e0:e') self._run_check_error(ValueError, b'd1:b0:1:a0:e') self._run_check_error(ValueError, b'd1:a0:1:a0:e') self._run_check_error(ValueError, b'd0:0:') self._run_check_error(ValueError, b'd0:') self._run_check_error(ValueError, b'd432432432432432432:e') def test_empty_string(self): self.assertRaises(ValueError, self.module.bdecode, b'') def test_junk(self): self._run_check_error(ValueError, b'i6easd') self._run_check_error(ValueError, b'2:abfdjslhfld') self._run_check_error(ValueError, b'0:0:') self._run_check_error(ValueError, b'leanfdldjfh') def test_unknown_object(self): self.assertRaises(ValueError, self.module.bdecode, b'relwjhrlewjh') def test_unsupported_type(self): self._run_check_error(TypeError, float(1.5)) self._run_check_error(TypeError, None) self._run_check_error(TypeError, lambda x: x) self._run_check_error(TypeError, object) self._run_check_error(TypeError, u"ie") def test_decoder_type_error(self): self.assertRaises(TypeError, self.module.bdecode, 1) class TestBencodeEncode(TestCase): module = None def _check(self, expected, source): self.assertEqual(expected, self.module.bencode(source)) def test_int(self): self._check(b'i4e', 4) self._check(b'i0e', 0) self._check(b'i-10e', -10) def test_long(self): self._check(b'i12345678901234567890e', 12345678901234567890) self._check(b'i-12345678901234567890e', -12345678901234567890) def test_string(self): self._check(b'0:', b'') self._check(b'3:abc', b'abc') self._check(b'10:1234567890', b'1234567890') def test_list(self): self._check(b'le', []) self._check(b'li1ei2ei3ee', [1, 2, 3]) self._check(b'll5:Alice3:Bobeli2ei3eee', [[b'Alice', b'Bob'], [2, 3]]) def test_list_as_tuple(self): self._check(b'le', ()) self._check(b'li1ei2ei3ee', (1, 2, 3)) self._check(b'll5:Alice3:Bobeli2ei3eee', ((b'Alice', b'Bob'), (2, 3))) def test_list_deep_nested(self): top = [] lst = top for unused_i in range(1000): lst.append([]) lst = lst[0] with RecursionLimit(): self.assertRaises(RuntimeError, self.module.bencode, top) def test_dict(self): self._check(b'de', {}) self._check(b'd3:agei25e4:eyes4:bluee', {b'age': 25, b'eyes': b'blue'}) self._check(b'd8:spam.mp3d6:author5:Alice6:lengthi100000eee', {b'spam.mp3': {b'author': b'Alice', b'length': 100000}}) def test_dict_deep_nested(self): d = top = {} for i in range(1000): d[b''] = {} d = d[b''] with RecursionLimit(): self.assertRaises(RuntimeError, self.module.bencode, top) def test_bencached(self): self._check(b'i3e', self.module.Bencached(self.module.bencode(3))) def test_invalid_dict(self): self.assertRaises(TypeError, self.module.bencode, {1: b"foo"}) def test_bool(self): self._check(b'i1e', True) self._check(b'i0e', False)