Skip to content

Commit

Permalink
Py3k fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
geertj committed Aug 21, 2013
1 parent b2a40db commit 3dcb2fc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
install: pip install -r requirements.txt --use-mirrors
script: nosetests
4 changes: 2 additions & 2 deletions gruvi/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def feed(self, line):
raise ValueError('client must initiate handshake')
self._state = self.s_auth_external
return b'\0AUTH EXTERNAL\r\n'
if not line.endswith('\r\n'):
if not line.endswith(b'\r\n'):
raise ValueError('Incomplete line')
pos = line.find(' ')
pos = line.find(b' ')
if pos != -1:
command = line[:pos]
args = line[pos+1:-2]
Expand Down
2 changes: 1 addition & 1 deletion gruvi/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def _send_message(self, transport, message):
raise RuntimeError('not connected')
if not check_message(message):
raise ValueError('illegal JSON-RPC message')
serialized = json.dumps(message, ensure_ascii=True)
serialized = json.dumps(message, ensure_ascii=True).encode('ascii')
self._write(transport, serialized)


Expand Down
40 changes: 20 additions & 20 deletions gruvi/test/test_dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,51 +166,51 @@ def test_incremental(self):
m = b'l\1\0\1\0\0\0\0\1\0\0\0\0\0\0\0'
offset = state = 0
ctx = dbus_ffi.ffi.new('struct context *')
for ch in m[:-1]:
set_buffer(ctx, ch)
for i in range(len(m)-1):
set_buffer(ctx, m[i:i+1])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == dbus_ffi.lib.INCOMPLETE
assert ctx.offset == 1
set_buffer(ctx, m[-1])
set_buffer(ctx, m[-1:])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == 0
assert ctx.offset == 1

def test_incremental_with_body(self):
m = b'l\1\0\1\4\0\0\0\1\0\0\0\0\0\0\0abcd'
ctx = dbus_ffi.ffi.new('struct context *')
for ch in m[:-1]:
set_buffer(ctx, ch)
for i in range(len(m)-1):
set_buffer(ctx, m[i:i+1])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == dbus_ffi.lib.INCOMPLETE
assert ctx.offset == 1
set_buffer(ctx, m[-1])
set_buffer(ctx, m[-1:])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == 0
assert ctx.offset == 1

def test_incremental_with_header(self):
m = b'l\1\0\1\0\0\0\0\1\0\0\0\10\0\0\0h2345678'
ctx = dbus_ffi.ffi.new('struct context *')
for ch in m[:-1]:
set_buffer(ctx, ch)
for i in range(len(m)-1):
set_buffer(ctx, m[i:i+1])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == dbus_ffi.lib.INCOMPLETE
assert ctx.offset == 1
set_buffer(ctx, m[-1])
set_buffer(ctx, m[-1:])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == 0
assert ctx.offset == 1

def test_incremental_with_header_and_body(self):
m = b'l\1\0\1\4\0\0\0\1\0\0\0\4\0\0\0h23456781234'
ctx = dbus_ffi.ffi.new('struct context *')
for ch in m[:-1]:
set_buffer(ctx, ch)
for i in range(len(m)-1):
set_buffer(ctx, m[i:i+1])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == dbus_ffi.lib.INCOMPLETE
assert ctx.offset == 1
set_buffer(ctx, m[-1])
set_buffer(ctx, m[-1:])
error = dbus_ffi.lib.split(ctx)
assert error == ctx.error == 0
assert ctx.offset == 1
Expand Down Expand Up @@ -268,11 +268,11 @@ def test_multiple(self):
def test_incremental(self):
m = b'l\1\0\1\0\0\0\0\1\0\0\0\0\0\0\0'
parser = DBusParser()
for ch in m[:-1]:
parser.feed(ch)
for i in range(len(m)-1):
parser.feed(m[i:i+1])
assert parser.pop_message() is None
assert parser.is_partial()
parser.feed(m[-1])
parser.feed(m[-1:])
msg = parser.pop_message()
assert isinstance(msg, txdbus.DBusMessage)
assert not parser.is_partial()
Expand All @@ -281,14 +281,14 @@ def test_illegal_message(self):
m = b'l\1\0\2\0\0\0\0\1\0\0\0\0\0\0\0'
parser = DBusParser()
exc = assert_raises(ParseError, parser.feed, m)
assert exc[0] == errno.FRAMING_ERROR
assert exc.args[0] == errno.FRAMING_ERROR

