Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
code-health: remove built-in types wrappers
The built-in `basestring` abstract type was removed in Python 3 [1].
`str` must be used for strings and `bytes` for byte objects. `long` type
was unified with `int` in Python 3 [2].

1. https://docs.python.org/3/whatsnew/3.0.html?highlight=basestring
2. https://peps.python.org/pep-0237/

Part of #212
  • Loading branch information
DifferentialOrange committed Oct 6, 2022
commit b8297028745f018e3d13ea9e26850617a8fe8961
23 changes: 11 additions & 12 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
check_key,
greeting_decode,
version_id,
string_types,
ENCODING_DEFAULT,
)

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

:rtype: `Response` instance
'''
if isinstance(space_name, string_types):
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
request = RequestReplace(self, space_name, values)
return self._send_request(request)
Expand Down Expand Up @@ -697,7 +696,7 @@ class JoinState:
def _ops_process(self, space, update_ops):
new_ops = []
for op in update_ops:
if isinstance(op[1], string_types):
if isinstance(op[1], str):
op = list(op)
op[1] = self.schema.get_field(space, op[1])['id']
new_ops.append(op)
Expand Down Expand Up @@ -733,7 +732,7 @@ def insert(self, space_name, values):

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

key = check_key(key)
if isinstance(space_name, string_types):
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, string_types):
if isinstance(index_name, str):
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 @@ -826,9 +825,9 @@ def upsert(self, space_name, tuple_value, op_list, **kwargs):
'''
index_name = kwargs.get("index", 0)

if isinstance(space_name, string_types):
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, string_types):
if isinstance(index_name, str):
index_name = self.schema.get_index(space_name, index_name).iid
op_list = self._ops_process(space_name, op_list)
request = RequestUpsert(self, space_name, index_name, tuple_value,
Expand Down Expand Up @@ -902,9 +901,9 @@ def update(self, space_name, key, op_list, **kwargs):
index_name = kwargs.get("index", 0)

key = check_key(key)
if isinstance(space_name, string_types):
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, string_types):
if isinstance(index_name, str):
index_name = self.schema.get_index(space_name, index_name).iid
op_list = self._ops_process(space_name, op_list)
request = RequestUpdate(self, space_name, index_name, key, op_list)
Expand Down Expand Up @@ -985,9 +984,9 @@ def select(self, space_name, key=None, **kwargs):
# tuples)
key = check_key(key, select=True)

if isinstance(space_name, string_types):
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if isinstance(index_name, string_types):
if isinstance(index_name, str):
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
13 changes: 4 additions & 9 deletions tarantool/mesh_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
RequestCall
)

try:
string_types = basestring
except NameError:
string_types = str

default_addr_opts = {
'transport': DEFAULT_TRANSPORT,
'ssl_key_file': DEFAULT_SSL_KEY_FILE,
Expand All @@ -55,7 +50,7 @@ def parse_error(uri, msg):

if not uri:
return parse_error(uri, 'should not be None or empty string')
if not isinstance(uri, string_types):
if not isinstance(uri, str):
return parse_error(uri, 'should be of a string type')
if uri.count(':') != 1:
return parse_error(uri, 'does not match host:port scheme')
Expand Down Expand Up @@ -117,7 +112,7 @@ def format_error(address, err):
if 'host' not in result or result['host'] is None:
return format_error(result,
'host is mandatory for an inet result')
if not isinstance(result['host'], string_types):
if not isinstance(result['host'], str):
return format_error(result,
'host must be a string for an inet result')

Expand All @@ -131,7 +126,7 @@ def format_error(address, err):

# Looks okay.
return result, None
elif isinstance(result['port'], string_types):
elif isinstance(result['port'], str):
# Looks like a unix address.

# Expect no host.
Expand All @@ -140,7 +135,7 @@ def format_error(address, err):
result, 'host must be unset or None for a unix result')

# Validate port.
if not isinstance(result['port'], string_types):
if not isinstance(result['port'], str):
return format_error(result,
'port must be a string for a unix result')

Expand Down
3 changes: 1 addition & 2 deletions tarantool/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
from tarantool.response import Response, ResponseExecute
from tarantool.utils import (
strxor,
binary_types
)

