This is an automated email from the ASF dual-hosted git repository. spmallette pushed a commit to branch TINKERPOP-2279 in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit 1e6d18f73b875d82a2891a465640a3a3ae1786f7 Author: Stephen Mallette <[email protected]> AuthorDate: Mon Jul 22 14:14:01 2019 -0400 Added Long graphbinary support to python Fixed bug in string serialization that only occurred on python 3.x --- .../gremlin_python/structure/io/graphbinaryV1.py | 30 +++++++++++++++++----- .../tests/structure/io/test_graphbinaryV1.py | 5 ++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py index 9c9ab30..225b399 100644 --- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py +++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py @@ -49,6 +49,7 @@ _deserializers = {} class DataType(Enum): int = 0x01 + long = 0x02 string = 0x03 list = 0x09 @@ -146,18 +147,33 @@ class _GraphBinaryTypeIO(object): raise NotImplementedError() -class IntIO(_GraphBinaryTypeIO): +class LongIO(_GraphBinaryTypeIO): - python_type = IntType - graphbinary_type = DataType.int + python_type = LongType + graphbinary_type = DataType.long + byte_format = ">q" @classmethod def dictify(cls, n, writer): - return cls.as_bytes(cls.graphbinary_type, None, struct.pack(">i", n)) + if n < -9223372036854775808 or n > 9223372036854775807: + raise Exception("TODO: don't forget bigint") + else: + return cls.as_bytes(cls.graphbinary_type, None, struct.pack(cls.byte_format, n)) @classmethod - def objectify(cls, b, reader): - return cls.read_int(b) + def objectify(cls, buff, reader): + return struct.unpack(">q", buff.read(8))[0] + + +class IntIO(LongIO): + + python_type = IntType + graphbinary_type = DataType.int + byte_format = ">i" + + @classmethod + def objectify(cls, buff, reader): + return cls.read_int(buff) class StringIO(_GraphBinaryTypeIO): @@ -171,7 +187,7 @@ class StringIO(_GraphBinaryTypeIO): @classmethod def objectify(cls, b, reader): - return b.read(cls.read_int(b)) + return b.read(cls.read_int(b)).decode("utf-8") class ListIO(_GraphBinaryTypeIO): diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py index b95b0e0..766544d 100644 --- a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py +++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py @@ -48,6 +48,11 @@ class TestGraphSONWriter(object): output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x)) assert x == output + def test_long(self): + x = long(100) + output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x)) + assert x == output + def test_string(self): x = "serialize this!" output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