def test_maximum_message_size_exceeded(self):
parser = DBusParser()
parser.max_message_size = 100
m = b'l\1\0\1\0\1\0\0\1\0\0\0\0\0\0\0' + b'x' * 256
exc = assert_raises(ParseError, parser.feed, m)
assert exc[0] == errno.MESSAGE_TOO_LARGE
assert exc.args[0] == errno.MESSAGE_TOO_LARGE


def uses_host_dbus(test):
Expand All @@ -315,11 +315,11 @@ def username(self):

def feed(self, line):
if self._state == self.s_start:
if line == '\0AUTH EXTERNAL\r\n':
if line == b'\0AUTH EXTERNAL\r\n':
self._state = self.s_begin
return 'OK\r\n'
return b'OK\r\n'
elif self._state == self.s_begin:
if line == 'BEGIN\r\n':
if line == b'BEGIN\r\n':
self._state = self.s_authenticated
self._username = '<external>'

Expand Down
73 changes: 37 additions & 36 deletions gruvi/test/test_jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ def split_string(s):
class TestJsonRpcFFI(UnitTest):

def test_simple(self):
r = '{ "foo": "bar" }'
r = b'{ "foo": "bar" }'
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == len(r)

def test_leading_whitespace(self):
r = ' { "foo": "bar" }'
r = b' { "foo": "bar" }'
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == len(r)
r = ' \t\n{ "foo": "bar" }'
r = b' \t\n{ "foo": "bar" }'
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == len(r)

def test_trailing_whitespace(self):
r = '{ "foo": "bar" } '
r = b'{ "foo": "bar" } '
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == len(r)-1
Expand All @@ -64,33 +64,33 @@ def test_trailing_whitespace(self):
assert ctx.offset == len(r)

def test_brace_in_string(self):
r = '{ "foo": "b{r" }'
r = b'{ "foo": "b{r" }'
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == len(r)
r = '{ "foo": "b}r" }'
r = b'{ "foo": "b}r" }'
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == len(r)

def test_string_escape(self):
r = r'{ "foo": "b\"}" }'
r = b'{ "foo": "b\\"}" }'
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == len(r)

def test_error(self):
r = ' x { "foo": "bar" }'
r = b' x { "foo": "bar" }'
ctx = split_string(r)
assert ctx.error == jsonrpc_ffi.lib.ERROR
assert ctx.offset == 1
r = '[ { "foo": "bar" } ]'
r = b'[ { "foo": "bar" } ]'
ctx = split_string(r)
assert ctx.error == jsonrpc_ffi.lib.ERROR
assert ctx.offset == 0

def test_multiple(self):
r = '{ "foo": "bar" } { "baz": "qux" }'
r = b'{ "foo": "bar" } { "baz": "qux" }'
ctx = split_string(r)
assert ctx.error == 0
assert ctx.offset == 16
Expand All @@ -99,22 +99,22 @@ def test_multiple(self):
assert ctx.offset == len(r)

def test_incremental(self):
r = '{ "foo": "bar" }'
r = b'{ "foo": "bar" }'
state = 0
ctx = jsonrpc_ffi.ffi.new('struct context *')
for ch in r[:-1]:
set_buffer(ctx, ch)
for i in range(len(r)-1):
set_buffer(ctx, r[i:i+1])
error = jsonrpc_ffi.lib.split(ctx)
assert error == ctx.error == jsonrpc_ffi.lib.INCOMPLETE
assert ctx.offset == 1
buf = ctx.buf = jsonrpc_ffi.ffi.new('char[]', r[-1])
buf = ctx.buf = jsonrpc_ffi.ffi.new('char[]', r[-1:])
ctx.buflen = 1; ctx.offset = 0
error = jsonrpc_ffi.lib.split(ctx)
assert error == ctx.error == 0
assert ctx.offset == 1

def test_performance(self):
chunk = '{' + 'x' * 100 + '}'
chunk = b'{' + b'x' * 100 + b'}'
buf = chunk * 100
ctx = jsonrpc_ffi.ffi.new('struct context *')
nbytes = 0
Expand All @@ -135,7 +135,7 @@ def test_performance(self):
class TestJsonRpcParser(UnitTest):

