Skip to content

Commit

Permalink
Remove some remnants of Python2 (#1172)
Browse files Browse the repository at this point in the history
Co-authored-by: vgali7 <vgali7@gatech.edu>
  • Loading branch information
Lorak-mmk and vgali7 authored Oct 27, 2023
1 parent e9136f4 commit d0e639b
Show file tree
Hide file tree
Showing 109 changed files with 410 additions and 984 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ tests/unit/cython/bytesio_testhelper.c
#iPython
*.ipynb

venv
docs/venv
.eggs
1 change: 0 additions & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ To protect the community, all contributors are required to `sign the DataStax Co

Design and Implementation Guidelines
------------------------------------
- We support Python 2.7+, so any changes must work in any of these runtimes (we use ``six``, ``futures``, and some internal backports for compatability)
- We have integrations (notably Cassandra cqlsh) that require pure Python and minimal external dependencies. We try to avoid new external dependencies. Where compiled extensions are concerned, there should always be a pure Python fallback implementation.
- This project follows `semantic versioning <http://semver.org/>`_, so breaking API changes will only be introduced in major versions.
- Legacy ``cqlengine`` has varying degrees of overreaching client-side validation. Going forward, we will avoid client validation where server feedback is adequate and not overly expensive.
Expand Down
5 changes: 1 addition & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
environment:
matrix:
- PYTHON: "C:\\Python27-x64"
cassandra_version: 3.11.2
ci_type: standard
- PYTHON: "C:\\Python35-x64"
- PYTHON: "C:\\Python37-x64"
cassandra_version: 3.11.2
ci_type: standard
os: Visual Studio 2015
Expand Down
1 change: 0 additions & 1 deletion benchmarks/callback_full_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from threading import Event

from base import benchmark, BenchmarkThread
from six.moves import range

log = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/future_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import logging
from base import benchmark, BenchmarkThread
from six.moves import queue
import queue

log = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/future_full_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import logging
from base import benchmark, BenchmarkThread
from six.moves import queue
import queue

log = logging.getLogger(__name__)

Expand Down
1 change: 0 additions & 1 deletion benchmarks/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

from base import benchmark, BenchmarkThread
from six.moves import range


class Runner(BenchmarkThread):
Expand Down
16 changes: 7 additions & 9 deletions cassandra/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
except ImportError:
SASLClient = None

import six

log = logging.getLogger(__name__)

# Custom payload keys related to DSE Unified Auth
Expand Down Expand Up @@ -270,15 +268,15 @@ def __init__(self, username, password):
self.password = password

def get_mechanism(self):
return six.b("PLAIN")
return b"PLAIN"

def get_initial_challenge(self):
return six.b("PLAIN-START")
return b"PLAIN-START"

def evaluate_challenge(self, challenge):
if challenge == six.b('PLAIN-START'):
if challenge == b'PLAIN-START':
data = "\x00%s\x00%s" % (self.username, self.password)
return data if six.PY2 else data.encode()
return data.encode()
raise Exception('Did not receive a valid challenge response from server')


Expand All @@ -297,13 +295,13 @@ def __init__(self, host, service, qops, properties):
self.sasl = SASLClient(host, service, 'GSSAPI', qops=qops, **properties)

def get_mechanism(self):
return six.b("GSSAPI")
return b"GSSAPI"

def get_initial_challenge(self):
return six.b("GSSAPI-START")
return b"GSSAPI-START"

def evaluate_challenge(self, challenge):
if challenge == six.b('GSSAPI-START'):
if challenge == b'GSSAPI-START':
return self.sasl.process()
else:
return self.sasl.process(challenge)
33 changes: 13 additions & 20 deletions cassandra/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import atexit
from binascii import hexlify
from collections import defaultdict
from collections.abc import Mapping
from concurrent.futures import ThreadPoolExecutor, FIRST_COMPLETED, wait as wait_futures
from copy import copy
from functools import partial, wraps
Expand All @@ -29,8 +30,8 @@
import logging
from warnings import warn
from random import random
import six
from six.moves import filter, range, queue as Queue
import re
import queue
import socket
import sys
import time
Expand Down Expand Up @@ -79,7 +80,6 @@
HostTargetingStatement)
from cassandra.marshal import int64_pack
from cassandra.timestamps import MonotonicTimestampGenerator
from cassandra.compat import Mapping
from cassandra.util import _resolve_contact_points_to_string_map, Version

