Skip to content

Commit

Permalink
trim the Python2+3 compatibility layer (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-detiste authored Aug 10, 2024
1 parent 36a04a9 commit a9800cc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 68 deletions.
50 changes: 7 additions & 43 deletions vertica_python/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,15 @@

"""Functions for Python 2 vs. 3 compatibility.
## Conversion routines
In addition to the functions below, `as_str` converts an object to a `str`.
@@as_bytes
@@as_text
@@as_str_any
## Types
The compatibility module also provides the following types:
* `bytes_or_text_types`
* `complex_types`
* `integral_types`
* `real_types`
@@as_str
"""


def as_bytes(bytes_or_text, encoding='utf-8'):
"""Converts either bytes or unicode to `bytes`, using utf-8 encoding for text.
Args:
bytes_or_text: A `bytes`, `bytearray`, `str`, or `unicode` object.
bytes_or_text: A `bytes`, `bytearray`, or `str` object.
encoding: A string indicating the charset for encoding unicode.
Returns:
A `bytes` object.
Expand All @@ -76,22 +68,19 @@ def as_bytes(bytes_or_text, encoding='utf-8'):
"""
if isinstance(bytes_or_text, str):
return bytes_or_text.encode(encoding)
elif isinstance(bytes_or_text, bytearray):
elif isinstance(bytes_or_text, (bytes, bytearray)):
return bytes(bytes_or_text)
elif isinstance(bytes_or_text, bytes):
return bytes_or_text
else:
raise TypeError('Expected binary or unicode string, got %r' %
(bytes_or_text,))
raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)


def as_text(bytes_or_text, encoding='utf-8'):
def as_str(bytes_or_text, encoding='utf-8'):
"""Returns the given argument as a unicode string.
Args:
bytes_or_text: A `bytes`, `bytearray`, `str`, or `unicode` object.
bytes_or_text: A `bytes`, `bytearray`, or `str` object.
encoding: A string indicating the charset for decoding unicode.
Returns:
A `unicode` (Python 2) or `str` (Python 3) object.
A `str` object.
Raises:
TypeError: If `bytes_or_text` is not a binary or unicode string.
"""
Expand All @@ -102,28 +91,3 @@ def as_text(bytes_or_text, encoding='utf-8'):
else:
raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)


# Convert an object to a `str` in both Python 2 and 3.
as_str = as_text


def as_str_any(value):
"""Converts to `str` as `str(value)`, but use `as_str` for `bytes`.
Args:
value: A object that can be converted to `str`.
Returns:
A `str` object.
"""
if isinstance(value, (bytes, bytearray)):
return as_str(value)
else:
return str(value)


# Either bytes or text.
bytes_or_text_types = (bytes, bytearray, str)

_allowed_symbols = [
'as_str',
'bytes_or_text_types',
]
9 changes: 2 additions & 7 deletions vertica_python/tests/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import getpass
from configparser import ConfigParser

from ...compat import as_text, as_str, as_bytes
from ...compat import as_str, as_bytes
from ...vertica.log import VerticaLogging


Expand Down Expand Up @@ -128,11 +128,6 @@ def tearDown(self):
self.logger.info('\n'+'-'*10+' End '+self.__class__.__name__+"."+self._testMethodName+' '+'-'*10+'\n')

# Common assertions
def assertTextEqual(self, first, second, msg=None):
first_text = as_text(first)
second_text = as_text(second)
self.assertEqual(first=first_text, second=second_text, msg=msg)

def assertStrEqual(self, first, second, msg=None):
first_str = as_str(first)
second_str = as_str(second)
Expand All @@ -145,7 +140,7 @@ def assertBytesEqual(self, first, second, msg=None):

def assertResultEqual(self, value, result, msg=None):
if isinstance(value, str):
self.assertTextEqual(first=value, second=result, msg=msg)
self.assertStrEqual(first=value, second=result, msg=msg)
else:
self.assertEqual(first=value, second=result, msg=msg)

Expand Down
8 changes: 2 additions & 6 deletions vertica_python/vertica/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from typing import Optional

