Skip to content

Commit

Permalink
Remove dependency on six
Browse files Browse the repository at this point in the history
  • Loading branch information
denisenkom committed Nov 12, 2023
1 parent 2d6bfda commit 575a936
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 42 deletions.
1 change: 0 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
six>=1.4.1
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
six>=1.4.1
12 changes: 5 additions & 7 deletions src/pytds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import keyword
import os
import re
import six
import socket
import uuid
import warnings
import weakref
import logging

from six.moves import xrange
from collections.abc import Iterator

from pytds.tds_types import NVarCharType
from . import lcid
Expand Down Expand Up @@ -379,7 +377,7 @@ def _open(self, sock=None):
last_error = None
end_time = time.time() + connect_timeout
while True:
for _ in xrange(len(login.servers)):
for _ in range(len(login.servers)):
try:
self._try_open(timeout=retry_time, sock=sock)
return
Expand Down Expand Up @@ -519,7 +517,7 @@ def _try_activate_cursor(self, cursor):
self._active_cursor = cursor


class Cursor(six.Iterator):
class Cursor(Iterator):
"""
This class represents a database cursor, which is used to issue queries
and fetch results from a database connection.
Expand Down Expand Up @@ -693,7 +691,7 @@ def _ensure_transaction(self):

def _execute(self, operation, params):
self._ensure_transaction()
operation = six.text_type(operation)
operation = str(operation)
if params:
named_params = {}
if isinstance(params, (list, tuple)):
Expand Down Expand Up @@ -930,7 +928,7 @@ def fetchmany(self, size=None):
size = self.arraysize

rows = []
for _ in xrange(size):
for _ in range(size):
row = self.fetchone()
if not row:
break
Expand Down
1 change: 0 additions & 1 deletion src/pytds/smp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import threading
import socket
import errno
from six.moves import range
try:
from bitarray import bitarray
except ImportError:
Expand Down
3 changes: 1 addition & 2 deletions src/pytds/sspi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import six
import logging

from ctypes import c_ulong, c_ushort, c_void_p, c_ulonglong, POINTER,\
Expand Down Expand Up @@ -346,7 +345,7 @@ def query_user_name(self):
byref(self._handle),
SECPKG_CRED_ATTR_NAMES,
byref(names))
user_name = six.text_type(names.UserName)
user_name = str(names.UserName)
finally:
p = c_wchar_p.from_buffer(names, SecPkgCredentials_Names.UserName.offset)
sec_fn.FreeContextBuffer(p)
Expand Down
3 changes: 1 addition & 2 deletions src/pytds/tds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import datetime
import warnings

import six
import socket
import struct

Expand Down Expand Up @@ -941,7 +940,7 @@ def make_param(self, name, value) -> tds_base.Param:

if isinstance(value, output):
param_flags |= tds_base.fByRefValue
if isinstance(value.type, six.string_types):
if isinstance(value.type, str):
param_type = tds_types.sql_type_by_declaration(value.type)
elif value.type:
param_type = self.conn.type_inferrer.from_class(value.type)
Expand Down
8 changes: 3 additions & 5 deletions src/pytds/tds_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import socket
import sys

import six

# tds protocol versions
TDS70 = 0x70000000
TDS71 = 0x71000000
Expand Down Expand Up @@ -362,10 +360,10 @@ def force_unicode(s):
return s.decode('utf8')
except UnicodeDecodeError as e:
raise DatabaseError(e)
elif isinstance(s, six.text_type):
elif isinstance(s, str):
return s
else:
return six.text_type(s)
return str(s)


def tds_quote_id(ident):
Expand Down Expand Up @@ -714,7 +712,7 @@ def __repr__(self):
val = self.value
if isinstance(val, bytes) and len(self.value) > 100:
val = self.value[:100] + b'... len is ' + str(len(val)).encode('ascii')
if isinstance(val, six.text_type) and len(self.value) > 100:
if isinstance(val, str) and len(self.value) > 100:
val = self.value[:100] + '... len is ' + str(len(val))
return '<Column(name={},type={},value={},flags={},user_type={},codec={})>'.format(
repr(self.column_name),
Expand Down
13 changes: 6 additions & 7 deletions src/pytds/tds_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import struct
import re
import uuid
import six
import functools
from io import StringIO, BytesIO

Expand Down Expand Up @@ -681,7 +680,7 @@ def write(self, w, val):
else:
if w._tds._tds._login.bytes_to_unicode:
val = tds_base.force_unicode(val)
if isinstance(val, six.text_type):
if isinstance(val, str):
val, _ = self._codec.encode(val)
w.put_smallint(len(val))
w.write(val)
Expand Down Expand Up @@ -733,7 +732,7 @@ def write(self, w, val):
else:
if w._tds._tds._login.bytes_to_unicode:
val = tds_base.force_unicode(val)
if isinstance(val, six.text_type):
if isinstance(val, str):
val, _ = self._codec.encode(val)

# Putting the actual length here causes an error when bulk inserting:
Expand Down Expand Up @@ -951,7 +950,7 @@ def write(self, w, val):
else:
if w._tds._tds._login.bytes_to_unicode:
val = tds_base.force_unicode(val)
if isinstance(val, six.text_type):
if isinstance(val, str):
val, _ = self._codec.encode(val)
w.put_int(len(val))
w.write(val)
Expand Down Expand Up @@ -2637,7 +2636,7 @@ def _from_class_value(self, value, value_type):

if issubclass(value_type, bool):
return BitType()
elif issubclass(value_type, six.integer_types):
elif issubclass(value_type, int):
if value is None:
return IntType()
if -2 ** 31 <= value <= 2 ** 31 - 1:
Expand All @@ -2655,12 +2654,12 @@ def _from_class_value(self, value, value_type):
return VarBinaryType(size=8000)
else:
return type_factory.long_binary_type()
elif issubclass(value_type, six.binary_type):
elif issubclass(value_type, bytes):
if bytes_to_unicode:
return type_factory.long_string_type()
else:
return type_factory.long_varchar_type()
elif issubclass(value_type, six.string_types):
elif issubclass(value_type, str):
return type_factory.long_string_type()
elif issubclass(value_type, datetime.datetime):
if value and value.tzinfo and allow_tz:
Expand Down
23 changes: 11 additions & 12 deletions tests/all_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import random
import string
import codecs
from six import StringIO, BytesIO
import logging
import socket
from io import StringIO

import utils

from pytds.tds_types import TimeType, DateTime2Type, DateType, DateTimeOffsetType, BitType, TinyIntType, SmallIntType, \
Expand All @@ -32,8 +33,6 @@
tzoffset = pytds.tz.FixedOffsetTimezone
utc = pytds.tz.utc
import pytds.extensions
import six
from six.moves import xrange
from pytds import (
connect, ProgrammingError, TimeoutError, Time,
Error, IntegrityError, Timestamp, DataError, Date, Binary,
Expand Down Expand Up @@ -253,7 +252,7 @@ def setUp(self):
#def test_mars_sessions_recycle_ids(self):
# if not self.conn.mars_enabled:
# self.skipTest('Only relevant to mars')
# for _ in xrange(2 ** 16 + 1):
# for _ in range(2 ** 16 + 1):
# cur = self.conn.cursor()
# cur.close()

Expand Down Expand Up @@ -318,7 +317,7 @@ def kill(conn, spid):
@unittest.skipUnless(LIVE_TEST, "requires HOST variable to be set")
class ConnectionClosing(unittest.TestCase):
def test_open_close(self):
for x in xrange(3):
for x in range(3):
kwargs = settings.CONNECT_KWARGS.copy()
kwargs['database'] = 'master'
connect(**kwargs).close()
Expand Down Expand Up @@ -1015,18 +1014,18 @@ def setUp(self):
def test_fetch(self):
cur = self.conn.cursor()

self.assertIsInstance(cur.execute_scalar("select cast('abc' as nvarchar(max))"), six.text_type)
self.assertIsInstance(cur.execute_scalar("select cast('abc' as varchar(max))"), six.binary_type)
self.assertIsInstance(cur.execute_scalar("select cast('abc' as text)"), six.binary_type)
self.assertIsInstance(cur.execute_scalar("select cast('abc' as nvarchar(max))"), str)
self.assertIsInstance(cur.execute_scalar("select cast('abc' as varchar(max))"), bytes)
self.assertIsInstance(cur.execute_scalar("select cast('abc' as text)"), bytes)

self.assertIsInstance(cur.execute_scalar("select %s", ['abc']), six.text_type)
self.assertIsInstance(cur.execute_scalar("select %s", [b'abc']), six.binary_type)
self.assertIsInstance(cur.execute_scalar("select %s", ['abc']), str)
self.assertIsInstance(cur.execute_scalar("select %s", [b'abc']), bytes)

rawBytes = six.b('\x01\x02\x03')
rawBytes = b'\x01\x02\x03'
self.assertEqual(rawBytes, cur.execute_scalar("select cast(0x010203 as varchar(max))"))
self.assertEqual(rawBytes, cur.execute_scalar("select %s", [rawBytes]))

utf8char = six.b('\xee\xb4\xba')
utf8char = b'\xee\xb4\xba'
self.assertEqual(utf8char, cur.execute_scalar("select %s", [utf8char]))


Expand Down
4 changes: 2 additions & 2 deletions tests/connected_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import logging
import random
import string
from io import BytesIO, StringIO

import pytest
import six
import os
from six import StringIO, BytesIO
import pytds
import pytds.extensions
import pytds.login
Expand Down
3 changes: 1 addition & 2 deletions tests/types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import uuid

import pytest
import six

import pytds

Expand Down Expand Up @@ -96,7 +95,7 @@ def test_money(cursor):

def test_strs(cursor):
cur = cursor
assert isinstance(cur.execute_scalar("select 'test'"), six.text_type)
assert isinstance(cur.execute_scalar("select 'test'"), str)


@pytest.mark.parametrize('val', [u'hello',
Expand Down

0 comments on commit 575a936

Please sign in to comment.