from cassandra.datastax.insights.reporter import MonitorReporter
Expand Down Expand Up @@ -111,9 +111,6 @@
except ImportError:
from cassandra.util import WeakSet # NOQA

if six.PY3:
long = int

def _is_eventlet_monkey_patched():
if 'eventlet.patcher' not in sys.modules:
return False
Expand Down Expand Up @@ -1158,7 +1155,7 @@ def __init__(self,
else:
self._contact_points_explicit = True

if isinstance(contact_points, six.string_types):
if isinstance(contact_points, str):
raise TypeError("contact_points should not be a string, it should be a sequence (e.g. list) of strings")

if None in contact_points:
Expand Down Expand Up @@ -1793,8 +1790,8 @@ def _new_session(self, keyspace):
return session

def _session_register_user_types(self, session):
for keyspace, type_map in six.iteritems(self._user_types):
for udt_name, klass in six.iteritems(type_map):
for keyspace, type_map in self._user_types.items():
for udt_name, klass in type_map.items():
session.user_type_registered(keyspace, udt_name, klass)

def _cleanup_failed_on_up_handling(self, host):
Expand Down Expand Up @@ -2683,7 +2680,7 @@ def execute_async(self, query, parameters=None, trace=False, custom_payload=None
"""
custom_payload = custom_payload if custom_payload else {}
if execute_as:
custom_payload[_proxy_execute_key] = six.b(execute_as)
custom_payload[_proxy_execute_key] = execute_as.encode()

future = self._create_response_future(
query, parameters, trace, custom_payload, timeout,
Expand Down Expand Up @@ -2747,8 +2744,8 @@ def execute_graph_async(self, query, parameters=None, trace=False, execution_pro

custom_payload = execution_profile.graph_options.get_options_map()
if execute_as:
custom_payload[_proxy_execute_key] = six.b(execute_as)
custom_payload[_request_timeout_key] = int64_pack(long(execution_profile.request_timeout * 1000))
custom_payload[_proxy_execute_key] = execute_as.encode()
custom_payload[_request_timeout_key] = int64_pack(int(execution_profile.request_timeout * 1000))

future = self._create_response_future(query, parameters=None, trace=trace, custom_payload=custom_payload,
timeout=_NOT_SET, execution_profile=execution_profile)
Expand Down Expand Up @@ -2885,7 +2882,7 @@ def _create_response_future(self, query, parameters, trace, custom_payload,

prepared_statement = None

if isinstance(query, six.string_types):
if isinstance(query, str):
query = SimpleStatement(query)
elif isinstance(query, PreparedStatement):
query = query.bind(parameters)
Expand Down Expand Up @@ -3353,10 +3350,6 @@ def user_type_registered(self, keyspace, user_type, klass):
'User type %s does not exist in keyspace %s' % (user_type, keyspace))

field_names = type_meta.field_names
if six.PY2:
# go from unicode to string to avoid decode errors from implicit
# decode when formatting non-ascii values
field_names = [fn.encode('utf-8') for fn in field_names]

def encode(val):
return '{ %s }' % ' , '.join('%s : %s' % (
Expand Down Expand Up @@ -4035,7 +4028,7 @@ def _get_schema_mismatches(self, peers_result, local_result, local_address):
log.debug("[control connection] Schemas match")
return None

return dict((version, list(nodes)) for version, nodes in six.iteritems(versions))
return dict((version, list(nodes)) for version, nodes in versions.items())

def _get_peers_query(self, peers_query_type, connection=None):
"""
Expand Down Expand Up @@ -4155,7 +4148,7 @@ class _Scheduler(Thread):
is_shutdown = False

def __init__(self, executor):
self._queue = Queue.PriorityQueue()
self._queue = queue.PriorityQueue()
self._scheduled_tasks = set()
self._count = count()
self._executor = executor
Expand Down Expand Up @@ -4213,7 +4206,7 @@ def run(self):
else:
self._queue.put_nowait((run_at, i, task))
break
except Queue.Empty:
except queue.Empty:
pass

time.sleep(0.1)
Expand Down
20 changes: 0 additions & 20 deletions cassandra/compat.py

This file was deleted.

26 changes: 6 additions & 20 deletions cassandra/concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from collections import namedtuple
from heapq import heappush, heappop
from itertools import cycle
import six
from six.moves import xrange, zip
from threading import Condition
import sys

Expand Down Expand Up @@ -119,7 +117,7 @@ def execute(self, concurrency, fail_fast):
self._current = 0
self._exec_count = 0
with self._condition:
for n in xrange(concurrency):
for n in range(concurrency):
if not self._execute_next():
break
return self._results()
Expand All @@ -143,17 +141,13 @@ def _execute(self, idx, statement, params):
callback=self._on_success, callback_args=args,
errback=self._on_error, errback_args=args)
except Exception as exc:
# exc_info with fail_fast to preserve stack trace info when raising on the client thread
# (matches previous behavior -- not sure why we wouldn't want stack trace in the other case)
e = sys.exc_info() if self._fail_fast and six.PY2 else exc

# If we're not failing fast and all executions are raising, there is a chance of recursing
# here as subsequent requests are attempted. If we hit this threshold, schedule this result/retry
# and let the event loop thread return.
if self._exec_depth < self.max_error_recursion:
self._put_result(e, idx, False)
self._put_result(exc, idx, False)
else:
self.session.submit(self._put_result, e, idx, False)
self.session.submit(self._put_result, exc, idx, False)
self._exec_depth -= 1

def _on_success(self, result, future, idx):
Expand All @@ -163,14 +157,6 @@ def _on_success(self, result, future, idx):
def _on_error(self, result, future, idx):
self._put_result(result, idx, False)

@staticmethod
def _raise(exc):
if six.PY2 and isinstance(exc, tuple):
(exc_type, value, traceback) = exc
six.reraise(exc_type, value, traceback)
else:
raise exc


class ConcurrentExecutorGenResults(_ConcurrentExecutor):

Expand All @@ -190,7 +176,7 @@ def _results(self):
try:
self._condition.release()
if self._fail_fast and not res[0]:
self._raise(res[1])
raise res[1]
yield res
finally:
self._condition.acquire()
Expand Down Expand Up @@ -221,9 +207,9 @@ def _results(self):
while self._current < self._exec_count:
self._condition.wait()
if self._exception and self._fail_fast:
self._raise(self._exception)
raise self._exception
if self._exception and self._fail_fast: # raise the exception even if there was no wait
self._raise(self._exception)
raise self._exception
return [r[1] for r in sorted(self._results_queue)]


Expand Down
14 changes: 3 additions & 11 deletions cassandra/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from heapq import heappush, heappop
import io
import logging
import six
from six.moves import range
import socket
import struct
import sys
Expand All @@ -33,7 +31,7 @@
if 'gevent.monkey' in sys.modules:
from gevent.queue import Queue, Empty
else:
from six.moves.queue import Queue, Empty # noqa
from queue import Queue, Empty # noqa

from cassandra import ConsistencyLevel, AuthenticationFailed, OperationTimedOut, ProtocolVersion
from cassandra.marshal import int32_pack
Expand Down Expand Up @@ -605,12 +603,6 @@ def wrapper(self, *args, **kwargs):

DEFAULT_CQL_VERSION = '3.0.0'

if six.PY3:
def int_from_buf_item(i):
return i
else:
int_from_buf_item = ord


class _ConnectionIOBuffer(object):
"""
Expand Down Expand Up @@ -1122,7 +1114,7 @@ def _read_frame_header(self):
buf = self._io_buffer.cql_frame_buffer.getvalue()
pos = len(buf)
if pos:
version = int_from_buf_item(buf[0]) & PROTOCOL_VERSION_MASK
version = buf[0] & PROTOCOL_VERSION_MASK
if version not in ProtocolVersion.SUPPORTED_VERSIONS:
raise ProtocolError("This version of the driver does not support protocol version %d" % version)
frame_header = frame_header_v3 if version >= 3 else frame_header_v1_v2
Expand Down Expand Up @@ -1321,7 +1313,7 @@ def _handle_options_response(self, options_response):
remote_supported_compressions)
else:
compression_type = None
if isinstance(self.compression, six.string_types):
if isinstance(self.compression, str):
# the user picked a specific compression type ('snappy' or 'lz4')
if self.compression not in remote_supported_compressions:
raise ProtocolError(
Expand Down
8 changes: 1 addition & 7 deletions cassandra/cqlengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import six


# Caching constants.
CACHING_ALL = "ALL"
CACHING_KEYS_ONLY = "KEYS_ONLY"
Expand All @@ -31,7 +28,4 @@ class ValidationError(CQLEngineException):


class UnicodeMixin(object):
if six.PY3:
__str__ = lambda x: x.__unicode__()
else:
__str__ = lambda x: six.text_type(x).encode('utf-8')
__str__ = lambda x: x.__unicode__()
Loading

0 comments on commit d0e639b

Please sign in to comment.