from ..datatypes import getDisplaySize, getPrecision, getScale
from ..compat import as_str, as_text


# Data of a particular SQL data type might be transmitted in either "text" format or "binary" format.
Expand Down Expand Up @@ -104,13 +103,10 @@ def debug_info(self) -> str:
")")

def __str__(self):
return as_str(str(self.props))

def __unicode__(self):
return as_text(str(self.props))
return str(self.props)

def __repr__(self):
return as_str(str(self.props))
return str(self.props)

def __iter__(self):
for prop in self.props:
Expand Down
21 changes: 10 additions & 11 deletions vertica_python/vertica/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
T = TypeVar('T')

from .. import errors, os_utils
from ..compat import as_text
from ..compat import as_str
from ..vertica import messages
from ..vertica.column import Column
from ..vertica.deserializer import Deserializer
Expand Down Expand Up @@ -217,7 +217,7 @@ def execute(self, operation: str,

self.flush_to_query_ready()

operation = as_text(operation)
operation = as_str(operation)
self.operation = operation

self.rowcount = -1
Expand Down Expand Up @@ -281,7 +281,7 @@ def executemany(self,

self.flush_to_query_ready()

operation = as_text(operation)
operation = as_str(operation)
self.operation = operation

use_prepared = bool(self.connection.options['use_prepared_statements']
Expand All @@ -308,12 +308,12 @@ def executemany(self,
#################################################################
m = self._insert_statement.match(operation)
if m:
target = as_text(m.group('target'))
target = as_str(m.group('target'))

variables = as_text(m.group('variables'))
variables = as_str(m.group('variables'))
variables = ",".join([variable.strip().strip('"') for variable in variables.split(",")])

values = as_text(m.group('values'))
values = as_str(m.group('values'))
values = "|".join([value.strip().strip('"') for value in values.split(",")])
seq_of_values = [self.format_operation_with_parameters(values, parameters, is_copy_data=True)
for parameters in seq_of_parameters]
Expand Down Expand Up @@ -474,7 +474,7 @@ def copy(self, sql: str, data: Union[IO[AnyStr], bytes, str], **kwargs: Any) ->
>> fs, buffer_size=65536)
```
"""
sql = as_text(sql)
sql = as_str(sql)
self.operation = sql

if self.closed():
Expand Down Expand Up @@ -647,14 +647,14 @@ def object_to_string(self, py_obj: Any, is_copy_data: bool, is_collection: bool
if not isinstance(result, (str, bytes)):
raise TypeError("Unexpected return type of {} adapter: {}, expected a string type."
.format(type(py_obj), type(result)))
return as_text(result)
return as_str(result)

if isinstance(py_obj, type(None)):
return '' if is_copy_data and not is_collection else 'NULL'
elif isinstance(py_obj, bool):
return str(py_obj)
elif isinstance(py_obj, (str, bytes)):
return self.format_quote(as_text(py_obj), is_copy_data, is_collection)
return self.format_quote(as_str(py_obj), is_copy_data, is_collection)
elif isinstance(py_obj, (int, Decimal)):
return str(py_obj)
elif isinstance(py_obj, float):
Expand Down Expand Up @@ -700,7 +700,7 @@ def object_to_string(self, py_obj: Any, is_copy_data: bool, is_collection: bool
# Use the ROW keyword to construct a row value
return f'ROW({",".join(elements)})'
elif isinstance(py_obj, (datetime.datetime, datetime.date, datetime.time, UUID)):
return self.format_quote(as_text(str(py_obj)), is_copy_data, is_collection)
return self.format_quote(str(py_obj), is_copy_data, is_collection)
else:
if is_copy_data:
return str(py_obj)
Expand All @@ -721,7 +721,6 @@ def format_operation_with_parameters(self, operation: str, parameters: Union[Lis
for key, param in parameters.items():
if not isinstance(key, str):
key = str(key)
key = as_text(key)

value = self.object_to_string(param, is_copy_data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

from __future__ import annotations

import os
import hashlib
from struct import pack

Expand Down

0 comments on commit a9800cc

Please # to comment.