Skip to content

Cleanup Python 2 compatibility wrappers #231

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 7 commits into from
Oct 10, 2022
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
2 changes: 0 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Tarantool python client library documentation build configuration file, created by
# sphinx-quickstart on Tue Nov 29 06:29:57 2011.
#
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import os
import re
Expand Down
1 change: 0 additions & 1 deletion tarantool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614

import sys
Expand Down
29 changes: 12 additions & 17 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614
'''
This module provides low-level API for Tarantool
Expand All @@ -18,10 +17,7 @@

import ctypes
import ctypes.util
try:
from ctypes import c_ssize_t
except ImportError:
from ctypes import c_longlong as c_ssize_t
from ctypes import c_ssize_t

import msgpack

Expand Down Expand Up @@ -85,7 +81,6 @@
check_key,
greeting_decode,
version_id,
string_types,
ENCODING_DEFAULT,
)

Expand Down Expand Up @@ -640,7 +635,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 +692,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 +728,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 +749,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 +821,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 +897,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 +980,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
2 changes: 0 additions & 2 deletions tarantool/connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import abc
import itertools
import queue
Expand Down
1 change: 0 additions & 1 deletion tarantool/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614

IPROTO_CODE = 0x00
Expand Down
1 change: 0 additions & 1 deletion tarantool/dbapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from tarantool.connection import Connection as BaseConnection
from tarantool.error import *

Expand Down
23 changes: 7 additions & 16 deletions tarantool/error.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614
'''
Python DB API compatible exceptions
http://www.python.org/dev/peps/pep-0249/

The PEP-249 says that database related exceptions must be inherited as follows:

StandardError
Exception
|__Warning
|__Error
|__InterfaceError
Expand All @@ -30,21 +29,13 @@
import warnings


try:
class Warning(StandardError):
'''Exception raised for important warnings
like data truncations while inserting, etc. '''
except NameError:
class Warning(Exception):
'''Exception raised for important warnings
like data truncations while inserting, etc. '''

try:
class Error(StandardError):
'''Base class for error exceptions'''
except NameError:
class Error(Exception):
'''Base class for error exceptions'''
class Warning(Exception):
'''Exception raised for important warnings
like data truncations while inserting, etc. '''

class Error(Exception):
'''Base class for error exceptions'''


class InterfaceError(Error):
Expand Down
14 changes: 4 additions & 10 deletions tarantool/mesh_connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
'''
This module provides the MeshConnection class with automatic switch
between Tarantool instances by the basic round-robin strategy.
Expand Down Expand Up @@ -33,11 +32,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 +49,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 +111,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 +125,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 +134,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
19 changes: 5 additions & 14 deletions tarantool/request.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614
'''
Request types definitions
'''

import sys
import collections
import msgpack
import hashlib

try:
collectionsAbc = collections.abc
except AttributeError:
collectionsAbc = collections
from collections.abc import Sequence, Mapping


from tarantool.error import DatabaseError
Expand Down Expand Up @@ -56,7 +51,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 @@ -87,9 +81,6 @@ def __init__(self, conn):
# The option controls whether to pack binary (non-unicode)
# string values as mp_bin or as mp_str.
#
# The default behaviour of the Python 2 connector is to pack
# both bytes and Unicode strings as mp_str.
#
# The default behaviour of the Python 3 connector (since
# default encoding is "utf-8") is to pack bytes as mp_bin
# and Unicode strings as mp_str. encoding=None mode must
Expand Down Expand Up @@ -119,7 +110,7 @@ def __init__(self, conn):
# just always set it for all msgpack versions to get rid
# of the warning on msgpack-0.5.0 and to keep our
# behaviour on msgpack-1.0.0.
if conn.encoding is None or sys.version_info.major == 2:
if conn.encoding is None:
packer_kwargs['use_bin_type'] = False
else:
packer_kwargs['use_bin_type'] = True
Expand Down Expand Up @@ -187,7 +178,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 Expand Up @@ -408,9 +399,9 @@ class RequestExecute(Request):

def __init__(self, conn, sql, args):
super(RequestExecute, self).__init__(conn)
if isinstance(args, collectionsAbc.Mapping):
if isinstance(args, Mapping):
args = [{":%s" % name: value} for name, value in args.items()]
elif not isinstance(args, collectionsAbc.Sequence):
elif not isinstance(args, Sequence):
raise TypeError("Parameter type '%s' is not supported. "
"Must be a mapping or sequence" % type(args))

Expand Down
8 changes: 1 addition & 7 deletions tarantool/response.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614

try:
# Python 3.3+
from collections.abc import Sequence
except ImportError:
# Python 2
from collections import Sequence
from collections.abc import Sequence

import json
import msgpack
Expand Down
15 changes: 5 additions & 10 deletions tarantool/schema.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# -*- coding: utf-8 -*-
# pylint: disable=R0903
'''
This module provides the :class:`~tarantool.schema.Schema` class.
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 +146,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 +156,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 +207,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 +224,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 +264,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
1 change: 0 additions & 1 deletion tarantool/space.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# pylint: disable=C0301,W0105,W0401,W0614
'''
This module provides the :class:`~tarantool.space.Space` class.
Expand Down
Loading