Added compatibility support for Python 2/3 in gremlin-python glv
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/b61f0d82 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/b61f0d82 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/b61f0d82 Branch: refs/heads/TINKERPOP-1442-master Commit: b61f0d827ee76b2bde3404d143c01a5edadbae18 Parents: cf8f6f7 Author: davebshow <davebs...@apache.org> Authored: Wed Sep 14 12:20:16 2016 -0400 Committer: davebshow <davebs...@apache.org> Committed: Wed Sep 14 12:20:16 2016 -0400 ---------------------------------------------------------------------- .../python/GraphTraversalSourceGenerator.groovy | 5 +-- .../python/TraversalSourceGenerator.groovy | 5 ++- .../src/main/jython/gremlin_python/compat.py | 34 ++++++++++++++++++++ .../driver/driver_remote_connection.py | 2 +- .../gremlin_python/driver/remote_connection.py | 2 +- .../gremlin_python/process/graph_traversal.py | 3 +- .../jython/gremlin_python/process/traversal.py | 6 +++- .../gremlin_python/structure/io/graphson.py | 10 +++--- .../driver/test_driver_remote_connection.py | 13 ++++---- .../jython/tests/structure/io/test_graphson.py | 16 ++++++--- .../main/jython/tests/structure/test_graph.py | 11 ++++--- 11 files changed, 80 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/GraphTraversalSourceGenerator.groovy ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/GraphTraversalSourceGenerator.groovy b/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/GraphTraversalSourceGenerator.groovy index 9187302..55f977c 100644 --- a/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/GraphTraversalSourceGenerator.groovy +++ b/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/GraphTraversalSourceGenerator.groovy @@ -60,7 +60,8 @@ under the License. pythonClass.append("from .traversal import TraversalStrategies\n") pythonClass.append("from .traversal import Bytecode\n") pythonClass.append("from ..driver.remote_connection import RemoteStrategy\n") - pythonClass.append("from .. import statics\n\n") + pythonClass.append("from .. import statics\n") + pythonClass.append("from ..compat import long\n\n") ////////////////////////// // GraphTraversalSource // @@ -128,7 +129,7 @@ under the License. if isinstance(index, int): return self.range(long(index), long(index + 1)) elif isinstance(index, slice): - return self.range(0L if index.start is None else long(index.start), long(sys.maxint) if index.stop is None else long(index.stop)) + return self.range(long(0) if index.start is None else long(index.start), long(sys.maxsize) if index.stop is None else long(index.stop)) else: raise TypeError("Index must be int or slice") def __getattr__(self, key): http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/TraversalSourceGenerator.groovy ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/TraversalSourceGenerator.groovy b/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/TraversalSourceGenerator.groovy index 79a1a4e..35e5b2f 100644 --- a/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/TraversalSourceGenerator.groovy +++ b/gremlin-python/src/main/groovy/org/apache/tinkerpop/gremlin/python/TraversalSourceGenerator.groovy @@ -57,6 +57,7 @@ under the License. pythonClass.append("import six\n") pythonClass.append("from aenum import Enum\n") pythonClass.append("from .. import statics\n") + pythonClass.append("from ..compat import long\n\n") pythonClass.append(""" class Traversal(object): @@ -182,7 +183,9 @@ TRAVERSER ''' class Traverser(object): - def __init__(self, object, bulk=1L): + def __init__(self, object, bulk=None): + if bulk is None: + bulk = long(1) self.object = object self.bulk = bulk def __repr__(self): http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/gremlin_python/compat.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/compat.py b/gremlin-python/src/main/jython/gremlin_python/compat.py new file mode 100644 index 0000000..8089d85 --- /dev/null +++ b/gremlin-python/src/main/jython/gremlin_python/compat.py @@ -0,0 +1,34 @@ +''' +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +''' + +from types import FunctionType + +import six + + +if six.PY3: + class long(int): pass + FloatType = float + IntType = int + LongType = long +else: + long = long + from types import FloatType + from types import IntType + from types import LongType http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py index 7aca638..cac5e73 100644 --- a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py +++ b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py @@ -174,7 +174,7 @@ class Response: if self._closed: return recv_message = yield self._websocket.read_message() - recv_message = json.loads(recv_message) + recv_message = json.loads(recv_message.decode('utf-8')) status_code = recv_message["status"]["code"] aggregateTo = recv_message["result"]["meta"].get("aggregateTo", "list") http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/gremlin_python/driver/remote_connection.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/remote_connection.py b/gremlin-python/src/main/jython/gremlin_python/driver/remote_connection.py index 491fffd..0b84a26 100644 --- a/gremlin-python/src/main/jython/gremlin_python/driver/remote_connection.py +++ b/gremlin-python/src/main/jython/gremlin_python/driver/remote_connection.py @@ -42,7 +42,7 @@ class RemoteConnection(object): @abc.abstractmethod def submit(self, bytecode): - print "sending " + bytecode + " to GremlinServer..." + print("sending " + bytecode + " to GremlinServer...") return RemoteTraversal(iter([]), TraversalSideEffects()) def __repr__(self): http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/gremlin_python/process/graph_traversal.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/process/graph_traversal.py b/gremlin-python/src/main/jython/gremlin_python/process/graph_traversal.py index 96985b7..974e127 100644 --- a/gremlin-python/src/main/jython/gremlin_python/process/graph_traversal.py +++ b/gremlin-python/src/main/jython/gremlin_python/process/graph_traversal.py @@ -22,6 +22,7 @@ from .traversal import TraversalStrategies from .traversal import Bytecode from ..driver.remote_connection import RemoteStrategy from .. import statics +from ..compat import long class GraphTraversalSource(object): def __init__(self, graph, traversal_strategies, bytecode=None): @@ -91,7 +92,7 @@ class GraphTraversal(Traversal): if isinstance(index, int): return self.range(long(index), long(index + 1)) elif isinstance(index, slice): - return self.range(0L if index.start is None else long(index.start), long(sys.maxint) if index.stop is None else long(index.stop)) + return self.range(long(0) if index.start is None else long(index.start), long(sys.maxsize) if index.stop is None else long(index.stop)) else: raise TypeError("Index must be int or slice") def __getattr__(self, key): http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/gremlin_python/process/traversal.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/process/traversal.py b/gremlin-python/src/main/jython/gremlin_python/process/traversal.py index 0302047..338c61b 100644 --- a/gremlin-python/src/main/jython/gremlin_python/process/traversal.py +++ b/gremlin-python/src/main/jython/gremlin_python/process/traversal.py @@ -20,6 +20,8 @@ import abc import six from aenum import Enum from .. import statics +from ..compat import long + class Traversal(object): def __init__(self, graph, traversal_strategies, bytecode): @@ -240,7 +242,9 @@ TRAVERSER ''' class Traverser(object): - def __init__(self, object, bulk=1L): + def __init__(self, object, bulk=None): + if bulk is None: + bulk = long(1) self.object = object self.bulk = bulk def __repr__(self): http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py index fd13ae9..3eece23 100644 --- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py +++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py @@ -22,12 +22,12 @@ __author__ = 'Marko A. Rodriguez (http://markorodriguez.com)' import json from abc import abstractmethod from aenum import Enum -from types import FloatType -from types import FunctionType -from types import IntType -from types import LongType + +import six from gremlin_python import statics +from gremlin_python.compat import ( + FloatType, FunctionType, IntType, LongType, long) from gremlin_python.process.traversal import Binding from gremlin_python.process.traversal import Bytecode from gremlin_python.process.traversal import P @@ -171,7 +171,7 @@ class LambdaSerializer(GraphSONSerializer): if not script.strip().startswith("lambda"): script = "lambda " + script dict["script"] = script - dict["arguments"] = eval(dict["script"]).func_code.co_argcount + dict["arguments"] = six.get_function_code(eval(dict["script"])).co_argcount else: dict["arguments"] = -1 return _SymbolHelper.objectify("Lambda", dict) http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py b/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py index 7855c74..46e2e1f 100644 --- a/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py +++ b/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py @@ -19,10 +19,12 @@ under the License. __author__ = 'Marko A. Rodriguez (http://markorodriguez.com)' +import pytest import unittest from unittest import TestCase from gremlin_python import statics +from gremlin_python.compat import long from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection from gremlin_python.process.traversal import Traverser from gremlin_python.structure.graph import Graph @@ -34,18 +36,17 @@ class TestDriverRemoteConnection(TestCase): statics.load_statics(globals()) connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g') assert "remoteconnection[ws://localhost:8182/gremlin,g]" == str(connection) - # g = Graph().traversal().withRemote(connection) - # - assert 6L == g.V().count().toList()[0] + + assert long(6) == g.V().count().toList()[0] # assert Vertex(1) == g.V(1).next() assert 1 == g.V(1).id().next() assert Traverser(Vertex(1)) == g.V(1).nextTraverser() assert 1 == len(g.V(1).toList()) assert isinstance(g.V(1).toList(), list) - # - results = g.V().repeat(out()).times(2).name.toList() + results = g.V().repeat(out()).times(2).name + results = results.toList() assert 2 == len(results) assert "lop" in results assert "ripple" in results @@ -125,7 +126,7 @@ if __name__ == '__main__': test = True connection.close() except: - print "GremlinServer is not running and this test case will not execute: " + __file__ + print("GremlinServer is not running and this test case will not execute: " + __file__) if test: unittest.main() http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py index cae1a53..fbd8438 100644 --- a/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py +++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py @@ -23,6 +23,9 @@ import json import unittest from unittest import TestCase +import six + +from gremlin_python.compat import long from gremlin_python.structure.graph import Vertex from gremlin_python.structure.graph import Path from gremlin_python.structure.io.graphson import GraphSONReader @@ -43,7 +46,7 @@ class TestGraphSONReader(TestCase): "@value": 31 })) assert isinstance(x, long) - assert 31L == x + assert long(31) == x ## x = GraphSONReader.readObject(json.dumps({ "@type": "g:Float", @@ -73,7 +76,10 @@ class TestGraphSONReader(TestCase): """{"@type":"g:Path","@value":{"labels":[["a"],["b","c"],[]],"objects":[{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":1},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":0},"value":"marko","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":1},"value":{"@type":"g:Int32","@value":29},"label":"age"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":3},"label":"software","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":4},"value":"lop","label":"name"}}],"lang":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":5},"value":"java","label":"lang"}}]}}},"lop"]}}""" ) assert isinstance(path, Path) - assert "[v[1], v[3], u'lop']" == str(path) + if six.PY3: + assert "[v[1], v[3], 'lop']" == str(path) + else: + assert "[v[1], v[3], u'lop']" == str(path) assert Vertex(1) == path[0] assert Vertex(1) == path["a"] assert "lop" == path[2] @@ -82,9 +88,9 @@ class TestGraphSONReader(TestCase): class TestGraphSONWriter(TestCase): def test_numbers(self): - assert """{"@type":"g:Int32","@value":1}""" == GraphSONWriter.writeObject(1) - assert """{"@type":"g:Int64","@value":2}""" == GraphSONWriter.writeObject(2L) - assert """{"@type":"g:Float","@value":3.2}""" == GraphSONWriter.writeObject(3.2) + assert {"@type":"g:Int64","@value":2} == json.loads(GraphSONWriter.writeObject(long(2))) + assert {"@type":"g:Int32","@value":1} == json.loads(GraphSONWriter.writeObject(1)) + assert {"@type":"g:Float","@value":3.2} == json.loads(GraphSONWriter.writeObject(3.2)) assert """true""" == GraphSONWriter.writeObject(True) http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b61f0d82/gremlin-python/src/main/jython/tests/structure/test_graph.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/tests/structure/test_graph.py b/gremlin-python/src/main/jython/tests/structure/test_graph.py index 72b4b79..63d9a1e 100644 --- a/gremlin-python/src/main/jython/tests/structure/test_graph.py +++ b/gremlin-python/src/main/jython/tests/structure/test_graph.py @@ -19,9 +19,11 @@ under the License. __author__ = 'Marko A. Rodriguez (http://markorodriguez.com)' +import sys import unittest from unittest import TestCase +from gremlin_python.compat import long from gremlin_python.structure.graph import Edge from gremlin_python.structure.graph import Property from gremlin_python.structure.graph import Vertex @@ -45,14 +47,14 @@ class TestGraph(TestCase): assert "phrase" == edge.inV.label assert edge.inV != edge.outV # - vertex_property = VertexProperty(24L, "name", "marko") + vertex_property = VertexProperty(long(24), "name", "marko") assert "vp[name->marko]" == str(vertex_property) assert "name" == vertex_property.label assert "name" == vertex_property.key assert "marko" == vertex_property.value - assert 24L == vertex_property.id + assert long(24) == vertex_property.id assert isinstance(vertex_property.id, long) - assert vertex_property == VertexProperty(24L, "name", "marko") + assert vertex_property == VertexProperty(long(24), "name", "marko") # property = Property("age", 29) assert "p[age->29]" == str(property) @@ -60,7 +62,8 @@ class TestGraph(TestCase): assert 29 == property.value assert isinstance(property.value, int) assert property == Property("age", 29) - assert property != Property("age", 29L) + if not sys.version_info > (3,): + assert property != Property("age", long(29)) # for i in [vertex, edge, vertex_property, property]: for j in [vertex, edge, vertex_property, property]: