Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions cassandra/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
This module houses the main classes you will interact with,
:class:`.Cluster` and :class:`.Session`.
"""
from __future__ import absolute_import

import atexit
import datetime
Expand Down Expand Up @@ -110,10 +109,7 @@
# TODO: remove it when eventlet issue would be fixed
EventletConnection = None

try:
from weakref import WeakSet
except ImportError:
from cassandra.util import WeakSet # NOQA
from weakref import WeakSet

def _is_gevent_monkey_patched():
if 'gevent.monkey' not in sys.modules:
Expand Down Expand Up @@ -183,12 +179,6 @@ def _connection_reduce_fn(val,import_fn):
raise DependencyException("Exception loading connection class dependencies", excs)
DefaultConnection = conn_class

# 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
# thread.
# See http://bugs.python.org/issue10923
"".encode('utf8')

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -1548,34 +1538,33 @@ def _create_thread_pool_executor(self, **kwargs):
Create a ThreadPoolExecutor for the cluster. In most cases, the built-in
`concurrent.futures.ThreadPoolExecutor` is used.

Python 3.7+ and Eventlet cause the `concurrent.futures.ThreadPoolExecutor`
Eventlet causes the `concurrent.futures.ThreadPoolExecutor`
to hang indefinitely. In that case, the user needs to have the `futurist`
package so we can use the `futurist.GreenThreadPoolExecutor` class instead.

:param kwargs: All keyword args are passed to the ThreadPoolExecutor constructor.
:return: A ThreadPoolExecutor instance.
"""
tpe_class = ThreadPoolExecutor
if sys.version_info[0] >= 3 and sys.version_info[1] >= 7:
try:
from cassandra.io.eventletreactor import EventletConnection
is_eventlet = issubclass(self.connection_class, EventletConnection)
except:
# Eventlet is not available or can't be detected
return tpe_class(**kwargs)
try:
from cassandra.io.eventletreactor import EventletConnection
is_eventlet = issubclass(self.connection_class, EventletConnection)
except:
# Eventlet is not available or can't be detected
return tpe_class(**kwargs)

if is_eventlet:
try:
from futurist import GreenThreadPoolExecutor
tpe_class = GreenThreadPoolExecutor
except ImportError:
# futurist is not available
raise ImportError(
("Python 3.7+ and Eventlet cause the `concurrent.futures.ThreadPoolExecutor` "
"to hang indefinitely. If you want to use the Eventlet reactor, you "
"need to install the `futurist` package to allow the driver to use "
"the GreenThreadPoolExecutor. See https://github.com/eventlet/eventlet/issues/508 "
"for more details."))
if is_eventlet:
try:
from futurist import GreenThreadPoolExecutor
tpe_class = GreenThreadPoolExecutor
except ImportError:
# futurist is not available
raise ImportError(
("Eventlet causes the `concurrent.futures.ThreadPoolExecutor` "
"to hang indefinitely. If you want to use the Eventlet reactor, you "
"need to install the `futurist` package to allow the driver to use "
"the GreenThreadPoolExecutor. See https://github.com/eventlet/eventlet/issues/508 "
"for more details."))

return tpe_class(**kwargs)

Expand Down Expand Up @@ -5389,11 +5378,9 @@ def __getitem__(self, i):
self._enter_list_mode("index operator")
return self._current_rows[i]

def __nonzero__(self):
def __bool__(self):
return bool(self._current_rows)

__bool__ = __nonzero__

def get_query_trace(self, max_wait_sec=None):
"""
Gets the last query trace from the associated future.
Expand Down
1 change: 0 additions & 1 deletion cassandra/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import # to enable import io from stdlib
from collections import defaultdict, deque
import errno
from functools import wraps, partial, total_ordering
Expand Down
3 changes: 0 additions & 3 deletions cassandra/cqlengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,3 @@ class CQLEngineException(Exception):
class ValidationError(CQLEngineException):
pass


class UnicodeMixin(object):
__str__ = lambda x: x.__unicode__()
8 changes: 4 additions & 4 deletions cassandra/cqlengine/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

from datetime import datetime

from cassandra.cqlengine import UnicodeMixin, ValidationError
from cassandra.cqlengine import ValidationError

def get_total_seconds(td):
return td.total_seconds()

class QueryValue(UnicodeMixin):
class QueryValue:
"""
Base class for query filter values. Subclasses of these classes can
be passed into .filter() keyword args
Expand All @@ -31,7 +31,7 @@ def __init__(self, value):
self.value = value
self.context_id = None

def __unicode__(self):
def __str__(self):
return self.format_string.format(self.context_id)

def set_context_id(self, ctx_id):
Expand Down Expand Up @@ -109,7 +109,7 @@ def set_columns(self, columns):
def get_context_size(self):
return len(self.value)

def __unicode__(self):
def __str__(self):
token_args = ', '.join('%({0})s'.format(self.context_id + i) for i in range(self.get_context_size()))
return "token({0})".format(token_args)

Expand Down
2 changes: 1 addition & 1 deletion cassandra/cqlengine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class ColumnQueryEvaluator(query.AbstractQueryableColumn):
def __init__(self, column):
self.column = column

def __unicode__(self):
def __str__(self):
return self.column.db_field_name

def _get_column(self):
Expand Down
2 changes: 1 addition & 1 deletion cassandra/cqlengine/named.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class NamedColumn(AbstractQueryableColumn):
def __init__(self, name):
self.name = name

def __unicode__(self):
def __str__(self):
return self.name

def _get_column(self):
Expand Down
5 changes: 2 additions & 3 deletions cassandra/cqlengine/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from cassandra.cqlengine import UnicodeMixin


class QueryOperatorException(Exception):
pass


class BaseQueryOperator(UnicodeMixin):
class BaseQueryOperator:
# The symbol that identifies this operator in kwargs
# ie: colname__<symbol>
symbol = None

# The comparator symbol this operator uses in cql
cql_symbol = None

def __unicode__(self):
def __str__(self):
if self.cql_symbol is None:
raise QueryOperatorException("cql symbol is None")
return self.cql_symbol
Expand Down
11 changes: 4 additions & 7 deletions cassandra/cqlengine/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from warnings import warn

from cassandra.query import SimpleStatement, BatchType as CBatchType, BatchStatement
from cassandra.cqlengine import columns, CQLEngineException, ValidationError, UnicodeMixin
from cassandra.cqlengine import columns, CQLEngineException, ValidationError
from cassandra.cqlengine import connection as conn
from cassandra.cqlengine.functions import Token, BaseQueryFunction, QueryValue
from cassandra.cqlengine.operators import (InOperator, EqualsOperator, GreaterThanOperator,
Expand Down Expand Up @@ -78,7 +78,7 @@ def check_applied(result):
raise LWTException(result.one())


class AbstractQueryableColumn(UnicodeMixin):
class AbstractQueryableColumn:
"""
exposes cql query operators through pythons
builtin comparator symbols
Expand All @@ -87,7 +87,7 @@ class AbstractQueryableColumn(UnicodeMixin):
def _get_column(self):
raise NotImplementedError

def __unicode__(self):
def __str__(self):
raise NotImplementedError

def _to_database(self, val):
Expand Down Expand Up @@ -405,11 +405,8 @@ def _execute(self, statement):
check_applied(result)
return result

def __unicode__(self):
return str(self._select_query())

def __str__(self):
return str(self.__unicode__())
return str(self._select_query())

def __call__(self, *args, **kwargs):
return self.filter(*args, **kwargs)
Expand Down
Loading
Loading