Skip to content

Commit 9de29a7

Browse files
committed
Add optional cythonized core driver files.
1 parent 858a268 commit 9de29a7

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ tests/integration/ccm
1717
setuptools*.tar.gz
1818
setuptools*.egg
1919

20+
cassandra/*.c
21+
!cassandra/murmur3.c
22+
2023
# OSX
2124
.DS_Store
2225

cassandra/marshal.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ def _make_packer(format_string):
4848
def varint_unpack(term):
4949
val = int(''.join("%02x" % i for i in term), 16)
5050
if (term[0] & 128) != 0:
51-
val -= 1 << (len(term) * 8)
51+
len_term = len(term) # pulling this out of the expression to avoid overflow in cython optimized code
52+
val -= 1 << (len_term * 8)
5253
return val
5354
else:
5455
def varint_unpack(term): # noqa
5556
val = int(term.encode('hex'), 16)
5657
if (ord(term[0]) & 128) != 0:
57-
val = val - (1 << (len(term) * 8))
58+
len_term = len(term) # pulling this out of the expression to avoid overflow in cython optimized code
59+
val = val - (1 << (len_term * 8))
5860
return val
5961

6062

setup.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,28 @@ def run_setup(extensions):
234234
],
235235
**kw)
236236

237-
extensions = [murmur3_ext, libev_ext]
237+
extensions = []
238+
239+
if "--no-murmur3" not in sys.argv:
240+
extensions.append(murmur3_ext)
241+
242+
if "--no-libev" not in sys.argv:
243+
extensions.append(libev_ext)
244+
245+
if "--no-cython" not in sys.argv:
246+
try:
247+
from Cython.Build import cythonize
248+
cython_candidates = ['cluster', 'concurrent', 'connection', 'cqltypes', 'marshal', 'metadata', 'pool', 'protocol', 'query', 'util']
249+
extensions.extend(cythonize(
250+
[Extension('cassandra.%s' % m, ['cassandra/%s.py' % m], extra_compile_args=['-Wno-unused-function']) for m in cython_candidates],
251+
exclude_failures=True))
252+
except ImportError:
253+
warnings.warn("Cython is not installed. Not compiling core driver files as extensions (optional).")
254+
238255
if "--no-extensions" in sys.argv:
239-
sys.argv = [a for a in sys.argv if a != "--no-extensions"]
240256
extensions = []
241-
elif "--no-murmur3" in sys.argv:
242-
sys.argv = [a for a in sys.argv if a != "--no-murmur3"]
243-
extensions.remove(murmur3_ext)
244-
elif "--no-libev" in sys.argv:
245-
sys.argv = [a for a in sys.argv if a != "--no-libev"]
246-
extensions.remove(libev_ext)
257+
258+
sys.argv = [a for a in sys.argv if a not in ("--no-murmur3", "--no-libev", "--no-cython", "--no-extensions")]
247259

248260
is_windows = os.name == 'nt'
249261
if is_windows:

0 commit comments

Comments
 (0)