From a9800ccb5db2c1b664fa87778d299242e33ca472 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sat, 10 Aug 2024 14:34:54 +0200 Subject: [PATCH] trim the Python2+3 compatibility layer (#561) --- vertica_python/compat.py | 50 +++---------------- vertica_python/tests/common/base.py | 9 +--- vertica_python/vertica/column.py | 8 +-- vertica_python/vertica/cursor.py | 21 ++++---- .../messages/frontend_messages/password.py | 1 - 5 files changed, 21 insertions(+), 68 deletions(-) diff --git a/vertica_python/compat.py b/vertica_python/compat.py index 353e2342..4c562f85 100644 --- a/vertica_python/compat.py +++ b/vertica_python/compat.py @@ -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. @@ -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. """ @@ -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', -] diff --git a/vertica_python/tests/common/base.py b/vertica_python/tests/common/base.py index b0e28197..c4b02feb 100644 --- a/vertica_python/tests/common/base.py +++ b/vertica_python/tests/common/base.py @@ -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 @@ -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) @@ -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) diff --git a/vertica_python/vertica/column.py b/vertica_python/vertica/column.py index 24be6e75..e5d824fa 100644 --- a/vertica_python/vertica/column.py +++ b/vertica_python/vertica/column.py @@ -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. @@ -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: diff --git a/vertica_python/vertica/cursor.py b/vertica_python/vertica/cursor.py index 8de24b71..9ce23e7c 100644 --- a/vertica_python/vertica/cursor.py +++ b/vertica_python/vertica/cursor.py @@ -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 @@ -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 @@ -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'] @@ -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] @@ -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(): @@ -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): @@ -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) @@ -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) diff --git a/vertica_python/vertica/messages/frontend_messages/password.py b/vertica_python/vertica/messages/frontend_messages/password.py index d4750995..f48d2684 100644 --- a/vertica_python/vertica/messages/frontend_messages/password.py +++ b/vertica_python/vertica/messages/frontend_messages/password.py @@ -35,7 +35,6 @@ from __future__ import annotations -import os import hashlib from struct import pack