Skip to content

For python3.4 six #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
msgpack-python>=0.4.0
pyyaml>=3.10
six
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@ def find_version(*file_paths):
command_options=command_options,
install_requires=[
'msgpack-python>=0.4',
'six',
'PyYAML>=3.10',
]
)
21 changes: 11 additions & 10 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
This module provides low-level API for Tarantool
'''

import six
import time
import errno
import ctypes
Expand Down Expand Up @@ -154,7 +155,7 @@ def connect(self):
raise NetworkError(e)

def _recv(self, to_read):
buf = ''
buf = b""
while to_read > 0:
try:
tmp = self._socket.recv(to_read)
Expand Down Expand Up @@ -188,7 +189,7 @@ def _send_request_wo_reconnect(self, request):

# Repeat request in a loop if the server returns completion_status == 1
# (try again)
for attempt in xrange(RETRY_MAX_ATTEMPTS): # pylint: disable=W0612
for attempt in range(RETRY_MAX_ATTEMPTS): # pylint: disable=W0612
self._socket.sendall(bytes(request))
response = Response(self, self._read_response())

Expand Down Expand Up @@ -328,7 +329,7 @@ def replace(self, space_name, values):

:rtype: `Response` instance
'''
if isinstance(space_name, basestring):
if isinstance(space_name, six.string_types):
space_name = self.schema.get_space(space_name).sid
request = RequestReplace(self, space_name, values)
return self._send_request(request)
Expand Down Expand Up @@ -377,7 +378,7 @@ def insert(self, space_name, values):

:rtype: `Response` instance
'''
if isinstance(space_name, basestring):
if isinstance(space_name, six.string_types):
space_name = self.schema.get_space(space_name).sid
request = RequestInsert(self, space_name, values)
return self._send_request(request)
Expand All @@ -397,9 +398,9 @@ def delete(self, space_name, key, **kwargs):
index_name = kwargs.get("index", 0)

key = check_key(key)
if isinstance(space_name, basestring):
if isinstance(space_name, six.string_types):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, basestring):
if isinstance(index_name, six.string_types):
index_name = self.schema.get_index(space_name, index_name).iid
request = RequestDelete(self, space_name, index_name, key)
return self._send_request(request)
Expand Down Expand Up @@ -427,9 +428,9 @@ def update(self, space_name, key, op_list, **kwargs):
index_name = kwargs.get("index", 0)

key = check_key(key)
if isinstance(space_name, basestring):
if isinstance(space_name, six.string_types):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, basestring):
if isinstance(index_name, six.string_types):
index_name = self.schema.get_index(space_name, index_name).iid
request = RequestUpdate(self, space_name, index_name, key, op_list)
return self._send_request(request)
Expand Down Expand Up @@ -503,9 +504,9 @@ def select(self, space_name, key=None, **kwargs):
# tuples)
key = check_key(key, select=True)

if isinstance(space_name, basestring):
if isinstance(space_name, six.string_types):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, basestring):
if isinstance(index_name, six.string_types):
index_name = self.schema.get_index(space_name, index_name).iid
request = RequestSelect(self, space_name, index_name, key, offset,
limit, iterator_type)
Expand Down
10 changes: 6 additions & 4 deletions tarantool/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import sys
import warnings


class Error(StandardError):

'''Base class for error exceptions'''
try:
class Error(StandardError):
'''Base class for error exceptions'''
except NameError:
class Error(Exception):
'''Base class for error exceptions'''


class DatabaseError(Error):
Expand Down
11 changes: 8 additions & 3 deletions tarantool/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Request types definitions
'''

import six
import msgpack
import hashlib

Expand Down Expand Up @@ -105,14 +106,18 @@ def __init__(self, conn, salt, user, password):
super(RequestAuthenticate, self).__init__(conn)

def sha1(values):
sha1 = hashlib.sha1()
sha = hashlib.sha1()
for i in values:
sha1.update(i)
return sha1.digest()
if i is not None:
sha.update(i if isinstance(i, six.binary_type) else i.encode())
return sha.digest()

