From 3c5ac78999517331f0f52816a0a9261a922d5d7c Mon Sep 17 00:00:00 2001 From: Domenico Lupinetti Date: Sat, 10 Oct 2015 19:16:13 +0200 Subject: [PATCH 1/2] Fix bug on test_serialization for csv escape --- pyorient/types.py | 10 +++++- pyorient/utils.py | 2 ++ tests/test_serializations.py | 64 ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/pyorient/types.py b/pyorient/types.py index e513cf9c..c7508e27 100644 --- a/pyorient/types.py +++ b/pyorient/types.py @@ -23,6 +23,14 @@ def __str__(self): rep = rep + ",'rid':'" + str(self.__rid) + "'" return '{' + rep + '}' + @staticmethod + def addslashes(string): + l = [ "\\", '"', "'", "\0", ] + for i in l: + if i in string: + string = string.replace( i, '\\' + i ) + return string + def __init__(self, content=None): self.__rid = None @@ -45,7 +53,7 @@ def __init__(self, content=None): # { '@my_class': { 'accommodation': 'hotel' } } self.__o_class = key[1:] for _key, _value in content[key].items(): - self.__o_storage[_key] = _value + self.__o_storage[_key] = self.addslashes( _value ) elif key == '__o_storage': self.__o_storage = content[key] else: diff --git a/pyorient/utils.py b/pyorient/utils.py index 099852b1..2b5b49f3 100644 --- a/pyorient/utils.py +++ b/pyorient/utils.py @@ -75,6 +75,8 @@ def parse_cluster_id(cluster_id): cluster_id = cluster_id.decode("utf-8") elif isinstance( cluster_id, OrientRecordLink ): cluster_id = cluster_id.get() + elif sys.version_info[0] < 3 and isinstance( cluster_id, unicode ): + cluster_id = cluster_id.encode('utf-8') _cluster_id, _position = cluster_id.split( ':' ) if _cluster_id[0] is '#': diff --git a/tests/test_serializations.py b/tests/test_serializations.py index 74923a96..0e851bb4 100644 --- a/tests/test_serializations.py +++ b/tests/test_serializations.py @@ -46,3 +46,67 @@ def test_csv_encoding(self): assert 'specie:"rodent"' in raw # TODO: add several more complex tests to have more coverage + + def test_csv_escape(self): + import pyorient + DB = pyorient.OrientDB("localhost", 2424) + DB.connect("root", "root") + + db_name = "test_escape" + try: + DB.db_drop(db_name) + except pyorient.PyOrientCommandException as e: + print(e) + finally: + db = DB.db_create(db_name, pyorient.DB_TYPE_GRAPH, + pyorient.STORAGE_TYPE_MEMORY) + pass + + cluster_info = DB.db_open( + db_name, "admin", "admin", pyorient.DB_TYPE_GRAPH, "" + ) + + cluster_id = DB.command( "CREATE CLASS MyModel EXTENDS V" )[0] + + data0 = {'key': '"""'} + DB.record_create( cluster_id, {'@MyModel': data0} ) + + data1 = {'key': "'''"} + DB.record_create( cluster_id, {'@MyModel': data1} ) + + data2 = {'key': '\\'} + DB.record_create( cluster_id, {'@MyModel': data2} ) + + data3 = {'key': '\0'} + DB.record_create( cluster_id, {'@MyModel': data3} ) + + data4 = {'key': '""'} + DB.record_create( cluster_id, {'@MyModel': data4} ) + + data5 = {'key': '\'\'""\0 \\ execution'} + DB.record_create( cluster_id, {'@MyModel': data5} ) + + rec0 = DB.record_load( "#" + cluster_id.decode() + ":0" ) + assert rec0._class == "MyModel" + assert rec0.oRecordData == data0 + + rec1 = DB.record_load( "#" + cluster_id.decode('utf-8') + ":1" ) + assert rec1._class == "MyModel" + assert rec1.oRecordData == data1 + + rec2 = DB.record_load( "#" + cluster_id.decode('utf-8') + ":2") + assert rec2._class == "MyModel" + assert rec2.oRecordData == data2 + + rec3 = DB.record_load( "#" + cluster_id.decode('utf-8') + ":3" ) + assert rec3._class == "MyModel" + assert rec3.oRecordData == data3 + + rec4 = DB.record_load( "#" + cluster_id.decode('utf-8') + ":4" ) + assert rec4._class == "MyModel" + assert rec4.oRecordData == data4 + + rec5 = DB.record_load( "#" + cluster_id.decode('utf-8') + ":5" ) + assert rec5._class == "MyModel" + assert rec5.oRecordData == data5 + From eb40386e45985c90eee563ab3c46b594003999f3 Mon Sep 17 00:00:00 2001 From: Domenico Lupinetti Date: Sat, 10 Oct 2015 20:29:53 +0200 Subject: [PATCH 2/2] sending large messages (over 65535 bytes) fails #108 --- pyorient/orient.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyorient/orient.py b/pyorient/orient.py index d21a01ef..d98f5f6a 100644 --- a/pyorient/orient.py +++ b/pyorient/orient.py @@ -92,7 +92,10 @@ def close(self): self.connected = False def write(self, buff): - return self._socket.send(buff) + count = 0 + while count < len(buff): + count += self._socket.send(buff[count:]) + return count # The man page for recv says: The receive calls normally return # any data available, up to the requested amount, rather than waiting