You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/08/19 19:51:47 UTC

[tinkerpop] branch TINKERPOP-2279 updated: Added SingleChar support to python graphbinary

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


The following commit(s) were added to refs/heads/TINKERPOP-2279 by this push:
     new 98ba8ec  Added SingleChar support to python graphbinary
98ba8ec is described below

commit 98ba8ec0e23c326374417b57ecfcf380bf553d2b
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Mon Aug 19 15:51:21 2019 -0400

    Added SingleChar support to python graphbinary
---
 .../gremlin_python/structure/io/graphbinaryV1.py   | 29 +++++++++++++++++++++-
 .../tests/structure/io/test_functionalityio.py     | 24 ++++++++++++++++++
 .../tests/structure/io/test_graphbinaryV1.py       |  8 ++++++
 3 files changed, 60 insertions(+), 1 deletion(-)

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 e37ad8e3..b29b3d9 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
@@ -30,7 +30,7 @@ import logging
 from aenum import Enum
 from gremlin_python import statics
 from gremlin_python.statics import FloatType, FunctionType, IntType, LongType, TypeType, DictType, ListType, SetType, \
-                                   SingleByte, ByteBufferType, GremlinType
+                                   SingleByte, ByteBufferType, GremlinType, SingleChar
 from gremlin_python.process.traversal import Barrier, Binding, Bytecode, Cardinality, Column, Direction, Operator, \
                                              Order, Pick, Pop, P, Scope, TextP, Traversal, Traverser, \
                                              TraversalStrategy, T
@@ -93,6 +93,7 @@ class DataType(Enum):
     tree = 0x2b                   # no tree object in Python yet
     metrics = 0x2c
     traversalmetrics = 0x2d
+    char = 0x80
     custom = 0x00                 #todo
 
 
@@ -328,6 +329,31 @@ class DoubleIO(FloatIO):
         return cls.is_null(buff, reader, lambda b, r: struct.unpack(cls.byte_format, b.read(8))[0], nullable)
 
 
+class CharIO(_GraphBinaryTypeIO):
+    python_type = SingleChar
+    graphbinary_type = DataType.char
+
+    @classmethod
+    def dictify(cls, obj, writer, as_value=False, nullable=True):
+        ba = obj.encode("utf-8")
+        return cls.as_bytes(cls.graphbinary_type, as_value, nullable, ba)
+
+    @classmethod
+    def objectify(cls, buff, reader, nullable=True):
+        return cls.is_null(buff, reader, cls._read_char, nullable)
+
+    @classmethod
+    def _read_char(cls, b, r):
+        max_bytes = 4
+        x = b.read(1)
+        while max_bytes > 0:
+            max_bytes = max_bytes - 1
+            try:
+                return x.decode("utf-8")
+            except UnicodeDecodeError:
+                x += b.read(1)
+
+
 class StringIO(_GraphBinaryTypeIO):
 
     python_type = str
@@ -963,3 +989,4 @@ class TraversalStrategySerializer(_GraphBinaryTypeIO):
     @classmethod
     def _convert(cls, v):
         return v.bytecode if isinstance(v, Traversal) else v
+
diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_functionalityio.py b/gremlin-python/src/main/jython/tests/structure/io/test_functionalityio.py
index 54dd35e..1aed19a 100644
--- a/gremlin-python/src/main/jython/tests/structure/io/test_functionalityio.py
+++ b/gremlin-python/src/main/jython/tests/structure/io/test_functionalityio.py
@@ -20,6 +20,7 @@ under the License.
 import datetime
 import uuid
 
+from gremlin_python.driver.serializer import GraphSONSerializersV2d0
 from gremlin_python.structure.graph import Graph
 from gremlin_python.statics import *
 
@@ -62,3 +63,26 @@ def test_uuid(remote_connection):
         assert uid_prop.value == uid
     finally:
         g.V(vid).drop().iterate()
+
+
+def test_odd_bits(remote_connection):
+    if not isinstance(remote_connection._client._message_serializer, GraphSONSerializersV2d0):
+        g = Graph().traversal().withRemote(remote_connection)
+        char_lower = str.__new__(SingleChar, chr(78))
+        resp = g.addV('test_vertex').property('char_lower', char_lower).toList()
+        vid = resp[0].id
+        try:
+            v = g.V(vid).values('char_lower').toList()[0]
+            assert v == char_lower
+        finally:
+            g.V(vid).drop().iterate()
+
+        if six.PY3:
+            char_upper = str.__new__(SingleChar, chr(57344))
+            resp = g.addV('test_vertex').property('char_upper', char_upper).toList()
+            vid = resp[0].id
+            try:
+                v = g.V(vid).values('char_upper').toList()[0]
+                assert v == char_upper
+            finally:
+                g.V(vid).drop().iterate()
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 917e7f2..4a2a2f3 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
@@ -196,4 +196,12 @@ class TestGraphSONWriter(object):
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output
 
+    def test_char(self):
+        x = str.__new__(SingleChar, chr(76))
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
 
+        if six.PY3:
+            x = str.__new__(SingleChar, chr(57344))
+            output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+            assert x == output