Skip to content

Commit f985e42

Browse files
committed
Update to use six.moves for simplify import of libraries that have moved. All tests passing for pythons other than 3
1 parent 1c60261 commit f985e42

19 files changed

Lines changed: 74 additions & 60 deletions

cassandra/cluster.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
This module houses the main classes you will interact with,
33
:class:`.Cluster` and :class:`.Session`.
44
"""
5-
65
from concurrent.futures import ThreadPoolExecutor
76
import logging
87
import socket
98
import sys
109
import time
1110
from threading import Lock, RLock, Thread, Event
12-
import Queue
11+
12+
import six
13+
from six.moves import xrange
14+
from six.moves import queue as Queue
15+
1316
import weakref
1417
from weakref import WeakValueDictionary
1518
try:
@@ -51,6 +54,9 @@
5154
except ImportError:
5255
from cassandra.io.asyncorereactor import AsyncoreConnection as DefaultConnection # NOQA
5356

57+
## Python 3 support #########
58+
#############################
59+
5460
# Forces load of utf8 encoding module to avoid deadlock that occurs
5561
# if code that is being imported tries to import the module in a seperate
5662
# thread.
@@ -1060,7 +1066,7 @@ def _create_response_future(self, query, parameters, trace):
10601066

10611067
prepared_statement = None
10621068

1063-
if isinstance(query, basestring):
1069+
if isinstance(query, six.string_types):
10641070
query = SimpleStatement(query)
10651071
elif isinstance(query, PreparedStatement):
10661072
query = query.bind(parameters)

cassandra/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from functools import wraps, partial
33
import logging
44
from threading import Event, RLock
5-
from Queue import Queue
5+
6+
from six.moves.queue import Queue
67

78
from cassandra import ConsistencyLevel, AuthenticationFailed, OperationTimedOut
89
from cassandra.marshal import int8_unpack, int32_pack

cassandra/cqltypes.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
from uuid import UUID
2323
import warnings
2424

25-
try:
26-
from cStringIO import StringIO
27-
except ImportError:
28-
from StringIO import StringIO # NOQA
25+
import six
26+
from six.moves import cStringIO as StringIO
27+
from six.moves import xrange
2928

3029
from cassandra.marshal import (int8_pack, int8_unpack, uint16_pack, uint16_unpack,
3130
int32_pack, int32_unpack, int64_pack, int64_unpack,
@@ -35,7 +34,12 @@
3534

3635
apache_cassandra_type_prefix = 'org.apache.cassandra.db.marshal.'
3736

38-
_number_types = frozenset((int, long, float))
37+
## Python 3 support #########
38+
if six.PY3:
39+
_number_types = frozenset((int, float))
40+
else:
41+
_number_types = frozenset((int, long, float))
42+
#############################
3943

4044
try:
4145
from blist import sortedset
@@ -482,7 +486,7 @@ class DateType(_CassandraType):
482486

483487
@classmethod
484488
def validate(cls, date):
485-
if isinstance(date, basestring):
489+
if isinstance(date, six.string_types):
486490
date = cls.interpret_datestring(date)
487491
return date
488492

@@ -624,7 +628,7 @@ def deserialize_safe(cls, byts):
624628

625629
@classmethod
626630
def serialize_safe(cls, items):
627-
if isinstance(items, basestring):
631+
if isinstance(items, six.string_types):
628632
raise TypeError("Received a string for a type that expects a sequence")
629633

630634
subtype, = cls.subtypes
@@ -733,7 +737,7 @@ def serialize_safe(cls, val):
733737

734738

735739
def is_counter_type(t):
736-
if isinstance(t, basestring):
740+
if isinstance(t, six.string_types):
737741
t = lookup_casstype(t)
738742
return issubclass(t, CounterColumnType)
739743

cassandra/decoder.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
import socket
33
from uuid import UUID
44

5-
try:
6-
from cStringIO import StringIO
7-
except ImportError:
8-
from StringIO import StringIO # ignore flake8 warning: # NOQA
5+
from six.moves import cStringIO as StringIO
6+
from six.moves import xrange
97

108
from cassandra import (Unavailable, WriteTimeout, ReadTimeout,
119
AlreadyExists, InvalidRequest, Unauthorized)
@@ -17,6 +15,8 @@
1715
InetAddressType, IntegerType, ListType,
1816
LongType, MapType, SetType, TimeUUIDType,
1917
UTF8Type, UUIDType, lookup_casstype)
18+
import six
19+
2020

2121
log = logging.getLogger(__name__)
2222

@@ -564,7 +564,7 @@ def send_body(self, f, protocol_version):
564564
write_byte(f, self.batch_type.value)
565565
write_short(f, len(self.queries))
566566
for string_or_query_id, params in self.queries:
567-
if isinstance(string_or_query_id, basestring):
567+
if isinstance(string_or_query_id, six.string_types):
568568
write_byte(f, 0)
569569
write_longstring(f, string_or_query_id)
570570
else:
@@ -679,7 +679,7 @@ def read_binary_string(f):
679679

680680

681681
def write_string(f, s):
682-
if isinstance(s, unicode):
682+
if isinstance(s, six.text_type):
683683
s = s.encode('utf8')
684684
write_short(f, len(s))
685685
f.write(s)
@@ -692,7 +692,7 @@ def read_longstring(f):
692692

693693

694694
def write_longstring(f, s):
695-
if isinstance(s, unicode):
695+
if isinstance(s, six.text_type):
696696
s = s.encode('utf8')
697697
write_int(f, len(s))
698698
f.write(s)

cassandra/encoder.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
import sys
55
import types
66
from uuid import UUID
7+
import six
78

89
from cassandra.util import OrderedDict
910

11+
if six.PY3:
12+
unicode = str
13+
long = int
14+
1015

1116
def cql_quote(term):
1217
if isinstance(term, unicode):
@@ -78,13 +83,10 @@ def cql_encode_all_types(val):
7883

7984
cql_encoders = {
8085
float: cql_encode_object,
81-
buffer: cql_encode_bytes,
8286
bytearray: cql_encode_bytes,
8387
str: cql_encode_str,
84-
unicode: cql_encode_unicode,
8588
types.NoneType: cql_encode_none,
8689
int: cql_encode_object,
87-
long: cql_encode_object,
8890
UUID: cql_encode_object,
8991
datetime.datetime: cql_encode_datetime,
9092
datetime.date: cql_encode_date,
@@ -96,3 +98,10 @@ def cql_encode_all_types(val):
9698
frozenset: cql_encode_set_collection,
9799
types.GeneratorType: cql_encode_list_collection
98100
}
101+
102+
if six.PY2:
103+
cql_encoders.update({
104+
buffer: cql_encode_bytes,
105+
unicode: cql_encode_unicode,
106+
long: cql_encode_object,
107+
})

cassandra/io/asyncorereactor.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
from threading import Event, Lock, Thread
88
import time
99
import traceback
10-
import Queue
10+
11+
from six.moves import queue as Queue
12+
from six.moves import cStringIO as StringIO
13+
from six.moves import xrange
1114
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, EINVAL, EISCONN, errorcode
1215

1316
import asyncore
1417

15-
try:
16-
from cStringIO import StringIO
17-
except ImportError:
18-
from StringIO import StringIO # ignore flake8 warning: # NOQA
19-
2018
try:
2119
import ssl
2220
except ImportError:
@@ -381,7 +379,7 @@ def wait_for_responses(self, *msgs, **kwargs):
381379
return waiter.deliver(timeout)
382380
except OperationTimedOut:
383381
raise
384-
except Exception, exc:
382+
except Exception as exc:
385383
self.defunct(exc)
386384
raise
387385

cassandra/io/libevreactor.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from threading import Event, Lock, Thread
77
import time
88
import traceback
9-
import Queue
9+
10+
from six.moves.queue import Queue
11+
from six.moves import cStringIO as StringIO
12+
from six.moves import xrange
1013

1114
from cassandra import OperationTimedOut
1215
from cassandra.connection import (Connection, ResponseWaiter, ConnectionShutdown,
@@ -25,10 +28,6 @@
2528
"for instructions on installing build dependencies and building "
2629
"the C extension.")
2730

28-
try:
29-
from cStringIO import StringIO
30-
except ImportError:
31-
from StringIO import StringIO # ignore flake8 warning: # NOQA
3231

3332
try:
3433
import ssl
@@ -436,7 +435,7 @@ def wait_for_responses(self, *msgs, **kwargs):
436435
return waiter.deliver(timeout)
437436
except OperationTimedOut:
438437
raise
439-
except Exception, exc:
438+
except Exception as exc:
440439
self.defunct(exc)
441440
raise
442441

cassandra/murmur3.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ initmurmur3(void)
202202

203203
#else
204204

205+
PyMODINIT_FUNC
206+
PyInit_murmur3(void)
207+
{
208+
209+
}
205210
/* Python 3.x */
206211
// TODO
207212

cassandra/policies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from cassandra import ConsistencyLevel
77

8+
from six.moves import xrange
9+
810
log = logging.getLogger(__name__)
911

1012

cassandra/pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def _maybe_spawn_new_connection(self):
360360
def _create_new_connection(self):
361361
try:
362362
self._add_conn_if_under_max()
363-
except (ConnectionException, socket.error), exc:
363+
except (ConnectionException, socket.error) as exc:
364364
log.warn("Failed to create new connection to %s: %s", self.host, exc)
365365
except Exception:
366366
log.exception("Unexpectedly failed to create new connection")

0 commit comments

Comments
 (0)