Skip to content

Commit

Permalink
Remove six compatibility library
Browse files Browse the repository at this point in the history
This library is necessary only for Python 2 support, and now that we
have dropped support for 2.7, we don't need it anymore.

Signed-off-by: Gus Monod <[email protected]>
  • Loading branch information
sarahmonod committed Nov 20, 2023
1 parent e100fa1 commit dae3858
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 70 deletions.
2 changes: 1 addition & 1 deletion comdb2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""This package provides Python interfaces to Comdb2 databases.
Two different Python submodules are provided for interacting with Comdb2
databases. Both submodules work from Python 2.7+ and from Python 3.5+.
databases. Both submodules work from Python 3.6.
`comdb2.dbapi2` provides an interface that conforms to `the Python Database API
Specification v2.0 <https://www.python.org/dev/peps/pep-0249/>`_. If you're
Expand Down
11 changes: 5 additions & 6 deletions comdb2/_ccdb2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ from cpython.mem cimport PyMem_Malloc, PyMem_Free
import datetime

from pytz import timezone, UTC
import six

from ._cdb2_types import Error, Effects, DatetimeUs
from . cimport _cdb2api as lib
Expand Down Expand Up @@ -113,7 +112,7 @@ cdef class _ParameterValue(object):
self.size = 0
self.data = NULL
return
elif isinstance(obj, six.integer_types):
elif isinstance(obj, int):
self.type = lib.CDB2_INTEGER
self.owner = None
self.size = sizeof(long long)
Expand Down Expand Up @@ -167,7 +166,7 @@ cdef class _ParameterValue(object):
param_name,
exc_desc,
)
six.raise_from(Error(lib.CDB2ERR_CONV_FAIL, errmsg), exc)
raise Error(lib.CDB2ERR_CONV_FAIL, errmsg) from exc
else:
errmsg = "Can't map %s value %r for parameter '%s' to a Comdb2 type" % (
type(obj).__name__,
Expand Down Expand Up @@ -248,7 +247,7 @@ cdef _column_value(lib.cdb2_hndl_tp *hndl, int col):

if exc is not None:
errmsg += _describe_exception(exc)
six.raise_from(Error(lib.CDB2ERR_CONV_FAIL, errmsg), exc)
raise Error(lib.CDB2ERR_CONV_FAIL, errmsg) from exc
else:
errmsg += "Unsupported column type"
raise Error(lib.CDB2ERR_NOTSUPPORTED, errmsg)
Expand Down Expand Up @@ -289,7 +288,7 @@ cdef class _Cursor(object):
ret = self.row_class(ret)
except Exception as e:
errmsg = "Instantiating row failed: " + _describe_exception(e)
six.raise_from(Error(lib.CDB2ERR_UNKNOWN, errmsg), e)
raise Error(lib.CDB2ERR_UNKNOWN, errmsg) from e
return ret


Expand Down Expand Up @@ -393,7 +392,7 @@ cdef class Handle(object):
row_class = self._row_factory(self.column_names())
except Exception as e:
errmsg = "row_factory call failed: " + _describe_exception(e)
six.raise_from(Error(lib.CDB2ERR_UNKNOWN, errmsg), e)
raise Error(lib.CDB2ERR_UNKNOWN, errmsg) from e

cdef _Cursor ret = _Cursor.__new__(_Cursor)
ret.handle = self
Expand Down
1 change: 0 additions & 1 deletion comdb2/_cdb2_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from collections import namedtuple
from datetime import datetime
import six

__name__ = 'comdb2.cdb2'

Expand Down
4 changes: 2 additions & 2 deletions comdb2/cdb2.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
NULL ``None``
integer `int`
real `float`
blob `six.binary_type` (aka `bytes` in Python 3, ``str`` in Python 2)
text `six.text_type` (aka `str` in Python 3, ``unicode`` in Python 2)
blob `bytes`
text `str`
datetime `datetime.datetime`
datetimeus `DatetimeUs`
============ ================================================================
Expand Down
12 changes: 4 additions & 8 deletions comdb2/dbapi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
NULL ``None``
integer `int`
real `float`
blob `six.binary_type` (aka `bytes` in Python 3, ``str`` in Python 2)
text `six.text_type` (aka `str` in Python 3, ``unicode`` in Python 2)
blob `bytes`
text `str`
datetime `datetime.datetime`
datetimeus `DatetimeUs`
============ ================================================================
Expand Down Expand Up @@ -245,7 +245,6 @@
import weakref
import datetime
import re
import six

from . import cdb2

Expand Down Expand Up @@ -297,7 +296,7 @@
\s* # then skip over any whitespace
(\w+) # and capture the first word
""",
re.VERBOSE | re.DOTALL | (0 if six.PY2 else re.ASCII),
re.VERBOSE | re.DOTALL | re.ASCII,
)
_VALID_SP_NAME = re.compile(r'^[A-Za-z0-9_.]+$')

Expand Down Expand Up @@ -349,10 +348,7 @@ def _binary(string):
TimestampFromTicks = Timestamp.fromtimestamp
TimestampUsFromTicks = TimestampUs.fromtimestamp

try:
UserException = StandardError # Python 2
except NameError:
UserException = Exception # Python 3
UserException = Exception


class Error(UserException):
Expand Down
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@


# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'python': ('https://docs.python.org/3', None),
'six': ('https://six.readthedocs.io', None)}
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}

# Don't highlight the code for the Python standard library datetime module.
viewcode_import = False
Expand Down
6 changes: 3 additions & 3 deletions docs/dbapi2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ them if you so choose, but you are not required to.

Creates an object suitable for binding as a BLOB parameter.

If the input argument was a `six.text_type` Unicode string, it is
encoded as a UTF-8 byte string and returned. Otherwise, the input
argument is passed to the `bytes` constructor, and the result returned.
If the input argument was a `str` object, it is encoded as a UTF-8 byte
string and returned. Otherwise, the input argument is passed to the
`bytes` constructor, and the result returned.

:param string: A string from which the new object is constructed
:rtype: `bytes`
Expand Down
14 changes: 5 additions & 9 deletions docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
Best Practices, Tips, and Tricks
********************************

#. This package uses `six.text_type` for text and `bytes` for blobs; this may
be surprising if you're using `str` in Python 2. See :doc:`types` for an
explanation of what Python types must be used when binding parameters, and
what Python types will be returned for result columns.
#. This package uses `str` for text and `bytes` for blobs. See :doc:`types` for
an explanation of what Python types must be used when binding parameters,
and what Python types will be returned for result columns.

An error message saying "incompatible values from SQL blob of length 3 to
bcstring field 'foo'" most likely means that you passed a byte string where
Expand All @@ -24,11 +23,8 @@ Best Practices, Tips, and Tricks
a ``WHERE`` clause that you expected to match some rows will silently fail
to match any.

#. Prefer using Python 3 rather than Python 2 when possible. The above type
mappings are much more intuitive in Python 3 than in Python 2. If you can't
use Python 3 but are writing a new module, prefer to use ``from __future__
import unicode_literals`` to opt into forward compatible unicode string
literals (rather than the default, string literals as byte strings).
#. The latest version of this package only supports Python 3. If you can't
use Python 3, make sure to use version 1.4.1 or earlier.

#. The database can time out connections that have been idle for a period of
time, and each idle connection uses some amount of resources on the database
Expand Down
46 changes: 11 additions & 35 deletions docs/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ SQL type Python type
NULL ``None``
integer `int`
real `float`
blob `six.binary_type` (aka `bytes` in Python 3, ``str`` in Python 2)
text `six.text_type` (aka `str` in Python 3, ``unicode`` in Python 2)
blob `bytes`
text `str`
datetime `datetime.datetime`
datetimeus `~.cdb2.DatetimeUs`
intervalym not supported
Expand Down Expand Up @@ -47,58 +47,34 @@ represent TEXT columns. This was chosen for maximum forward compatibility with
Python 3, and to make it easier to write code that will work identically in
both languages. This decision has many important ramifications.

#. In Python 2, ``'foo'`` is the wrong type for binding a ``cstring`` column.
You instead need to use a Unicode literal like ``u'foo'``.
Alternately, you can make all string literals in your module be Unicode
literals by default by doing::
#. If you have a variable of type `bytes` and you want to pass it to the
database as a TEXT value, you need to convert it to a `str` using
`~bytes.decode`.

from __future__ import unicode_literals

.. note::
It's not safe to blindly do this in existing code as it changes that
code's behavior, and might cause problems.

#. If you have a variable of type `six.binary_type` (byte string) and you
want to pass it to the database as a TEXT value, you need to convert it to
a `six.text_type` (Unicode) string using `~bytes.decode`. In Python 2,
unless ``unicode_literals`` was imported from `__future__`, you will need to
do this with every string variable that you want to use as a cstring
column. For example::

value = 'foo'
params = {'col': value.decode('utf-8')}
# dbapi2
cursor.execute("select * from tbl where col=%(col)s", params)
# cdb2
handle.execute("select * from tbl where col=@col", params)

#. TEXT columns are returned to you as `six.text_type` (Unicode) strings.
If you need to pass them to a library that expects `six.binary_type` (byte)
strings, you have to use `~str.encode`, like this::
#. TEXT columns are returned to you as `str` strings. If you need to pass them
to a library that expects `bytes` strings, you have to use `~str.encode`,
like this::

cursor.execute("select col from tbl")
for row in cursor:
col = row[0]
library_func(col.encode('utf-8'))

This may come up a lot in Python 2, where historically libraries were
written to expect byte strings rather than Unicode strings.

#. When a TEXT column is read back from the database, it is decoded as UTF-8.
If your database has non-UTF-8 text in a ``cstring`` column, you need to
cast that column from a TEXT to a BLOB in SQL when reading it out, like
this::

cursor.execute("select cast(col as blob) from tbl")

That will result in you receiving a byte string rather than a Unicode
string for that result column.
That will result in you receiving a `bytes` object rather than `str` for
that result column.

.. note::
ASCII is a subset of UTF-8, so this is not required for if the column
contains plain ASCII text.

#. Likewise, when a Unicode string is sent to the database, it is encoded as
#. Likewise, when a `str` object is sent to the database, it is encoded as
UTF-8. If you need to store non-UTF-8 text in a ``cstring`` column in your
database, you need to bind a byte string in Python land, and then cast the
value from BLOB to TEXT in SQL. For instance, to bind a latin1 string
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def make_static_extension(name, **kwargs):
author='Alex Chamberlain',
author_email='[email protected]',
packages=['comdb2'],
install_requires=["six", "pytz"],
install_requires=["pytz"],
extras_require={"tests": ["python-dateutil>=2.6.0", "pytest"]},
python_requires=">=3.6",
ext_modules=[ccdb2],
Expand Down
1 change: 0 additions & 1 deletion tests/test_cdb2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from comdb2.factories import dict_row_factory
from comdb2.factories import namedtuple_row_factory
import pytest
import six

COLUMN_LIST = ("short_col u_short_col int_col u_int_col longlong_col"
" float_col double_col byte_col byte_array_col"
Expand Down
1 change: 0 additions & 1 deletion tests/test_dbapi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import pytest
import datetime
import pytz
import six
from functools import partial

try:
Expand Down

0 comments on commit dae3858

Please sign in to comment.