Skip to content

Commit a9800cc

Browse files
authored
trim the Python2+3 compatibility layer (#561)
1 parent 36a04a9 commit a9800cc

5 files changed

Lines changed: 21 additions & 68 deletions

File tree

vertica_python/compat.py

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,15 @@
5151

5252
"""Functions for Python 2 vs. 3 compatibility.
5353
## Conversion routines
54-
In addition to the functions below, `as_str` converts an object to a `str`.
5554
@@as_bytes
56-
@@as_text
57-
@@as_str_any
58-
## Types
59-
The compatibility module also provides the following types:
60-
* `bytes_or_text_types`
61-
* `complex_types`
62-
* `integral_types`
63-
* `real_types`
55+
@@as_str
6456
"""
6557

6658

6759
def as_bytes(bytes_or_text, encoding='utf-8'):
6860
"""Converts either bytes or unicode to `bytes`, using utf-8 encoding for text.
6961
Args:
70-
bytes_or_text: A `bytes`, `bytearray`, `str`, or `unicode` object.
62+
bytes_or_text: A `bytes`, `bytearray`, or `str` object.
7163
encoding: A string indicating the charset for encoding unicode.
7264
Returns:
7365
A `bytes` object.
@@ -76,22 +68,19 @@ def as_bytes(bytes_or_text, encoding='utf-8'):
7668
"""
7769
if isinstance(bytes_or_text, str):
7870
return bytes_or_text.encode(encoding)
79-
elif isinstance(bytes_or_text, bytearray):
71+
elif isinstance(bytes_or_text, (bytes, bytearray)):
8072
return bytes(bytes_or_text)
81-
elif isinstance(bytes_or_text, bytes):
82-
return bytes_or_text
8373
else:
84-
raise TypeError('Expected binary or unicode string, got %r' %
85-
(bytes_or_text,))
74+
raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)
8675

8776

88-
def as_text(bytes_or_text, encoding='utf-8'):
77+
def as_str(bytes_or_text, encoding='utf-8'):
8978
"""Returns the given argument as a unicode string.
9079
Args:
91-
bytes_or_text: A `bytes`, `bytearray`, `str`, or `unicode` object.
80+
bytes_or_text: A `bytes`, `bytearray`, or `str` object.
9281
encoding: A string indicating the charset for decoding unicode.
9382
Returns:
94-
A `unicode` (Python 2) or `str` (Python 3) object.
83+
A `str` object.
9584
Raises:
9685
TypeError: If `bytes_or_text` is not a binary or unicode string.
9786
"""
@@ -102,28 +91,3 @@ def as_text(bytes_or_text, encoding='utf-8'):
10291
else:
10392
raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)
10493

105-
106-
# Convert an object to a `str` in both Python 2 and 3.
107-
as_str = as_text
108-
109-
110-
def as_str_any(value):
111-
"""Converts to `str` as `str(value)`, but use `as_str` for `bytes`.
112-
Args:
113-
value: A object that can be converted to `str`.
114-
Returns:
115-
A `str` object.
116-
"""
117-
if isinstance(value, (bytes, bytearray)):
118-
return as_str(value)
119-
else:
120-
return str(value)
121-
122-
123-
# Either bytes or text.
124-
bytes_or_text_types = (bytes, bytearray, str)
125-
126-
_allowed_symbols = [
127-
'as_str',
128-
'bytes_or_text_types',
129-
]

vertica_python/tests/common/base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import getpass
4444
from configparser import ConfigParser
4545

46-
from ...compat import as_text, as_str, as_bytes
46+
from ...compat import as_str, as_bytes
4747
from ...vertica.log import VerticaLogging
4848

4949

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

130130
# Common assertions
131-
def assertTextEqual(self, first, second, msg=None):
132-
first_text = as_text(first)
133-
second_text = as_text(second)
134-
self.assertEqual(first=first_text, second=second_text, msg=msg)
135-
136131
def assertStrEqual(self, first, second, msg=None):
137132
first_str = as_str(first)
138133
second_str = as_str(second)
@@ -145,7 +140,7 @@ def assertBytesEqual(self, first, second, msg=None):
145140

146141
def assertResultEqual(self, value, result, msg=None):
147142
if isinstance(value, str):
148-
self.assertTextEqual(first=value, second=result, msg=msg)
143+
self.assertStrEqual(first=value, second=result, msg=msg)
149144
else:
150145
self.assertEqual(first=value, second=result, msg=msg)
151146

vertica_python/vertica/column.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from typing import Optional
4242

4343
from ..datatypes import getDisplaySize, getPrecision, getScale
44-
from ..compat import as_str, as_text
4544

4645

4746
# 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:
104103
")")
105104

106105
def __str__(self):
107-
return as_str(str(self.props))
108-
109-
def __unicode__(self):
110-
return as_text(str(self.props))
106+
return str(self.props)
111107

112108
def __repr__(self):
113-
return as_str(str(self.props))
109+
return str(self.props)
114110

115111
def __iter__(self):
116112
for prop in self.props:

vertica_python/vertica/cursor.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
T = TypeVar('T')
6868

6969
from .. import errors, os_utils
70-
from ..compat import as_text
70+
from ..compat import as_str
7171
from ..vertica import messages
7272
from ..vertica.column import Column
7373
from ..vertica.deserializer import Deserializer
@@ -217,7 +217,7 @@ def execute(self, operation: str,
217217

218218
self.flush_to_query_ready()
219219

220-
operation = as_text(operation)
220+
operation = as_str(operation)
221221
self.operation = operation
222222

223223
self.rowcount = -1
@@ -281,7 +281,7 @@ def executemany(self,
281281

282282
self.flush_to_query_ready()
283283

284-
operation = as_text(operation)
284+
operation = as_str(operation)
285285
self.operation = operation
286286

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

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

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

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

652652
if isinstance(py_obj, type(None)):
653653
return '' if is_copy_data and not is_collection else 'NULL'
654654
elif isinstance(py_obj, bool):
655655
return str(py_obj)
656656
elif isinstance(py_obj, (str, bytes)):
657-
return self.format_quote(as_text(py_obj), is_copy_data, is_collection)
657+
return self.format_quote(as_str(py_obj), is_copy_data, is_collection)
658658
elif isinstance(py_obj, (int, Decimal)):
659659
return str(py_obj)
660660
elif isinstance(py_obj, float):
@@ -700,7 +700,7 @@ def object_to_string(self, py_obj: Any, is_copy_data: bool, is_collection: bool
700700
# Use the ROW keyword to construct a row value
701701
return f'ROW({",".join(elements)})'
702702
elif isinstance(py_obj, (datetime.datetime, datetime.date, datetime.time, UUID)):
703-
return self.format_quote(as_text(str(py_obj)), is_copy_data, is_collection)
703+
return self.format_quote(str(py_obj), is_copy_data, is_collection)
704704
else:
705705
if is_copy_data:
706706
return str(py_obj)
@@ -721,7 +721,6 @@ def format_operation_with_parameters(self, operation: str, parameters: Union[Lis
721721
for key, param in parameters.items():
722722
if not isinstance(key, str):
723723
key = str(key)
724-
key = as_text(key)
725724

726725
value = self.object_to_string(param, is_copy_data)
727726

vertica_python/vertica/messages/frontend_messages/password.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
from __future__ import annotations
3737

38-
import os
3938
import hashlib
4039
from struct import pack
4140

0 commit comments

Comments
 (0)