diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2013-08-27 08:46:35 +0000 |
---|---|---|
committer | Jelmer Vernooij <jelmer@jelmer.uk> | 2016-07-07 12:16:40 +0000 |
commit | c1952f923f008cd299a23e644db8af1d3b89e0cc (patch) | |
tree | d8550b5811b6a61bc92be0917c898c705d387341 | |
parent | fbceddcbf68640d2dda9301197806153d482f5a5 (diff) |
Use byte strings in marshall
Patch by Martin Panter.
-rw-r--r-- | subvertpy/marshall.py | 50 | ||||
-rw-r--r-- | subvertpy/tests/test_marshall.py | 32 |
2 files changed, 41 insertions, 41 deletions
diff --git a/subvertpy/marshall.py b/subvertpy/marshall.py index f51f0ec5..d55b157a 100644 --- a/subvertpy/marshall.py +++ b/subvertpy/marshall.py @@ -55,45 +55,45 @@ def marshall(x): """Marshall a Python data item. :param x: Data item - :return: encoded string + :return: encoded byte string """ if isinstance(x, int): - return "%d " % x + return ("%d " % x).encode("ascii") elif isinstance(x, (list, tuple)): - return "( " + "".join(map(marshall, x)) + ") " + return b"( " + bytes().join(map(marshall, x)) + b") " elif isinstance(x, literal): - return "%s " % x + return ("%s " % x).encode("ascii") elif isinstance(x, bytes): - return "%d:%s " % (len(x), x) + return ("%d:" % len(x)).encode("ascii") + x + b" " elif isinstance(x, str): x = x.encode("utf-8") - return "%d:%s " % (len(x), x) + return ("%d:" % len(x)).encode("ascii") + x + b" " elif isinstance(x, bool): if x == True: - return "true " + return b"true " elif x == False: - return "false " + return b"false " raise MarshallError("Unable to marshall type %s" % x) def unmarshall(x): - """Unmarshall the next item from a text. + """Unmarshall the next item from a buffer. - :param x: Text to parse - :return: tuple with unpacked item and remaining text + :param x: Bytes to parse + :return: tuple with unpacked item and remaining bytes """ - whitespace = ['\n', ' '] + whitespace = frozenset(b'\n ') if len(x) == 0: raise NeedMoreData("Not enough data") - if x[0] == "(": # list follows + if x[0] == ord(b"("): # list follows if len(x) <= 1: raise NeedMoreData("Missing whitespace") - if x[1] != " ": + if x[1] != ord(b" "): raise MarshallError("missing whitespace after list start") x = x[2:] ret = [] try: - while x[0] != ")": + while x[0] != ord(b")"): (x, n) = unmarshall(x) ret.append(n) except IndexError: @@ -106,28 +106,28 @@ def unmarshall(x): raise MarshallError("Expected space, got '%c'" % x[1]) return (x[2:], ret) - elif x[0].isdigit(): - num = "" + elif x[:1].isdigit(): + num = bytearray() # Check if this is a string or a number - while x[0].isdigit(): - num += x[0] + while x[:1].isdigit(): + num.append(x[0]) x = x[1:] num = int(num) if x[0] in whitespace: return (x[1:], num) - elif x[0] == ":": + elif x[0] == ord(b":"): if len(x) < num: raise NeedMoreData("Expected string of length %r" % num) return (x[num+2:], x[1:num+1]) else: raise MarshallError("Expected whitespace or ':', got '%c'" % x[0]) - elif x[0].isalpha(): - ret = "" + elif x[:1].isalpha(): + ret = bytearray() # Parse literal try: - while x[0].isalpha() or x[0].isdigit() or x[0] == '-': - ret += x[0] + while x[:1].isalpha() or x[:1].isdigit() or x[0] == ord(b'-'): + ret.append(x[0]) x = x[1:] except IndexError: raise NeedMoreData("Expected literal") @@ -135,7 +135,7 @@ def unmarshall(x): if not x[0] in whitespace: raise MarshallError("Expected whitespace, got '%c'" % x[0]) - return (x[1:], ret) + return (x[1:], ret.decode("ascii")) else: raise MarshallError("Unexpected character '%c'" % x[0]) diff --git a/subvertpy/tests/test_marshall.py b/subvertpy/tests/test_marshall.py index 7dca4eaf..9884e4a3 100644 --- a/subvertpy/tests/test_marshall.py +++ b/subvertpy/tests/test_marshall.py @@ -43,53 +43,53 @@ class TestMarshalling(TestCase): self.assertEqual("bla bla", e.__str__()) def test_marshall_int(self): - self.assertEqual("1 ", marshall(1)) + self.assertEqual(b"1 ", marshall(1)) def test_marshall_list(self): - self.assertEqual("( 1 2 3 4 ) ", marshall([1,2,3,4])) + self.assertEqual(b"( 1 2 3 4 ) ", marshall([1,2,3,4])) def test_marshall_list_mixed(self): - self.assertEqual("( 1 3 4 3:str ) ", marshall([1,3,4,"str"])) + self.assertEqual(b"( 1 3 4 3:str ) ", marshall([1,3,4,"str"])) def test_marshall_literal(self): - self.assertEqual("foo ", marshall(literal("foo"))) + self.assertEqual(b"foo ", marshall(literal("foo"))) def test_marshall_string(self): - self.assertEqual("3:foo ", marshall("foo")) + self.assertEqual(b"3:foo ", marshall("foo")) def test_marshall_raises(self): self.assertRaises(MarshallError, marshall, dict()) def test_marshall_list_nested(self): - self.assertEqual("( ( ( 3 ) 4 ) ) ", marshall([[[3], 4]])) + self.assertEqual(b"( ( ( 3 ) 4 ) ) ", marshall([[[3], 4]])) def test_marshall_string_space(self): - self.assertEqual("5:bla l ", marshall("bla l")) + self.assertEqual(b"5:bla l ", marshall("bla l")) def test_unmarshall_string(self): - self.assertEqual(('', "bla l"), unmarshall("5:bla l")) + self.assertEqual((b'', b"bla l"), unmarshall(b"5:bla l")) def test_unmarshall_list(self): - self.assertEqual(('', [4,5]), unmarshall("( 4 5 ) ")) + self.assertEqual((b'', [4,5]), unmarshall(b"( 4 5 ) ")) def test_unmarshall_int(self): - self.assertEqual(('', 2), unmarshall("2 ")) + self.assertEqual((b'', 2), unmarshall(b"2 ")) def test_unmarshall_literal(self): - self.assertEqual(('', literal("x")), unmarshall("x ")) + self.assertEqual((b'', literal("x")), unmarshall(b"x ")) def test_unmarshall_empty(self): - self.assertRaises(MarshallError, unmarshall, "") + self.assertRaises(MarshallError, unmarshall, b"") def test_unmarshall_nospace(self): - self.assertRaises(MarshallError, unmarshall, "nospace") + self.assertRaises(MarshallError, unmarshall, b"nospace") def test_unmarshall_toolong(self): - self.assertRaises(MarshallError, unmarshall, "43432432:bla") + self.assertRaises(MarshallError, unmarshall, b"43432432:bla") def test_unmarshall_literal(self): - self.assertRaises(MarshallError, unmarshall, ":-3213") + self.assertRaises(MarshallError, unmarshall, b":-3213") def test_unmarshall_open_list(self): - self.assertRaises(MarshallError, unmarshall, "( 3 4 ") + self.assertRaises(MarshallError, unmarshall, b"( 3 4 ") |