Skip to content

Commit ac03767

Browse files
Lorak-mmkfruch
authored andcommitted
Fix code incompatibilities with Python 2
There are some functionalities used that don't work with Python 2. - Format strings - list.clear() - Type hints - super() without arguments - Packages without __init__.py file - Some import names (futures.thread -> concurrent.futures) Import behaviour changed between Py2 and 3 and one file was missing `from __future__ import absolute_import` line (that makes the behaviour consistent between 2 and 3) which caused import error. Some members in "c_sharding_info.pyx" had "str" type. This type maps to "str" both in Py2 and 3 - and those are different types - raw bytes in Py2, unicode string in py3. This caused errors in Py2, because code was trying to assign unicode strings to those members. The fix is to use Cython's / Pyrex's "unicode" type - it maps to "unicode" in Py2 and to "str" in Py3. This commit fixes all of those problems.
1 parent 91eaf4a commit ac03767

File tree

7 files changed

+20
-14
lines changed

7 files changed

+20
-14
lines changed

cassandra/c_shard_info.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ cdef extern from *:
1919

2020
cdef class ShardingInfo():
2121
cdef readonly int shards_count
22-
cdef readonly str partitioner
23-
cdef readonly str sharding_algorithm
22+
cdef readonly unicode partitioner
23+
cdef readonly unicode sharding_algorithm
2424
cdef readonly int sharding_ignore_msb
2525
cdef readonly int shard_aware_port
2626
cdef readonly int shard_aware_port_ssl

cassandra/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ def _initiate_connection(self, sockaddr):
903903
break
904904
except Exception as ex:
905905
log.debug("port=%d couldn't bind cause: %s", port, str(ex))
906-
log.debug(f'connection (%r) port=%d should be shard_id=%d', id(self), port, port % self.total_shards)
906+
log.debug('connection (%r) port=%d should be shard_id=%d', id(self), port, port % self.total_shards)
907907

908908
self._socket.connect(sockaddr)
909909

cassandra/pool.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"""
1616
Connection pooling and host management.
1717
"""
18+
from __future__ import absolute_import
19+
1820
from concurrent.futures import Future
1921
from functools import total_ordering
2022
import logging
@@ -1200,7 +1202,9 @@ def shutdown(self):
12001202
with self._lock:
12011203
connections_to_close.extend(self._connections)
12021204
self.open_count -= len(self._connections)
1203-
self._connections.clear()
1205+
# After dropping support for Python 2 we can again use list.clear()
1206+
# self._connections.clear()
1207+
del self._connections[:]
12041208
connections_to_close.extend(self._trash)
12051209
self._trash.clear()
12061210

cassandra/scylla/__init__.py

Whitespace-only changes.

cassandra/scylla/cloud.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ def nth(iterable, n, default=None):
5252

5353

5454
class CloudConfiguration:
55-
endpoint_factory: SniEndPointFactory
56-
contact_points: list
57-
auth_provider: AuthProvider = None
58-
ssl_options: dict
59-
ssl_context: SSLContext
60-
skip_tls_verify: bool
55+
# Commented out because this syntax doesn't work with Python2
56+
# Can be restores after dropping support for Python2
57+
# endpoint_factory: SniEndPointFactory
58+
# contact_points: list
59+
# auth_provider: AuthProvider = None
60+
# ssl_options: dict
61+
# ssl_context: SSLContext
62+
# skip_tls_verify: bool
6163

6264
def __init__(self, configuration_file, pyopenssl=False, endpoint_factory=None):
6365
cloud_config = yaml.safe_load(open(configuration_file))

tests/unit/test_host_connection_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class MockSession(MagicMock):
283283
keyspace = "reprospace"
284284

285285
def __init__(self, *args, **kwargs):
286-
super().__init__(*args, **kwargs)
286+
super(MockSession, self).__init__(*args, **kwargs)
287287
self.cluster = MagicMock()
288288
self.cluster.executor = ThreadPoolExecutor(max_workers=2, initializer=self.executor_init)
289289
self.cluster.signal_connection_failure = lambda *args, **kwargs: False

tests/unit/test_shard_aware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import unittest # noqa
1919

2020
import logging
21-
from unittest.mock import MagicMock
22-
from futures.thread import ThreadPoolExecutor
21+
from mock import MagicMock
22+
from concurrent.futures import ThreadPoolExecutor
2323

2424
from cassandra.cluster import ShardAwareOptions
2525
from cassandra.pool import HostConnection, HostDistance
@@ -62,7 +62,7 @@ class MockSession(MagicMock):
6262
keyspace = "ks1"
6363

6464
def __init__(self, is_ssl=False, *args, **kwargs):
65-
super().__init__(*args, **kwargs)
65+
super(MockSession, self).__init__(*args, **kwargs)
6666
self.cluster = MagicMock()
6767
if is_ssl:
6868
self.cluster.ssl_options = {'some_ssl_options': True}

0 commit comments

Comments
 (0)