From 613536920ac26b15a1be5a9759f38d2ac8ce4851 Mon Sep 17 00:00:00 2001 From: shveenkov Date: Wed, 23 Sep 2015 22:46:02 +0300 Subject: [PATCH 1/2] add encoding for tnt database --- tarantool/__init__.py | 9 ++++++--- tarantool/connection.py | 9 +++++++-- tarantool/const.py | 7 +++++++ tarantool/response.py | 7 +++---- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/tarantool/__init__.py b/tarantool/__init__.py index b05f3681..a2be5ebb 100644 --- a/tarantool/__init__.py +++ b/tarantool/__init__.py @@ -7,7 +7,8 @@ from tarantool.const import ( SOCKET_TIMEOUT, RECONNECT_MAX_ATTEMPTS, - RECONNECT_DELAY + RECONNECT_DELAY, + ENCODING_DEFAULT ) from tarantool.error import ( @@ -24,7 +25,7 @@ ) -def connect(host="localhost", port=33013, user=None, password=None): +def connect(host="localhost", port=33013, user=None, password=None, encoding=ENCODING_DEFAULT): '''\ Create a connection to the Tarantool server. @@ -42,7 +43,9 @@ def connect(host="localhost", port=33013, user=None, password=None): socket_timeout=SOCKET_TIMEOUT, reconnect_max_attempts=RECONNECT_MAX_ATTEMPTS, reconnect_delay=RECONNECT_DELAY, - connect_now=True) + connect_now=True, + encoding=encoding) + __all__ = ['connect', 'Connection', 'Schema', 'Error', 'DatabaseError', 'NetworkError', 'NetworkWarning', 'RetryWarning', 'SchemaError'] diff --git a/tarantool/connection.py b/tarantool/connection.py index 9625276a..d216bbb6 100644 --- a/tarantool/connection.py +++ b/tarantool/connection.py @@ -43,13 +43,16 @@ RETRY_MAX_ATTEMPTS, REQUEST_TYPE_OK, REQUEST_TYPE_ERROR, - IPROTO_GREETING_SIZE) + IPROTO_GREETING_SIZE, + ENCODING_DEFAULT) + from tarantool.error import ( NetworkError, DatabaseError, warn, RetryWarning, NetworkWarning) + from tarantool.schema import Schema from tarantool.utils import check_key @@ -75,7 +78,8 @@ def __init__(self, host, port, socket_timeout=SOCKET_TIMEOUT, reconnect_max_attempts=RECONNECT_MAX_ATTEMPTS, reconnect_delay=RECONNECT_DELAY, - connect_now=True): + connect_now=True, + encoding=ENCODING_DEFAULT): ''' Initialize a connection to the server. @@ -101,6 +105,7 @@ def __init__(self, host, port, self._socket = None self.connected = False self.error = True + self.encoding = encoding if connect_now: self.connect() diff --git a/tarantool/const.py b/tarantool/const.py index 7cbdf41c..0229a35f 100644 --- a/tarantool/const.py +++ b/tarantool/const.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # pylint: disable=C0301,W0105,W0401,W0614 +import six + IPROTO_CODE = 0x00 IPROTO_SYNC = 0x01 IPROTO_SPACE_ID = 0x10 @@ -69,3 +71,8 @@ # Number of reattempts in case of server # return completion_status == 1 (try again) RETRY_MAX_ATTEMPTS = 10 + +if six.PY2: + ENCODING_DEFAULT = None +else: + ENCODING_DEFAULT = "utf-8" diff --git a/tarantool/response.py b/tarantool/response.py index ca9548ec..e9f43288 100644 --- a/tarantool/response.py +++ b/tarantool/response.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=C0301,W0105,W0401,W0614 -import six import sys import msgpack import yaml @@ -42,10 +41,10 @@ def __init__(self, conn, response): # created in the __new__(). But let it be. super(Response, self).__init__() - if six.PY2: - unpacker = msgpack.Unpacker(use_list=True) + if conn.encoding is not None: + unpacker = msgpack.Unpacker(use_list=True, encoding=conn.encoding) else: - unpacker = msgpack.Unpacker(use_list=True, encoding="utf-8") + unpacker = msgpack.Unpacker(use_list=True) unpacker.feed(response) header = unpacker.unpack() From 4dcb81586464fde0f133ec84e0202665eb9d0389 Mon Sep 17 00:00:00 2001 From: shveenkov Date: Thu, 24 Sep 2015 01:19:21 +0300 Subject: [PATCH 2/2] fix for encoding=None --- tarantool/schema.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tarantool/schema.py b/tarantool/schema.py index 5046ed31..b4ef8ac9 100644 --- a/tarantool/schema.py +++ b/tarantool/schema.py @@ -15,6 +15,8 @@ class SchemaIndex(object): def __init__(self, array, space): self.iid = array[1] self.name = array[2] + if isinstance(self.name, bytes): + self.name = self.name.decode() self.index = array[3] self.unique = array[4] self.parts = [] @@ -36,6 +38,8 @@ def __init__(self, array, schema): self.sid = array[0] self.arity = array[1] self.name = array[2] + if isinstance(self.name, bytes): + self.name = self.name.decode() self.indexes = {} self.schema = schema self.schema[self.sid] = self