Skip to content

Commit 9160c58

Browse files
committed
Merge pull request #62 from shveenkov/master
add encoding feature for py3 tarantool.connect
2 parents 5d0786a + 4dcb815 commit 9160c58

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

tarantool/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from tarantool.const import (
88
SOCKET_TIMEOUT,
99
RECONNECT_MAX_ATTEMPTS,
10-
RECONNECT_DELAY
10+
RECONNECT_DELAY,
11+
ENCODING_DEFAULT
1112
)
1213

1314
from tarantool.error import (
@@ -24,7 +25,7 @@
2425
)
2526

2627

27-
def connect(host="localhost", port=33013, user=None, password=None):
28+
def connect(host="localhost", port=33013, user=None, password=None, encoding=ENCODING_DEFAULT):
2829
'''\
2930
Create a connection to the Tarantool server.
3031
@@ -42,7 +43,9 @@ def connect(host="localhost", port=33013, user=None, password=None):
4243
socket_timeout=SOCKET_TIMEOUT,
4344
reconnect_max_attempts=RECONNECT_MAX_ATTEMPTS,
4445
reconnect_delay=RECONNECT_DELAY,
45-
connect_now=True)
46+
connect_now=True,
47+
encoding=encoding)
48+
4649

4750
__all__ = ['connect', 'Connection', 'Schema', 'Error', 'DatabaseError',
4851
'NetworkError', 'NetworkWarning', 'RetryWarning', 'SchemaError']

tarantool/connection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@
4343
RETRY_MAX_ATTEMPTS,
4444
REQUEST_TYPE_OK,
4545
REQUEST_TYPE_ERROR,
46-
IPROTO_GREETING_SIZE)
46+
IPROTO_GREETING_SIZE,
47+
ENCODING_DEFAULT)
48+
4749
from tarantool.error import (
4850
NetworkError,
4951
DatabaseError,
5052
warn,
5153
RetryWarning,
5254
NetworkWarning)
55+
5356
from tarantool.schema import Schema
5457
from tarantool.utils import check_key
5558

@@ -75,7 +78,8 @@ def __init__(self, host, port,
7578
socket_timeout=SOCKET_TIMEOUT,
7679
reconnect_max_attempts=RECONNECT_MAX_ATTEMPTS,
7780
reconnect_delay=RECONNECT_DELAY,
78-
connect_now=True):
81+
connect_now=True,
82+
encoding=ENCODING_DEFAULT):
7983
'''
8084
Initialize a connection to the server.
8185
@@ -101,6 +105,7 @@ def __init__(self, host, port,
101105
self._socket = None
102106
self.connected = False
103107
self.error = True
108+
self.encoding = encoding
104109
if connect_now:
105110
self.connect()
106111

tarantool/const.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
# pylint: disable=C0301,W0105,W0401,W0614
33

4+
import six
5+
46
IPROTO_CODE = 0x00
57
IPROTO_SYNC = 0x01
68
IPROTO_SPACE_ID = 0x10
@@ -69,3 +71,8 @@
6971
# Number of reattempts in case of server
7072
# return completion_status == 1 (try again)
7173
RETRY_MAX_ATTEMPTS = 10
74+
75+
if six.PY2:
76+
ENCODING_DEFAULT = None
77+
else:
78+
ENCODING_DEFAULT = "utf-8"

tarantool/response.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# pylint: disable=C0301,W0105,W0401,W0614
33

4-
import six
54
import sys
65
import msgpack
76
import yaml
@@ -42,10 +41,10 @@ def __init__(self, conn, response):
4241
# created in the __new__(). But let it be.
4342
super(Response, self).__init__()
4443

45-
if six.PY2:
46-
unpacker = msgpack.Unpacker(use_list=True)
44+
if conn.encoding is not None:
45+
unpacker = msgpack.Unpacker(use_list=True, encoding=conn.encoding)
4746
else:
48-
unpacker = msgpack.Unpacker(use_list=True, encoding="utf-8")
47+
unpacker = msgpack.Unpacker(use_list=True)
4948

5049
unpacker.feed(response)
5150
header = unpacker.unpack()

tarantool/schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class SchemaIndex(object):
1515
def __init__(self, array, space):
1616
self.iid = array[1]
1717
self.name = array[2]
18+
if isinstance(self.name, bytes):
19+
self.name = self.name.decode()
1820
self.index = array[3]
1921
self.unique = array[4]
2022
self.parts = []
@@ -36,6 +38,8 @@ def __init__(self, array, schema):
3638
self.sid = array[0]
3739
self.arity = array[1]
3840
self.name = array[2]
41+
if isinstance(self.name, bytes):
42+
self.name = self.name.decode()
3943
self.indexes = {}
4044
self.schema = schema
4145
self.schema[self.sid] = self

0 commit comments

Comments
 (0)