Skip to content

Commit f1157b5

Browse files
committed
Merge branch 'develop'
2 parents 0e27053 + ac86559 commit f1157b5

11 files changed

+77
-80
lines changed

README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ Usage
4646
ChangeLog
4747
----------
4848

49+
Version 1.2.1
50+
~~~~~~~~~~~~~~~
51+
52+
+ Drop support for Python 2.6
53+
+ Performance boost for `bencode` method. `#7 <https://github.com/whtsky/bencoder.pyx/issues/7>`_
54+
4955
Version 1.2.0
5056
~~~~~~~~~~~~~~~
5157

appveyor.yml

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ environment:
66
secure: pp1j5lAB9NN8ZDasgY+oxoGrNw0+4gGzbNZmHVwJkCzUyrNBP5ZIuCrwjmx4q6ifg7RMiE3bVt9MljFCJh3XpsvVOAcx+AGKsHSjtXd40HM=
77

88
matrix:
9-
- PYTHON: "C:\\Python26"
10-
- PYTHON: "C:\\Python26-x64"
119
- PYTHON: "C:\\Python27"
1210
- PYTHON: "C:\\Python27-x64"
1311
- PYTHON: "C:\\Python33"

bencoder.pyx

+26-27
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

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

15-
__version__ = '1.2.0'
15+
__version__ = '1.2.1'
1616

17-
import array
1817

1918
try:
2019
from collections import OrderedDict
@@ -101,7 +100,7 @@ def bdecode(bytes x):
101100
raise BTFailure("invalid bencoded value (data after valid prefix)")
102101
return r
103102

104-
def encode(v, r):
103+
cdef encode(v, list r):
105104
tp = type(v)
106105
if tp in encode_func:
107106
return encode_func[tp](v, r)
@@ -114,47 +113,47 @@ def encode(v, r):
114113
)
115114

116115

117-
def encode_int(long x, r):
118-
r.fromstring(b'i')
119-
r.fromstring(str(x).encode())
120-
r.fromstring(b'e')
116+
cdef encode_int(long x, list r):
117+
r.append(b'i')
118+
r.append(str(x).encode())
119+
r.append(b'e')
121120

122121

123-
def encode_long(x, r):
124-
r.fromstring(b'i')
125-
r.fromstring(str(x).encode())
126-
r.fromstring(b'e')
122+
cdef encode_long(x, list r):
123+
r.append(b'i')
124+
r.append(str(x).encode())
125+
r.append(b'e')
127126

128127

129-
def encode_bytes(bytes x, r):
130-
r.fromstring(str(len(x)).encode())
131-
r.fromstring(b':')
132-
r.fromstring(x)
128+
cdef encode_bytes(bytes x, list r):
129+
r.append(str(len(x)).encode())
130+
r.append(b':')
131+
r.append(x)
133132

134133

135-
def encode_string(str x, r):
136-
r.fromstring(str(len(x)).encode())
137-
r.fromstring(b':')
138-
r.fromstring(x.encode())
134+
cdef encode_string(str x, list r):
135+
r.append(str(len(x)).encode())
136+
r.append(b':')
137+
r.append(x.encode())
139138

140139

141-
def encode_list(x, r):
142-
r.fromstring(b'l')
140+
cdef encode_list(x, list r):
141+
r.append(b'l')
143142
for i in x:
144143
encode(i, r)
145-
r.fromstring(b'e')
144+
r.append(b'e')
146145

147146

148-
def encode_dict(x, r):
149-
r.fromstring(b'd')
147+
cdef encode_dict(x, list r):
148+
r.append(b'd')
150149
item_list = list(x.items())
151150
item_list.sort()
152151
for k, v in item_list:
153152
if isinstance(k, str):
154153
k = k.encode()
155154
encode_bytes(k, r)
156155
encode(v, r)
157-
r.fromstring(b'e')
156+
r.append(b'e')
158157

159158