from tarantool.msgpack_ext.packer import default as packer_default
Expand Down Expand Up @@ -187,7 +186,7 @@ def sha1(values):
sha = hashlib.sha1()
for i in values:
if i is not None:
if isinstance(i, binary_types):
if isinstance(i, bytes):
sha.update(i)
else:
sha.update(i.encode())
Expand Down
14 changes: 5 additions & 9 deletions tarantool/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
It is a Tarantool schema description.
'''

from tarantool.utils import (
string_types,
integer_types,
)
from tarantool.error import (
Error,
SchemaError,
Expand Down Expand Up @@ -151,7 +147,7 @@ def fetch_space(self, space):
)
elif len(space_row) == 0 or not len(space_row[0]):
# We can't find space with this name or id
temp_name = 'name' if isinstance(space, string_types) else 'id'
temp_name = 'name' if isinstance(space, str) else 'id'
errmsg = "There's no space with {1} '{0}'".format(space, temp_name)
raise SchemaError(errmsg)

Expand All @@ -161,7 +157,7 @@ def fetch_space(self, space):

def fetch_space_from(self, space):
_index = None
if isinstance(space, string_types):
if isinstance(space, str):
_index = const.INDEX_SPACE_NAME
else:
_index = const.INDEX_SPACE_PRIMARY
Expand Down Expand Up @@ -212,7 +208,7 @@ def fetch_index(self, space_object, index):
)
elif len(index_row) == 0 or not len(index_row[0]):
# We can't find index with this name or id
temp_name = 'name' if isinstance(index, string_types) else 'id'
temp_name = 'name' if isinstance(index, str) else 'id'
errmsg = ("There's no index with {2} '{0}'"
" in space '{1}'").format(index, space_object.name,
temp_name)
Expand All @@ -229,7 +225,7 @@ def fetch_index_all(self):

def fetch_index_from(self, space, index):
_index = None
if isinstance(index, string_types):
if isinstance(index, str):
_index = const.INDEX_INDEX_NAME
else:
_index = const.INDEX_INDEX_PRIMARY
Expand Down Expand Up @@ -269,7 +265,7 @@ def get_field(self, space, field):
try:
return _space.format[field]
except:
tp = 'name' if isinstance(field, string_types) else 'id'
tp = 'name' if isinstance(field, str) else 'id'
errmsg = "There's no field with {2} '{0}' in space '{1}'".format(
field, _space.name, tp
)
Expand Down
34 changes: 5 additions & 29 deletions tarantool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,14 @@
import sys
import uuid

# Compatibility layer for Python2/Python3
if sys.version_info.major == 2:
string_types = (basestring, )
integer_types = (int, long)
supported_types = integer_types + string_types + (float,)
supported_types = (int, str, bytes, float,)

ENCODING_DEFAULT = None
ENCODING_DEFAULT = "utf-8"

if sys.version_info.minor < 6:
binary_types = (str, )
else:
binary_types = (bytes, )
from base64 import decodestring as base64_decode
from base64 import decodebytes as base64_decode

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

elif sys.version_info.major == 3:
binary_types = (bytes, )
string_types = (str, )
integer_types = (int, )
supported_types = integer_types + string_types + binary_types + (float,)

ENCODING_DEFAULT = "utf-8"

from base64 import decodebytes as base64_decode

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

else:
pass # unreachable
def strxor(rhs, lhs):
return bytes([x ^ y for x, y in zip(rhs, lhs)])

def check_key(*args, **kwargs):
if 'first' not in kwargs:
Expand Down
4 changes: 2 additions & 2 deletions test/suites/test_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_07_call_16(self):
ans = con.call('fiber.time64')
self.assertEqual(len(ans), 1)
self.assertEqual(len(ans[0]), 1)
self.assertIsInstance(ans[0][0], tarantool.utils.integer_types)
self.assertIsInstance(ans[0][0], int)
ans = con.call('uuid.str')
self.assertEqual(len(ans), 1)
self.assertEqual(len(ans[0]), 1)
Expand All @@ -207,7 +207,7 @@ def test_07_call_17(self):
self.assertIsInstance(ans[0], float)
ans = con.call('fiber.time64')
self.assertEqual(len(ans), 1)
self.assertIsInstance(ans[0], tarantool.utils.integer_types)
self.assertIsInstance(ans[0], int)
ans = con.call('uuid.str')
self.assertEqual(len(ans), 1)
self.assertIsInstance(ans[0], str)
Expand Down