Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
whtsky committed Feb 11, 2018
2 parents 0e27053 + ac86559 commit f1157b5
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 80 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Usage
ChangeLog
----------

Version 1.2.1
~~~~~~~~~~~~~~~

+ Drop support for Python 2.6
+ Performance boost for `bencode` method. `#7 <https://github.com/whtsky/bencoder.pyx/issues/7>`_

Version 1.2.0
~~~~~~~~~~~~~~~

Expand Down
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ environment:
secure: pp1j5lAB9NN8ZDasgY+oxoGrNw0+4gGzbNZmHVwJkCzUyrNBP5ZIuCrwjmx4q6ifg7RMiE3bVt9MljFCJh3XpsvVOAcx+AGKsHSjtXd40HM=

matrix:
- PYTHON: "C:\\Python26"
- PYTHON: "C:\\Python26-x64"
- PYTHON: "C:\\Python27"
- PYTHON: "C:\\Python27-x64"
- PYTHON: "C:\\Python33"
Expand Down
53 changes: 26 additions & 27 deletions bencoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

# Based on https://github.com/karamanolev/bencode3/blob/master/bencode.py

__version__ = '1.2.0'
__version__ = '1.2.1'

import array

try:
from collections import OrderedDict
Expand Down Expand Up @@ -101,7 +100,7 @@ def bdecode(bytes x):
raise BTFailure("invalid bencoded value (data after valid prefix)")
return r

def encode(v, r):
cdef encode(v, list r):
tp = type(v)
if tp in encode_func:
return encode_func[tp](v, r)
Expand All @@ -114,47 +113,47 @@ def encode(v, r):
)


def encode_int(long x, r):
r.fromstring(b'i')
r.fromstring(str(x).encode())
r.fromstring(b'e')
cdef encode_int(long x, list r):
r.append(b'i')
r.append(str(x).encode())
r.append(b'e')


def encode_long(x, r):
r.fromstring(b'i')
r.fromstring(str(x).encode())
r.fromstring(b'e')
cdef encode_long(x, list r):
r.append(b'i')
r.append(str(x).encode())
r.append(b'e')


def encode_bytes(bytes x, r):
r.fromstring(str(len(x)).encode())
r.fromstring(b':')
r.fromstring(x)
cdef encode_bytes(bytes x, list r):
r.append(str(len(x)).encode())
r.append(b':')
r.append(x)


def encode_string(str x, r):
r.fromstring(str(len(x)).encode())
r.fromstring(b':')
r.fromstring(x.encode())
cdef encode_string(str x, list r):
r.append(str(len(x)).encode())
r.append(b':')
r.append(x.encode())


def encode_list(x, r):
r.fromstring(b'l')
cdef encode_list(x, list r):
r.append(b'l')
for i in x:
encode(i, r)
r.fromstring(b'e')
r.append(b'e')


def encode_dict(x, r):
r.fromstring(b'd')
cdef encode_dict(x, list r):
r.append(b'd')
item_list = list(x.items())
item_list.sort()
for k, v in item_list:
if isinstance(k, str):
k = k.encode()
encode_bytes(k, r)
encode(v, r)
r.fromstring(b'e')
r.append(b'e')