160159
encode_func = {
@@ -171,6 +170,6 @@ encode_func = {
171170

172171

173172
def bencode(x):
174-
r = array.array(ARRAY_TYPECODE)
173+
r = []
175174
encode(x, r)
176-
return r.tostring()
175+
return b''.join(r)

ci/build-wheels.sh

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ for whl in /tmp/wheelhouse/bencoder*.whl; do
1414
auditwheel repair $whl -w /io/wheelhouse/
1515
done
1616

17-
cp /tmp/wheelhouse/ordereddict* /io/wheelhouse
18-
1917
# Install packages and test again
2018
for PYBIN in /opt/python/*/bin/; do
2119
${PYBIN}/pip install bencoder.pyx --no-index -f /io/wheelhouse

dev-requirements.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
pytest==3.0.5
2-
pytest-benchmark==3.0.0
1+
pytest==3.2.5
32
coverage==4.3.1
4-
cython==0.25.2
3+
cython==0.27.3
54
tox==2.5.0

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bdist_wheel]
22

3-
[pytest]
3+
[tool:pytest]
44
testpaths = tests
55
addopts = -rw

setup.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
version = platform.python_version_tuple()
1010
install_requires = []
11-
if version < ('2', '7'):
12-
install_requires.append('ordereddict>=1.1')
1311

1412
pyx_path = 'bencoder.pyx'
1513
c_path = 'bencoder.c'
@@ -38,12 +36,14 @@
3836
from Cython.Build import cythonize
3937
ext_modules = cythonize(Extension(
4038
"bencoder",
41-
[pyx_path]
39+
[pyx_path],
40+
extra_compile_args=['-O3']
4241
))
4342
else:
4443
ext_modules = [Extension(
4544
'bencoder',
46-
[c_path]
45+
[c_path],
46+
extra_compile_args=['-O3']
4747
)]
4848

4949

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

5757
def run_tests(self):
58-
#import here, cause outside the eggs aren't loaded
58+
# import here, cause outside the eggs aren't loaded
5959
import coverage
6060
cov = coverage.Coverage()
6161
cov.start()
@@ -97,7 +97,7 @@ def get_tag(self):
9797

9898
setup(
9999
name='bencoder.pyx',
100-
version='1.2.0',
100+
version='1.2.1',
101101
description='Yet another bencode implementation in Cython',
102102
long_description=open('README.rst', 'r').read(),
103103
author='whtsky',
@@ -117,7 +117,6 @@ def get_tag(self):
117117
'Programming Language :: Cython',
118118
'Programming Language :: Python',
119119
'Programming Language :: Python :: 2',
120-
'Programming Language :: Python :: 2.6',
121120
'Programming Language :: Python :: 2.7',
122121
'Programming Language :: Python :: 3',
123122
'Programming Language :: Python :: 3.3',

tests/test_decode.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,37 @@ def test_decode2():
1616
assert length == 8
1717

1818

19-
def test_decode_str(benchmark):
20-
assert benchmark(bdecode, b'6:WWWWWW') == b"WWWWWW"
19+
def test_decode_str():
20+
assert bdecode(b'6:WWWWWW') == b"WWWWWW"
2121

2222

23-
def test_decode_int(benchmark):
24-
assert benchmark(bdecode, b'i233e') == 233
23+
def test_decode_int():
24+
assert bdecode(b'i233e') == 233
2525

2626

27-
def test_decode_large_int(benchmark):
27+
def test_decode_large_int():
2828
assert bdecode(b'i1455189890e') == 1455189890
2929
assert bdecode(b'i25735241490e') == 25735241490
3030

3131
MAX_SIZE = sys.maxsize + 1
3232
BENCODED_MAXSIZE = ('i%de' % MAX_SIZE).encode()
33-
assert benchmark(bdecode, BENCODED_MAXSIZE) == MAX_SIZE
33+
assert bdecode(BENCODED_MAXSIZE) == MAX_SIZE
3434

3535

36-
def test_decode_list(benchmark):
37-
assert benchmark(bdecode, b'l1:a1:bi3ee') == [b'a', b'b', 3]
36+
def test_decode_list():
37+
assert bdecode(b'l1:a1:bi3ee') == [b'a', b'b', 3]
3838

3939

40-
def test_decode_dict(benchmark):
40+
def test_decode_dict():
4141
od = dict()
4242
od[b'ka'] = b'va'
4343
od[b'kb'] = 2
44-
assert benchmark(bdecode, b'd2:ka2:va2:kbi2ee') == od
44+
assert bdecode(b'd2:ka2:va2:kbi2ee') == od
4545

4646

47-
def test_ordered_dict(benchmark):
47+
def test_ordered_dict():
4848
from bencoder import OrderedDict
49-
rv = benchmark(bdecode, b'd2:ka2:va2:kbi2ee')
49+
rv = bdecode(b'd2:ka2:va2:kbi2ee')
5050
assert isinstance(rv, OrderedDict)
5151
assert list(rv.keys()) == [b'ka', b'kb']
5252
assert list(bdecode(b'd2:kc2:va2:kei2ee').keys()) == [b'kc', b'ke']

tests/test_encode.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,68 @@
1010
)
1111

1212

13-
def test_encode_str(benchmark):
14-
assert benchmark(bencode, "WWWWWW") == b'6:WWWWWW'
13+
def test_encode_str():
14+
assert bencode("WWWWWW") == b'6:WWWWWW'
1515

1616

17-
def test_encode_int(benchmark):
18-
assert benchmark(bencode, 233) == b'i233e'
17+
def test_encode_int():
18+
assert bencode(233) == b'i233e'
1919

2020

21-
def test_encode_large_int(benchmark):
21+
def test_encode_large_int():
2222
assert bencode(1455189890) == b'i1455189890e'
2323
assert bencode(25735241490) == b'i25735241490e'
2424
MAX_SIZE = sys.maxsize + 1
2525
BENCODED_MAXSIZE = ('i%de' % MAX_SIZE).encode()
2626

27-
assert benchmark(bencode, MAX_SIZE) == BENCODED_MAXSIZE
27+
assert bencode(MAX_SIZE) == BENCODED_MAXSIZE
2828

2929

30-
def test_encode_bytes(benchmark):
30+
def test_encode_bytes():
3131
b = b"TheseAreSomeBytes"
32-
coded = benchmark(bencode, b)
32+
coded = bencode(b)
3333
l = str(len(b)).encode()
3434
assert coded == l + b':' + b
3535

3636

37-
def test_encode_string(benchmark):
37+
def test_encode_string():
3838
b = "TheseAreSomeString"
39-
coded = benchmark(bencode, b)
39+
coded = bencode(b)
4040
l = str(len(b))
4141
assert coded == (l + ':' + b).encode()
4242

4343

44-
def test_encode_list(benchmark):
45-
assert benchmark(bencode, ['a', 'b', 3]) == b'l1:a1:bi3ee'
44+
def test_encode_list():
45+
assert bencode(['a', 'b', 3]) == b'l1:a1:bi3ee'
4646

4747

48-
def test_encode_tuple(benchmark):
49-
assert benchmark(bencode, ('a', 'b', 3)) == b'l1:a1:bi3ee'
48+
def test_encode_tuple():
49+
assert bencode(('a', 'b', 3)) == b'l1:a1:bi3ee'
5050

5151

52-
def test_encode_true(benchmark):
53-
assert benchmark(bencode, True) == bencode(1)
52+
def test_encode_true():
53+
assert bencode(True) == bencode(1)
5454

5555

56-
def test_encode_false(benchmark):
57-
assert benchmark(bencode, False) == bencode(0)
56+
def test_encode_false():
57+
assert bencode(False) == bencode(0)
5858

5959

60-
def test_encode_dict(benchmark):
60+
def test_encode_dict():
6161
od = dict()
6262
od['ka'] = 'va'
6363
od['kb'] = 2
64-
assert benchmark(bencode, od) == b'd2:ka2:va2:kbi2ee'
64+
assert bencode(od) == b'd2:ka2:va2:kbi2ee'
6565

6666

67-
def test_encode_dict_subclass(benchmark):
67+
def test_encode_dict_subclass():
6868
class AAA(dict):
6969
pass
7070

7171
od = AAA()
7272
od['ka'] = 'va'
7373
od['kb'] = 2
74-
assert benchmark(bencode, od) == b'd2:ka2:va2:kbi2ee'
74+
assert bencode(od) == b'd2:ka2:va2:kbi2ee'
7575

7676

7777
def test_encode_complex():

tox-wheels.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py26, py27, py33, py34, py35, py36
2+
envlist = py27, py33, py34, py35, py36
33

44
[testenv]
55
commands = pip wheel {toxinidir} -w {toxinidir}/wheelhouse/

tox.ini

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py26, py27, py33, py34, py35, py36, pypy
2+
envlist = py27, py33, py34, py35, py36, pypy
33

44
[testenv]
55
commands =
@@ -11,13 +11,11 @@ deps =
1111
cython
1212
pytest
1313
coverage
14-
pytest-benchmark
1514
codecov
1615

1716
[testenv:pypy]
1817
skip_install = False
1918
deps =
2019
cython
2120
pytest
22-
pytest-benchmark
2321
commands = py.test

0 commit comments

Comments
 (0)