Skip to content

Commit 99d3f51

Browse files
committed
Compatibility improvement
1 parent 6dfcbb4 commit 99d3f51

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

bencoder.pyx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ from cpython.version cimport PY_MAJOR_VERSION
2626
IS_PY2 = PY_MAJOR_VERSION == 2
2727
if IS_PY2:
2828
END_CHAR = 'e'
29+
ARRAY_TYPECODE = b'b'
2930
else:
3031
END_CHAR = ord('e')
32+
ARRAY_TYPECODE = 'b'
3133

3234

3335
class BTFailure(Exception):
@@ -111,46 +113,46 @@ def encode(v, r):
111113

112114

113115
def encode_int(long x, r):
114-
r.frombytes(b'i')
115-
r.frombytes(str(x).encode())
116-
r.frombytes(b'e')
116+
r.fromstring(b'i')
117+
r.fromstring(str(x).encode())
118+
r.fromstring(b'e')
117119

118120

119121
def encode_long(x, r):
120-
r.frombytes(b'i')
121-
r.frombytes(str(x).encode())
122-
r.frombytes(b'e')
122+
r.fromstring(b'i')
123+
r.fromstring(str(x).encode())
124+
r.fromstring(b'e')
123125

124126

125127
def encode_bytes(bytes x, r):
126-
r.frombytes(str(len(x)).encode())
127-
r.frombytes(b':')
128-
r.frombytes(x)
128+
r.fromstring(str(len(x)).encode())
129+
r.fromstring(b':')
130+
r.fromstring(x)
129131

130132

131133
def encode_string(str x, r):
132-
r.frombytes(str(len(x)).encode())
133-
r.frombytes(b':')
134-
r.frombytes(x.encode())
134+
r.fromstring(str(len(x)).encode())
135+
r.fromstring(b':')
136+
r.fromstring(x.encode())
135137

136138

137139
def encode_list(x, r):
138-
r.frombytes(b'l')
140+
r.fromstring(b'l')
139141
for i in x:
140142
encode(i, r)
141-
r.frombytes(b'e')
143+
r.fromstring(b'e')
142144

143145

144146
def encode_dict(x, r):
145-
r.frombytes(b'd')
147+
r.fromstring(b'd')
146148
item_list = list(x.items())
147149
item_list.sort()
148150
for k, v in item_list:
149151
if isinstance(k, str):
150152
k = k.encode()
151153
encode_bytes(k, r)
152154
encode(v, r)
153-
r.frombytes(b'e')
155+
r.fromstring(b'e')
154156

155157

156158
encode_func = {
@@ -167,6 +169,6 @@ encode_func = {
167169

168170

169171
def bencode(x):
170-
cdef array.array r = array.array(b'b')
172+
cdef array.array r = array.array(ARRAY_TYPECODE)
171173
encode(x, r)
172-
return r.tobytes()
174+
return r.tostring()

setup.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,39 @@
1515
pyx_path = os.path.join(base_path, 'bencoder.pyx')
1616
c_path = os.path.join(base_path, 'bencoder.c')
1717

18-
if 'test' in sys.argv and platform.python_implementation() == 'CPython':
18+
try:
19+
import Cython
20+
HAVE_CYTHON = True
21+
except ImportError:
22+
HAVE_CYTHON = False
23+
24+
if HAVE_CYTHON:
1925
if os.path.exists(c_path):
2026
# Remove C file to force Cython recompile.
2127
os.remove(c_path)
22-
from Cython.Compiler.Options import directive_defaults
23-
24-
directive_defaults['linetrace'] = True
25-
directive_defaults['binding'] = True
26-
27-
from Cython.Build import cythonize
28-
ext_modules = cythonize(Extension(
29-
"bencoder",
30-
[pyx_path],
31-
define_macros=[('CYTHON_TRACE', '1')]
32-
))
33-
elif os.path.exists(c_path):
28+
if 'test' in sys.argv and platform.python_implementation() == 'CPython':
29+
from Cython.Compiler.Options import directive_defaults
30+
31+
directive_defaults['linetrace'] = True
32+
directive_defaults['binding'] = True
33+
34+
from Cython.Build import cythonize
35+
ext_modules = cythonize(Extension(
36+
"bencoder",
37+
[pyx_path],
38+
define_macros=[('CYTHON_TRACE', '1')]
39+
))
40+
else:
41+
from Cython.Build import cythonize
42+
ext_modules = cythonize(Extension(
43+
"bencoder",
44+
[pyx_path]
45+
))
46+
else:
3447
ext_modules = [Extension(
3548
'bencoder',
3649
[c_path]
3750
)]
38-
else:
39-
from Cython.Build import cythonize
40-
ext_modules = cythonize(Extension(
41-
"bencoder",
42-
[pyx_path]
43-
))
4451

4552

4653
class PyTest(TestCommand):

0 commit comments

Comments
 (0)