encode_func = {
Expand All @@ -171,6 +170,6 @@ encode_func = {


def bencode(x):
r = array.array(ARRAY_TYPECODE)
r = []
encode(x, r)
return r.tostring()
return b''.join(r)
2 changes: 0 additions & 2 deletions ci/build-wheels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ for whl in /tmp/wheelhouse/bencoder*.whl; do
auditwheel repair $whl -w /io/wheelhouse/
done

cp /tmp/wheelhouse/ordereddict* /io/wheelhouse

# Install packages and test again
for PYBIN in /opt/python/*/bin/; do
${PYBIN}/pip install bencoder.pyx --no-index -f /io/wheelhouse
Expand Down
5 changes: 2 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pytest==3.0.5
pytest-benchmark==3.0.0
pytest==3.2.5
coverage==4.3.1
cython==0.25.2
cython==0.27.3
tox==2.5.0
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bdist_wheel]

[pytest]
[tool:pytest]
testpaths = tests
addopts = -rw
13 changes: 6 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

version = platform.python_version_tuple()
install_requires = []
if version < ('2', '7'):
install_requires.append('ordereddict>=1.1')

pyx_path = 'bencoder.pyx'
c_path = 'bencoder.c'
Expand Down Expand Up @@ -38,12 +36,14 @@
from Cython.Build import cythonize
ext_modules = cythonize(Extension(
"bencoder",
[pyx_path]
[pyx_path],
extra_compile_args=['-O3']
))
else:
ext_modules = [Extension(
'bencoder',
[c_path]
[c_path],
extra_compile_args=['-O3']
)]


Expand All @@ -55,7 +55,7 @@ def initialize_options(self):
self.pytest_args = []

def run_tests(self):
#import here, cause outside the eggs aren't loaded
# import here, cause outside the eggs aren't loaded
import coverage
cov = coverage.Coverage()
cov.start()
Expand Down Expand Up @@ -97,7 +97,7 @@ def get_tag(self):

setup(
name='bencoder.pyx',
version='1.2.0',
version='1.2.1',
description='Yet another bencode implementation in Cython',
long_description=open('README.rst', 'r').read(),
author='whtsky',
Expand All @@ -117,7 +117,6 @@ def get_tag(self):
'Programming Language :: Cython',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
Expand Down
24 changes: 12 additions & 12 deletions tests/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,37 @@ def test_decode2():
assert length == 8


def test_decode_str(benchmark):
assert benchmark(bdecode, b'6:WWWWWW') == b"WWWWWW"
def test_decode_str():
assert bdecode(b'6:WWWWWW') == b"WWWWWW"


def test_decode_int(benchmark):
assert benchmark(bdecode, b'i233e') == 233
def test_decode_int():
assert bdecode(b'i233e') == 233


def test_decode_large_int(benchmark):
def test_decode_large_int():
assert bdecode(b'i1455189890e') == 1455189890
assert bdecode(b'i25735241490e') == 25735241490

MAX_SIZE = sys.maxsize + 1
BENCODED_MAXSIZE = ('i%de' % MAX_SIZE).encode()
assert benchmark(bdecode, BENCODED_MAXSIZE) == MAX_SIZE
assert bdecode(BENCODED_MAXSIZE) == MAX_SIZE


def test_decode_list(benchmark):
assert benchmark(bdecode, b'l1:a1:bi3ee') == [b'a', b'b', 3]
def test_decode_list():
assert bdecode(b'l1:a1:bi3ee') == [b'a', b'b', 3]


def test_decode_dict(benchmark):
def test_decode_dict():
od = dict()
od[b'ka'] = b'va'
od[b'kb'] = 2
assert benchmark(bdecode, b'd2:ka2:va2:kbi2ee') == od
assert bdecode(b'd2:ka2:va2:kbi2ee') == od


def test_ordered_dict(benchmark):
def test_ordered_dict():
from bencoder import OrderedDict
rv = benchmark(bdecode, b'd2:ka2:va2:kbi2ee')
rv = bdecode(b'd2:ka2:va2:kbi2ee')
assert isinstance(rv, OrderedDict)
assert list(rv.keys()) == [b'ka', b'kb']
assert list(bdecode(b'd2:kc2:va2:kei2ee').keys()) == [b'kc', b'ke']
Expand Down
44 changes: 22 additions & 22 deletions tests/test_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,68 @@
)


def test_encode_str(benchmark):
assert benchmark(bencode, "WWWWWW") == b'6:WWWWWW'
def test_encode_str():
assert bencode("WWWWWW") == b'6:WWWWWW'


def test_encode_int(benchmark):
assert benchmark(bencode, 233) == b'i233e'
def test_encode_int():
assert bencode(233) == b'i233e'


def test_encode_large_int(benchmark):
def test_encode_large_int():
assert bencode(1455189890) == b'i1455189890e'
assert bencode(25735241490) == b'i25735241490e'
MAX_SIZE = sys.maxsize + 1
BENCODED_MAXSIZE = ('i%de' % MAX_SIZE).encode()

assert benchmark(bencode, MAX_SIZE) == BENCODED_MAXSIZE
assert bencode(MAX_SIZE) == BENCODED_MAXSIZE


def test_encode_bytes(benchmark):
def test_encode_bytes():
b = b"TheseAreSomeBytes"
coded = benchmark(bencode, b)
coded = bencode(b)
l = str(len(b)).encode()
assert coded == l + b':' + b


def test_encode_string(benchmark):
def test_encode_string():
b = "TheseAreSomeString"
coded = benchmark(bencode, b)
coded = bencode(b)
l = str(len(b))
assert coded == (l + ':' + b).encode()


def test_encode_list(benchmark):
assert benchmark(bencode, ['a', 'b', 3]) == b'l1:a1:bi3ee'
def test_encode_list():
assert bencode(['a', 'b', 3]) == b'l1:a1:bi3ee'


def test_encode_tuple(benchmark):
assert benchmark(bencode, ('a', 'b', 3)) == b'l1:a1:bi3ee'
def test_encode_tuple():
assert bencode(('a', 'b', 3)) == b'l1:a1:bi3ee'


def test_encode_true(benchmark):
assert benchmark(bencode, True) == bencode(1)
def test_encode_true():
assert bencode(True) == bencode(1)


def test_encode_false(benchmark):
assert benchmark(bencode, False) == bencode(0)
def test_encode_false():
assert bencode(False) == bencode(0)


def test_encode_dict(benchmark):
def test_encode_dict():
od = dict()
od['ka'] = 'va'
od['kb'] = 2
assert benchmark(bencode, od) == b'd2:ka2:va2:kbi2ee'
assert bencode(od) == b'd2:ka2:va2:kbi2ee'


def test_encode_dict_subclass(benchmark):
def test_encode_dict_subclass():
class AAA(dict):
pass

od = AAA()
od['ka'] = 'va'
od['kb'] = 2
assert benchmark(bencode, od) == b'd2:ka2:va2:kbi2ee'
assert bencode(od) == b'd2:ka2:va2:kbi2ee'


def test_encode_complex():
Expand Down
2 changes: 1 addition & 1 deletion tox-wheels.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py26, py27, py33, py34, py35, py36
envlist = py27, py33, py34, py35, py36

[testenv]
commands = pip wheel {toxinidir} -w {toxinidir}/wheelhouse/
Expand Down
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py26, py27, py33, py34, py35, py36, pypy
envlist = py27, py33, py34, py35, py36, pypy

[testenv]
commands =
Expand All @@ -11,13 +11,11 @@ deps =
cython
pytest
coverage
pytest-benchmark
codecov

[testenv:pypy]
skip_install = False
deps =
cython
pytest
pytest-benchmark
commands = py.test

0 comments on commit f1157b5

Please sign in to comment.