Update to use six.moves for simplify import of libraries that have moved. All tests passing for pythons other than 3

This commit is contained in:
Tim Savage
2014-03-17 01:47:48 +11:00
parent 1c60261c25
commit f985e42be7
19 changed files with 74 additions and 60 deletions

View File

@@ -2,14 +2,17 @@
This module houses the main classes you will interact with, This module houses the main classes you will interact with,
:class:`.Cluster` and :class:`.Session`. :class:`.Cluster` and :class:`.Session`.
""" """
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import logging import logging
import socket import socket
import sys import sys
import time import time
from threading import Lock, RLock, Thread, Event from threading import Lock, RLock, Thread, Event
import Queue
import six
from six.moves import xrange
from six.moves import queue as Queue
import weakref import weakref
from weakref import WeakValueDictionary from weakref import WeakValueDictionary
try: try:
@@ -51,6 +54,9 @@ try:
except ImportError: except ImportError:
from cassandra.io.asyncorereactor import AsyncoreConnection as DefaultConnection # NOQA from cassandra.io.asyncorereactor import AsyncoreConnection as DefaultConnection # NOQA
## Python 3 support #########
#############################
# Forces load of utf8 encoding module to avoid deadlock that occurs # Forces load of utf8 encoding module to avoid deadlock that occurs
# if code that is being imported tries to import the module in a seperate # if code that is being imported tries to import the module in a seperate
# thread. # thread.
@@ -1060,7 +1066,7 @@ class Session(object):
prepared_statement = None prepared_statement = None
if isinstance(query, basestring): if isinstance(query, six.string_types):
query = SimpleStatement(query) query = SimpleStatement(query)
elif isinstance(query, PreparedStatement): elif isinstance(query, PreparedStatement):
query = query.bind(parameters) query = query.bind(parameters)

View File

@@ -2,7 +2,8 @@ import errno
from functools import wraps, partial from functools import wraps, partial
import logging import logging
from threading import Event, RLock from threading import Event, RLock
from Queue import Queue
from six.moves.queue import Queue
from cassandra import ConsistencyLevel, AuthenticationFailed, OperationTimedOut from cassandra import ConsistencyLevel, AuthenticationFailed, OperationTimedOut
from cassandra.marshal import int8_unpack, int32_pack from cassandra.marshal import int8_unpack, int32_pack

View File