def strxor(rhs, lhs):
if six.PY2:
return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(rhs, lhs))

return bytes([x ^ y for x, y in zip(rhs, lhs)])

hash1 = sha1((password,))
hash2 = sha1((hash1,))
scramble = sha1((salt, hash2))
Expand Down
7 changes: 6 additions & 1 deletion tarantool/response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614

import six
import sys
import msgpack
import yaml
Expand Down Expand Up @@ -41,7 +42,11 @@ def __init__(self, conn, response):
# created in the __new__(). But let it be.
super(Response, self).__init__()

unpacker = msgpack.Unpacker(use_list=True)
if six.PY2:
unpacker = msgpack.Unpacker(use_list=True)
else:
unpacker = msgpack.Unpacker(use_list=True, encoding="utf-8")

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

Expand Down
12 changes: 7 additions & 5 deletions tarantool/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
It is a Tarantool schema description.
'''

import six

from tarantool.error import SchemaError
import tarantool.const as const

Expand All @@ -16,7 +18,7 @@ def __init__(self, array, space):
self.index = array[3]
self.unique = array[4]
self.parts = []
for i in xrange(array[5]):
for i in range(array[5]):
self.parts.append((array[5 + 1 + i * 2], array[5 + 2 + i * 2]))
self.space = space
self.space.indexes[self.iid] = self
Expand Down Expand Up @@ -57,14 +59,14 @@ def get_space(self, space):
except KeyError:
pass
_index = (const.INDEX_SPACE_NAME
if isinstance(space, basestring)
if isinstance(space, six.string_types)
else const.INDEX_SPACE_PRIMARY)

array = self.con.select(const.SPACE_SPACE, space, index=_index)
if len(array) > 1:
raise SchemaError('Some strange output from server: \n' + array)
elif len(array) == 0 or not len(array[0]):
temp_name = ('name' if isinstance(space, basestring) else 'id')
temp_name = ('name' if isinstance(space, six.string_types) else 'id')
raise SchemaError(
"There's no space with {1} '{0}'".format(space, temp_name))
array = array[0]
Expand All @@ -77,15 +79,15 @@ def get_index(self, space, index):
except KeyError:
pass
_index = (const.INDEX_INDEX_NAME
if isinstance(index, basestring)
if isinstance(index, six.string_types)
else const.INDEX_INDEX_PRIMARY)

array = self.con.select(const.SPACE_INDEX, [_space.sid, index],
index=_index)
if len(array) > 1:
raise SchemaError('Some strange output from server: \n' + array)
elif len(array) == 0 or not len(array[0]):
temp_name = ('name' if isinstance(index, basestring) else 'id')
temp_name = ('name' if isinstance(index, six.string_types) else 'id')
raise SchemaError(
"There's no index with {2} '{0}' in space '{1}'".format(
index, _space.name, temp_name))
Expand Down
6 changes: 5 additions & 1 deletion tarantool/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# -*- coding: utf-8 -*-

import six

def check_key(*args, **kwargs):
if 'first' not in kwargs:
kwargs['first'] = True
Expand All @@ -12,5 +16,5 @@ def check_key(*args, **kwargs):
elif args[0] is None and kwargs['select']:
return []
for key in args:
assert isinstance(key, (int, long, basestring))
assert isinstance(key, six.integer_types + six.string_types)
return list(args)
23 changes: 16 additions & 7 deletions tests/suites/lib/tarantool_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-

import os
import os.path
import errno
import shlex
import random
Expand Down Expand Up @@ -39,6 +42,7 @@ def __init__(self, host, port):
self.port = port
self.is_connected = False
self.socket = None

def connect(self):
self.socket = socket.create_connection((self.host, self.port))
self.socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
Expand Down Expand Up @@ -87,7 +91,7 @@ def execute_no_reconnect(self, command):
if not command:
return
cmd = command.replace('\n', ' ') + '\n'
self.socket.sendall(cmd)
self.socket.sendall(cmd.encode())

bufsiz = 4096
res = ""
Expand All @@ -96,7 +100,7 @@ def execute_no_reconnect(self, command):
buf = self.socket.recv(bufsiz)
if not buf:
break
res = res + buf
res = res + buf.decode()
if (res.rfind("\n...\n") >= 0 or res.rfind("\r\n...\r\n") >= 0):
break

Expand Down Expand Up @@ -134,6 +138,7 @@ def script_dst(self):
def script(self):
if not hasattr(self, '_script'): self._script = None
return self._script

@script.setter
def script(self, val):
if val is None:
Expand All @@ -153,6 +158,7 @@ def _admin(self):
if not hasattr(self, 'admin'):
self.admin = None
return self.admin

@_admin.setter
def _admin(self, port):
try:
Expand All @@ -168,6 +174,7 @@ def log_des(self):
if not hasattr(self, '_log_des'):
self._log_des = open(self.logfile_path, 'a')
return self._log_des

@log_des.deleter
def log_des(self):
if not hasattr(self, '_log_des'):
Expand All @@ -193,7 +200,7 @@ def find_exe(self):
exe = os.path.join(_dir, self.default_tarantool["bin"])
if os.access(exe, os.X_OK):
return os.path.abspath(exe)
raise RuntimeError("Can't find server executable in " + path)
raise RuntimeError("Can't find server executable in " + os.environ["PATH"])

def generate_configuration(self):
os.putenv("PRIMARY_PORT", str(self.args['primary']))
Expand Down Expand Up @@ -240,7 +247,7 @@ def start(self):
self.generate_configuration()
if self.script:
shutil.copy(self.script, self.script_dst)
os.chmod(self.script_dst, 0777)
os.chmod(self.script_dst, 0o777)
args = self.prepare_args()
self.process = subprocess.Popen(args,
cwd = self.vardir,
Expand All @@ -249,15 +256,17 @@ def start(self):
self.wait_until_started()

def stop(self):
self.process.terminate()
self.process.wait()
if self.process.poll() is None:
self.process.terminate()
self.process.wait()

def restart(self):
self.stop()
self.start()

def clean(self):
shutil.rmtree(self.vardir)
if os.path.isdir(self.vardir):
shutil.rmtree(self.vardir)

def __del__(self):
self.stop()
Expand Down
10 changes: 6 additions & 4 deletions tests/suites/test_dml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-

import six
import yaml
import unittest
import tarantool
Expand All @@ -7,8 +9,8 @@
class Request(unittest.TestCase):
@classmethod
def setUpClass(self):
print ' DML '.center(70, '=')
print '-' * 70
print(' DML '.center(70, '='))
print('-' * 70)
self.srv = TarantoolServer()
self.srv.script = 'tests/suites/box.lua'
self.srv.start()
Expand All @@ -32,7 +34,7 @@ def test_00_01_space_created(self):

def test_00_02_fill_space(self):
# Fill space with values
for i in xrange(1, 500):
for i in range(1, 500):
self.assertEqual(
self.con.insert('space_1', [i, i%5, 'tuple_'+str(i)])[0],
[i, i%5, 'tuple_'+str(i)]
Expand Down Expand Up @@ -145,7 +147,7 @@ def test_07_call(self):
ans = self.con.call('fiber.time64')
self.assertEqual(len(ans), 1)
self.assertEqual(len(ans[0]), 1)
self.assertIsInstance(ans[0][0], (int, long))
self.assertIsInstance(ans[0][0], six.integer_types)
ans = self.con.call('uuid.str')
self.assertEqual(len(ans), 1)
self.assertEqual(len(ans[0]), 1)
Expand Down
4 changes: 2 additions & 2 deletions tests/suites/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class TestSuite_Schema(unittest.TestCase):
@classmethod
def setUpClass(self):
print ' SCHEMA '.center(70, '=')
print '-' * 70
print(' SCHEMA '.center(70, '='))
print('-' * 70)
self.srv = TarantoolServer()
self.srv.script = 'tests/suites/box.lua'
self.srv.start()
Expand Down