From 53b2b48f582b2079b48b5336a06aeb97089fe042 Mon Sep 17 00:00:00 2001 From: Mark Florisson Date: Sat, 8 Aug 2015 12:28:53 +0100 Subject: [PATCH] Be more careful when Cython is available but NumPy is not --- cassandra/cython_deps.py | 8 +++++++- cassandra/protocol.py | 11 +++++++---- tests/unit/cython/utils.py | 12 ++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/cassandra/cython_deps.py b/cassandra/cython_deps.py index 41516426..fdd15464 100644 --- a/cassandra/cython_deps.py +++ b/cassandra/cython_deps.py @@ -2,4 +2,10 @@ try: from cassandra.rowparser import make_recv_results_rows HAVE_CYTHON = True except ImportError: - HAVE_CYTHON = False \ No newline at end of file + HAVE_CYTHON = False + +try: + import numpy + HAVE_NUMPY = True +except ImportError: + HAVE_NUMPY = False diff --git a/cassandra/protocol.py b/cassandra/protocol.py index de8a464d..5ebbfa5c 100644 --- a/cassandra/protocol.py +++ b/cassandra/protocol.py @@ -40,7 +40,7 @@ from cassandra.cqltypes import (AsciiType, BytesType, BooleanType, TupleType, lookup_casstype, SimpleDateType, TimeType, ByteType, ShortType) from cassandra.policies import WriteType -from cassandra.cython_deps import HAVE_CYTHON +from cassandra.cython_deps import HAVE_CYTHON, HAVE_NUMPY from cassandra import util log = logging.getLogger(__name__) @@ -1039,14 +1039,17 @@ def cython_protocol_handler(colparser): if HAVE_CYTHON: from cassandra.objparser import ListParser, LazyParser - from cassandra.numpyparser import NumpyParser - ProtocolHandler = cython_protocol_handler(ListParser()) LazyProtocolHandler = cython_protocol_handler(LazyParser()) - NumpyProtocolHandler = cython_protocol_handler(NumpyParser()) else: # Use Python-based ProtocolHandler LazyProtocolHandler = None + + +if HAVE_CYTHON and HAVE_NUMPY: + from cassandra.numpyparser import NumpyParser + NumpyProtocolHandler = cython_protocol_handler(NumpyParser()) +else: NumpyProtocolHandler = None diff --git a/tests/unit/cython/utils.py b/tests/unit/cython/utils.py index eea4698f..f2598c0e 100644 --- a/tests/unit/cython/utils.py +++ b/tests/unit/cython/utils.py @@ -1,9 +1,4 @@ -try: - import tests.unit.cython.dummy_module -except ImportError: - have_cython = False -else: - have_cython = True +from cassandra.cython_deps import HAVE_CYTHON, HAVE_NUMPY try: import unittest2 as unittest @@ -18,10 +13,11 @@ def cyimport(import_path): try: return __import__(import_path, fromlist=True) except ImportError: - if have_cython: + if HAVE_CYTHON: raise return None # @cythontest # def test_something(self): ... -cythontest = unittest.skipUnless(have_cython, 'Cython is not available') +cythontest = unittest.skipUnless(HAVE_CYTHON, 'Cython is not available') +numpytest = unittest.skipUnless(HAVE_CYTHON and HAVE_NUMPY, 'NumPy is not available') \ No newline at end of file