@@ -22,10 +22,9 @@ from datetime import datetime
from uuid import UUID from uuid import UUID
import warnings import warnings
try: import six
from cStringIO import StringIO from six.moves import cStringIO as StringIO
except ImportError: from six.moves import xrange
from StringIO import StringIO # NOQA
from cassandra.marshal import (int8_pack, int8_unpack, uint16_pack, uint16_unpack, from cassandra.marshal import (int8_pack, int8_unpack, uint16_pack, uint16_unpack,
int32_pack, int32_unpack, int64_pack, int64_unpack, int32_pack, int32_unpack, int64_pack, int64_unpack,
@@ -35,7 +34,12 @@ from cassandra.util import OrderedDict
apache_cassandra_type_prefix = 'org.apache.cassandra.db.marshal.' apache_cassandra_type_prefix = 'org.apache.cassandra.db.marshal.'
_number_types = frozenset((int, long, float)) ## Python 3 support #########
if six.PY3:
_number_types = frozenset((int, float))
else:
_number_types = frozenset((int, long, float))
#############################
try: try:
from blist import sortedset from blist import sortedset
@@ -482,7 +486,7 @@ class DateType(_CassandraType):
@classmethod @classmethod
def validate(cls, date): def validate(cls, date):
if isinstance(date, basestring): if isinstance(date, six.string_types):
date = cls.interpret_datestring(date) date = cls.interpret_datestring(date)
return date return date
@@ -624,7 +628,7 @@ class _SimpleParameterizedType(_ParameterizedType):
@classmethod @classmethod
def serialize_safe(cls, items): def serialize_safe(cls, items):
if isinstance(items, basestring): if isinstance(items, six.string_types):
raise TypeError("Received a string for a type that expects a sequence") raise TypeError("Received a string for a type that expects a sequence")
subtype, = cls.subtypes subtype, = cls.subtypes
@@ -733,7 +737,7 @@ class ReversedType(_ParameterizedType):
def is_counter_type(t): def is_counter_type(t):
if isinstance(t, basestring): if isinstance(t, six.string_types):
t = lookup_casstype(t) t = lookup_casstype(t)
return issubclass(t, CounterColumnType) return issubclass(t, CounterColumnType)

View File

@@ -2,10 +2,8 @@ import logging
import socket import socket
from uuid import UUID from uuid import UUID
try: from six.moves import cStringIO as StringIO
from cStringIO import StringIO from six.moves import xrange
except ImportError:
from StringIO import StringIO # ignore flake8 warning: # NOQA
from cassandra import (Unavailable, WriteTimeout, ReadTimeout, from cassandra import (Unavailable, WriteTimeout, ReadTimeout,
AlreadyExists, InvalidRequest, Unauthorized) AlreadyExists, InvalidRequest, Unauthorized)
@@ -17,6 +15,8 @@ from cassandra.cqltypes import (AsciiType, BytesType, BooleanType,
InetAddressType, IntegerType, ListType, InetAddressType, IntegerType, ListType,
LongType, MapType, SetType, TimeUUIDType, LongType, MapType, SetType, TimeUUIDType,
UTF8Type, UUIDType, lookup_casstype) UTF8Type, UUIDType, lookup_casstype)
import six
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -564,7 +564,7 @@ class BatchMessage(_MessageType):
write_byte(f, self.batch_type.value) write_byte(f, self.batch_type.value)
write_short(f, len(self.queries)) write_short(f, len(self.queries))
for string_or_query_id, params in self.queries: for string_or_query_id, params in self.queries:
if isinstance(string_or_query_id, basestring): if isinstance(string_or_query_id, six.string_types):
write_byte(f, 0) write_byte(f, 0)
write_longstring(f, string_or_query_id) write_longstring(f, string_or_query_id)
else: else:
@@ -679,7 +679,7 @@ def read_binary_string(f):
def write_string(f, s): def write_string(f, s):
if isinstance(s, unicode): if isinstance(s, six.text_type):
s = s.encode('utf8') s = s.encode('utf8')
write_short(f, len(s)) write_short(f, len(s))
f.write(s) f.write(s)
@@ -692,7 +692,7 @@ def read_longstring(f):
def write_longstring(f, s): def write_longstring(f, s):
if isinstance(s, unicode): if isinstance(s, six.text_type):
s = s.encode('utf8') s = s.encode('utf8')
write_int(f, len(s)) write_int(f, len(s))
f.write(s) f.write(s)

View File

@@ -4,9 +4,14 @@ import datetime
import sys import sys
import types import types
from uuid import UUID from uuid import UUID
import six
from cassandra.util import OrderedDict from cassandra.util import OrderedDict
if six.PY3:
unicode = str
long = int
def cql_quote(term): def cql_quote(term):
if isinstance(term, unicode): if isinstance(term, unicode):
@@ -78,13 +83,10 @@ def cql_encode_all_types(val):
cql_encoders = { cql_encoders = {
float: cql_encode_object, float: cql_encode_object,
buffer: cql_encode_bytes,
bytearray: cql_encode_bytes, bytearray: cql_encode_bytes,
str: cql_encode_str, str: cql_encode_str,
unicode: cql_encode_unicode,
types.NoneType: cql_encode_none, types.NoneType: cql_encode_none,
int: cql_encode_object, int: cql_encode_object,
long: cql_encode_object,
UUID: cql_encode_object, UUID: cql_encode_object,
datetime.datetime: cql_encode_datetime, datetime.datetime: cql_encode_datetime,
datetime.date: cql_encode_date, datetime.date: cql_encode_date,
@@ -96,3 +98,10 @@ cql_encoders = {
frozenset: cql_encode_set_collection, frozenset: cql_encode_set_collection,
types.GeneratorType: cql_encode_list_collection types.GeneratorType: cql_encode_list_collection
} }
if six.PY2:
cql_encoders.update({
buffer: cql_encode_bytes,
unicode: cql_encode_unicode,
long: cql_encode_object,
})

View File

@@ -7,16 +7,14 @@ import sys
from threading import Event, Lock, Thread from threading import Event, Lock, Thread
import time import time
import traceback import traceback
import Queue
from six.moves import queue as Queue
from six.moves import cStringIO as StringIO
from six.moves import xrange
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, EINVAL, EISCONN, errorcode from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, EINVAL, EISCONN, errorcode
import asyncore import asyncore
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO # ignore flake8 warning: # NOQA
try: try:
import ssl import ssl
except ImportError: except ImportError:
@@ -381,7 +379,7 @@ class AsyncoreConnection(Connection, asyncore.dispatcher):
return waiter.deliver(timeout) return waiter.deliver(timeout)
except OperationTimedOut: except OperationTimedOut:
raise raise
except Exception, exc: except Exception as exc:
self.defunct(exc) self.defunct(exc)
raise raise

View File

@@ -6,7 +6,10 @@ import socket
from threading import Event, Lock, Thread from threading import Event, Lock, Thread
import time import time
import traceback import traceback
import Queue
from six.moves.queue import Queue
from six.moves import cStringIO as StringIO
from six.moves import xrange
from cassandra import OperationTimedOut from cassandra import OperationTimedOut
from cassandra.connection import (Connection, ResponseWaiter, ConnectionShutdown, from cassandra.connection import (Connection, ResponseWaiter, ConnectionShutdown,
@@ -25,10 +28,6 @@ except ImportError:
"for instructions on installing build dependencies and building " "for instructions on installing build dependencies and building "
"the C extension.") "the C extension.")
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO # ignore flake8 warning: # NOQA
try: try:
import ssl import ssl
@@ -436,7 +435,7 @@ class LibevConnection(Connection):
return waiter.deliver(timeout) return waiter.deliver(timeout)
except OperationTimedOut: except OperationTimedOut:
raise raise
except Exception, exc: except Exception as exc:
self.defunct(exc) self.defunct(exc)
raise raise

View File

@@ -202,6 +202,11 @@ initmurmur3(void)
#else #else
PyMODINIT_FUNC
PyInit_murmur3(void)
{
}
/* Python 3.x */ /* Python 3.x */
// TODO // TODO

View File

@@ -5,6 +5,8 @@ from threading import Lock
from cassandra import ConsistencyLevel from cassandra import ConsistencyLevel
from six.moves import xrange
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@@ -360,7 +360,7 @@ class HostConnectionPool(object):
def _create_new_connection(self): def _create_new_connection(self):
try: try:
self._add_conn_if_under_max() self._add_conn_if_under_max()
except (ConnectionException, socket.error), exc: except (ConnectionException, socket.error) as exc:
log.warn("Failed to create new connection to %s: %s", self.host, exc) log.warn("Failed to create new connection to %s: %s", self.host, exc)
except Exception: except Exception:
log.exception("Unexpectedly failed to create new connection") log.exception("Unexpectedly failed to create new connection")

View File

@@ -9,6 +9,7 @@ from datetime import datetime, timedelta
import re import re
import struct import struct
import time import time
import six
from cassandra import ConsistencyLevel, OperationTimedOut from cassandra import ConsistencyLevel, OperationTimedOut
from cassandra.cqltypes import unix_time_from_uuid1 from cassandra.cqltypes import unix_time_from_uuid1
@@ -354,7 +355,7 @@ class BatchStatement(Statement):
Statement.__init__(self, retry_policy=retry_policy, consistency_level=consistency_level) Statement.__init__(self, retry_policy=retry_policy, consistency_level=consistency_level)
def add(self, statement, parameters=None): def add(self, statement, parameters=None):
if isinstance(statement, basestring): if isinstance(statement, six.string_types):
if parameters: if parameters:
statement = bind_params(statement, parameters) statement = bind_params(statement, parameters)
self._statements_and_parameters.append((statement, ())) self._statements_and_parameters.append((statement, ()))

View File

@@ -1,7 +1,5 @@
from __future__ import with_statement from __future__ import with_statement
from UserDict import DictMixin
try: try:
from collections import OrderedDict from collections import OrderedDict
except ImportError: except ImportError:
@@ -28,6 +26,7 @@ except ImportError:
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE. # OTHER DEALINGS IN THE SOFTWARE.
from UserDict import DictMixin
class OrderedDict(dict, DictMixin): # noqa class OrderedDict(dict, DictMixin): # noqa
""" A dictionary which maintains the insertion order of keys. """ """ A dictionary which maintains the insertion order of keys. """

View File

@@ -1,3 +1,4 @@
blist blist
futures futures
scales scales
six >=1.6

View File

@@ -149,7 +149,7 @@ def run_setup(extensions):
kw['cmdclass']['build_ext'] = build_extensions kw['cmdclass']['build_ext'] = build_extensions
kw['ext_modules'] = extensions kw['ext_modules'] = extensions
dependencies = ['futures', 'scales', 'blist', 'six'] dependencies = ['futures', 'scales', 'blist', 'six >=1.6']
if platform.python_implementation() != "CPython": if platform.python_implementation() != "CPython":
dependencies.remove('blist') dependencies.remove('blist')

View File

@@ -5,10 +5,9 @@ except ImportError:
import errno import errno
import os import os
try:
from StringIO import StringIO from six.moves import StringIO
except ImportError:
from io import StringIO
import socket import socket
from socket import error as socket_error from socket import error as socket_error

View File

@@ -5,10 +5,9 @@ except ImportError:
import errno import errno
import os import os
try:
from StringIO import StringIO from six.moves import StringIO
except ImportError:
from io import StringIO
from socket import error as socket_error from socket import error as socket_error
from mock import patch, Mock from mock import patch, Mock

View File

@@ -3,10 +3,7 @@ try:
except ImportError: except ImportError:
import unittest # noqa import unittest # noqa
try: from six.moves import StringIO
from StringIO import StringIO
except ImportError:
from io import StringIO
from mock import Mock, ANY from mock import Mock, ANY

View File

@@ -1,5 +1,3 @@
import six
try: try:
import unittest2 as unittest import unittest2 as unittest
except ImportError: except ImportError:
@@ -10,8 +8,7 @@ from cassandra.query import PreparedStatement, BoundStatement
from cassandra.cqltypes import Int32Type from cassandra.cqltypes import Int32Type
from cassandra.util import OrderedDict from cassandra.util import OrderedDict
if six.PY3: from six.moves import xrange
xrange = range
class ParamBindingTest(unittest.TestCase): class ParamBindingTest(unittest.TestCase):

View File

@@ -1,5 +1,3 @@
import six
try: try:
import unittest2 as unittest import unittest2 as unittest
except ImportError: except ImportError:
@@ -24,8 +22,7 @@ from cassandra.policies import (RoundRobinPolicy, DCAwareRoundRobinPolicy,
from cassandra.pool import Host from cassandra.pool import Host
from cassandra.query import Statement from cassandra.query import Statement
if six.PY3: from six.moves import xrange
xrange = range
class TestLoadBalancingPolicy(unittest.TestCase): class TestLoadBalancingPolicy(unittest.TestCase):