Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please merge Xpra changes #3

Merged
merged 13 commits into from
Jun 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rencode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
try:
from rencode._rencode import *
from rencode._rencode import __version__
except ImportError:
import rencode.rencode_orig
prev_all = rencode.rencode_orig.__all__[:]
del rencode.rencode_orig.__all__
from rencode.rencode_orig import *
from rencode.rencode_orig import __version__
rencode.rencode_orig.__all__ = prev_all

__all__ = ['dumps', 'loads']
22 changes: 15 additions & 7 deletions rencode/rencode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@

import sys

py3 = False
if sys.version_info.major >= 3:
py3 = True
py3 = sys.version_info[0] >= 3
if py3:
unicode = str

from cpython cimport bool
from libc.stdlib cimport realloc, malloc, free
from libc.string cimport memcpy

__version__ = "1.0.2"
__version__ = ("Cython", 1, 0, 3)

cdef long long data_length = 0
cdef bool _decode_utf8 = False
Expand Down Expand Up @@ -261,16 +260,23 @@ cdef encode_dict(char **buf, int *pos, x):
encode(buf, pos, v)
write_buffer_char(buf, pos, CHR_TERM)

cdef object MAX_SIGNED_INT = 2**31
cdef object MIN_SIGNED_INT = -MAX_SIGNED_INT
#note: negating the Python value avoids compiler problems
#(negating the "long long" constant can make it unsigned with some compilers!)
cdef object MAX_SIGNED_LONGLONG = int(2**63)
cdef object MIN_SIGNED_LONGLONG = -MAX_SIGNED_LONGLONG

cdef encode(char **buf, int *pos, data):
t = type(data)
if t == int or t == long:
if -128 <= data < 128:
encode_char(buf, pos, data)
elif -32768 <= data < 32768:
encode_short(buf, pos, data)
elif -2147483648 <= data < 2147483648:
elif MIN_SIGNED_INT <= data < MAX_SIGNED_INT:
encode_int(buf, pos, data)
elif -9223372036854775808 <= data < 9223372036854775808:
elif MIN_SIGNED_LONGLONG <= data < MAX_SIGNED_LONGLONG:
encode_long_long(buf, pos, data)
else:
s = str(data)
Expand Down Expand Up @@ -306,6 +312,8 @@ cdef encode(char **buf, int *pos, data):
elif t == dict:
encode_dict(buf, pos, data)

else:
raise Exception("type %s not handled" % t)

def dumps(data, float_bits=DEFAULT_FLOAT_BITS):
"""
Expand Down
36 changes: 15 additions & 21 deletions rencode/rencode_orig.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
same rencode version throughout your project.
"""

__version__ = '1.0.2'
__version__ = ("Python", 1, 0, 3)
__all__ = ['dumps', 'loads']

# Original bencode module by Petru Paler, et al.
Expand Down Expand Up @@ -64,18 +64,16 @@

import sys

py3 = False
if sys.version_info.major >= 3:
py3 = True
py3 = sys.version_info[0] >= 3
if py3:
long = int
unicode = str

def int2byte(c):
if py3:
def int2byte(c):
return bytes([c])
else:
else:
def int2byte(c):
return chr(c)

import struct
from threading import Lock

Expand Down Expand Up @@ -196,13 +194,13 @@ def decode_dict(x, f):
return (r, f + 1)

def decode_true(x, f):
return (True, f+1)
return (True, f+1)

def decode_false(x, f):
return (False, f+1)
return (False, f+1)

def decode_none(x, f):
return (None, f+1)
return (None, f+1)

decode_func = {}
decode_func[b'0'] = decode_string
Expand Down Expand Up @@ -245,7 +243,7 @@ def make_fixed_length_list_decoders():
def make_decoder(slen):
def f(x, f):
r, f = [], f+1
for i in range(slen):
for _ in range(slen):
v, f = decode_func[x[f:f+1]](x, f)
r.append(v)
return (tuple(r), f)
Expand All @@ -271,7 +269,7 @@ def make_fixed_length_dict_decoders():
def make_decoder(slen):
def f(x, f):
r, f = {}, f+1
for j in range(slen):
for _ in range(slen):
k, f = decode_func[x[f:f+1]](x, f)
r[k], f = decode_func[x[f:f+1]](x, f)
return (r, f)
Expand Down Expand Up @@ -309,7 +307,7 @@ def encode_int(x, r):
s = str(x)
if py3:
s = bytes(s, "ascii")

if len(s) >= MAX_INT_LENGTH:
raise ValueError('overflow')
r.extend((CHR_INT, s, CHR_TERM))
Expand Down Expand Up @@ -381,8 +379,7 @@ def dumps(x, float_bits=DEFAULT_FLOAT_BITS):

Here float_bits is either 32 or 64.
"""
lock.acquire()
try:
with lock:
if float_bits == 32:
encode_func[float] = encode_float32
elif float_bits == 64:
Expand All @@ -391,9 +388,6 @@ def dumps(x, float_bits=DEFAULT_FLOAT_BITS):
raise ValueError('Float bits (%d) is not 32 or 64' % float_bits)
r = []
encode_func[type(x)](x, r)
finally:
lock.release()

return b''.join(r)

def test():
Expand Down Expand Up @@ -433,4 +427,4 @@ def test():


if __name__ == '__main__':
test()
test()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _unavailable(self, exc):

setup(
name="rencode",
version="1.0.2",
version="1.0.3",
packages=["rencode"],
description=description,
author="Andrew Resch",
Expand Down
12 changes: 10 additions & 2 deletions tests/test_rencode.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,17 @@ def test_decode_str_nullbytes(self):

def test_decode_utf8(self):
s = b"foobarbaz"
self.assertIsInstance(rencode.loads(rencode.dumps(s), decode_utf8=True), unicode)
#no assertIsInstance with python2.6
d = rencode.loads(rencode.dumps(s), decode_utf8=True)
if not isinstance(d, unicode):
self.fail('%s is not an instance of %r' % (repr(d), unicode))
s = rencode.dumps(b"\x56\xe4foo\xc3")
self.assertRaises(UnicodeDecodeError, rencode.loads, s, decode_utf8=True)


def test_version_exposed(self):
assert rencode.__version__
assert rencode_orig.__version__
self.assertEqual(rencode.__version__[1:], rencode_orig.__version__[1:], "version number does not match")

if __name__ == '__main__':
unittest.main()
1 change: 0 additions & 1 deletion tests/timetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ def test_overall_decode_orig():

if __name__ == "__main__":
import timeit
import sys

iterations = 10000
# ANSI escape codes
Expand Down