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 /subvertpy/marshall.py | |
parent | fbceddcbf68640d2dda9301197806153d482f5a5 (diff) |
Use byte strings in marshall
Patch by Martin Panter.
Diffstat (limited to 'subvertpy/marshall.py')
-rw-r--r-- | subvertpy/marshall.py | 50 |
1 files changed, 25 insertions, 25 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]) |