def test_simple(self):
m = '{ "id": "1", "method": "foo" }'
m = b'{ "id": "1", "method": "foo" }'
parser = JsonRpcParser()
parser.feed(m)
msg = parser.pop_message()
Expand All @@ -145,8 +145,8 @@ def test_simple(self):
assert msg is None

def test_multiple(self):
m = '{ "id": "1", "method": "foo" }' \
'{ "id": "2", "method": "bar" }'
m = b'{ "id": "1", "method": "foo" }' \
b'{ "id": "2", "method": "bar" }'
parser = JsonRpcParser()
parser.feed(m)
msg = parser.pop_message()
Expand All @@ -157,8 +157,8 @@ def test_multiple(self):
assert msg is None

def test_whitespace(self):
m = ' { "id": "1", "method": "foo" }' \
' { "id": "2", "method": "bar" }'
m = b' { "id": "1", "method": "foo" }' \
b' { "id": "2", "method": "bar" }'
parser = JsonRpcParser()
parser.feed(m)
msg = parser.pop_message()
Expand All @@ -169,47 +169,48 @@ def test_whitespace(self):
assert msg is None

def test_incremental(self):
m = '{ "id": "1", "method": "foo" }'
m = b'{ "id": "1", "method": "foo" }'
parser = JsonRpcParser()
for ch in m[:-1]:
parser.feed(ch)
for i in range(len(m)-1):
parser.feed(m[i:i+1])
assert parser.pop_message() is None
assert parser.is_partial()
parser.feed(m[-1])
parser.feed(m[-1:])
msg = parser.pop_message()
assert not parser.is_partial()
assert msg == { 'id': '1', 'method': 'foo' }

def test_framing_error(self):
m = 'xxx'
m = b'xxx'
parser = JsonRpcParser()
exc = assert_raises(ParseError, parser.feed, m)
assert exc[0] == errno.FRAMING_ERROR
assert exc.args[0] == errno.FRAMING_ERROR

def test_encoding_error(self):
m = '{ xxx\xff }'
m = b'{ xxx\xff }'
parser = JsonRpcParser()
exc = assert_raises(ParseError, parser.feed, m)
assert exc[0] == errno.PARSE_ERROR
assert exc.args[0] == errno.PARSE_ERROR

def test_illegal_json(self):
m = '{ "xxxx" }'
m = b'{ "xxxx" }'
parser = JsonRpcParser()
exc = assert_raises(ParseError, parser.feed, m)
assert exc[0] == errno.PARSE_ERROR
assert exc.args[0] == errno.PARSE_ERROR

def test_illegal_jsonrpc(self):
m = '{ "xxxx": "yyyy" }'
m = b'{ "xxxx": "yyyy" }'
parser = JsonRpcParser()
exc = assert_raises(ParseError, parser.feed, m)
assert exc[0] == errno.PARSE_ERROR
assert exc.args[0] == errno.PARSE_ERROR

def test_maximum_message_size_exceeded(self):
parser = JsonRpcParser()
parser.max_message_size = 100
message = '{{ "{0}": "{1}" }}'.format('x' * 100, 'y' * 100)
message = message.encode('ascii')
exc = assert_raises(ParseError, parser.feed, message)
assert exc[0] == errno.MESSAGE_TOO_LARGE
assert exc.args[0] == errno.MESSAGE_TOO_LARGE


def echo_app(message, endpoint, transport):
Expand Down Expand Up @@ -269,7 +270,7 @@ def test_call_method_error(self):
client = JsonRpcClient()
client.connect(addr)
exc = assert_raises(JsonRpcError, client.call_method, 'echo2')
assert exc[0] == errno.INVALID_REQUEST
assert exc.args[0] == errno.INVALID_REQUEST

def test_send_notification(self):
server = JsonRpcServer(notification_app())
Expand Down Expand Up @@ -336,7 +337,7 @@ def test_send_evil(self):
client = StreamClient()
client.connect(addr)
try:
chunk = '{' * 1024
chunk = b'{' * 1024
while True:
client.write(chunk)
except pyuv.error.TCPError as e:
Expand All @@ -352,7 +353,7 @@ def test_send_whitespace(self):
client = StreamClient()
client.connect(addr)
try:
chunk = ' ' * 1024
chunk = b' ' * 1024
while True:
client.write(chunk)
except pyuv.error.TCPError as e:
Expand Down

0 comments on commit 3dcb2fc

Please sign in to comment.