Skip to content

Commit df1fa44

Browse files
committed
Address review feedback: fix docstrings, imports, and add documentation
- Fix _check_float_range() docstring: clarify it raises OverflowError, not struct.error - Fix _check_int32_range() docstring: same clarification - Document __getitem__ requirement in vector fast-paths (_serialize_float, _serialize_double, _serialize_int32) as intentional for performance - Expand test import guard to cover VERIFY_CYTHON - Remove unused imports: math, parse_casstype_args
1 parent d6b8571 commit df1fa44

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

cassandra/serializers.pyx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ from cassandra.util import is_little_endian
4545
cdef inline void _check_float_range(double value) except *:
4646
"""Raise OverflowError for finite values outside float32 range.
4747
48-
Matches the behavior of struct.pack('>f', value), which raises
49-
struct.error for values that cannot be represented as a 32-bit
50-
IEEE 754 float. inf, -inf, and nan pass through unchanged.
48+
Intentionally raises OverflowError (not struct.error) so callers
49+
can catch a single exception type. The accepted range matches
50+
struct.pack('>f', ...). inf, -inf, and nan pass through unchanged.
5151
"""
5252
if not isinf(value) and not isnan(value):
5353
if value > <double>FLT_MAX or value < -<double>FLT_MAX:
@@ -58,9 +58,10 @@ cdef inline void _check_float_range(double value) except *:
5858
cdef inline void _check_int32_range(object value) except *:
5959
"""Raise OverflowError for values outside the signed int32 range.
6060
61-
Matches the behavior of struct.pack('>i', value), which raises
62-
struct.error for values outside [-2147483648, 2147483647]. The check
63-
must be done on the Python int *before* the C-level <int32_t> cast,
61+
Intentionally raises OverflowError (not struct.error) so callers
62+
can catch a single exception type. The accepted range matches
63+
struct.pack('>i', ...): [-2147483648, 2147483647]. The check must
64+
be done on the Python int *before* the C-level <int32_t> cast,
6465
which would silently truncate.
6566
"""
6667
if value > 2147483647 or value < -2147483648:
@@ -215,7 +216,12 @@ cdef class SerVectorType(Serializer):
215216
return self._serialize_generic(value, protocol_version)
216217

217218
cdef inline bytes _serialize_float(self, object values):
218-
"""Serialize a list of floats into a contiguous big-endian buffer."""
219+
"""Serialize a list of floats into a contiguous big-endian buffer.
220+
221+
Uses ``values[i]`` (``__getitem__``) intentionally rather than
222+
iteration so Cython can emit a tight indexed loop with minimal
223+
Python overhead. Callers must pass a sequence (list, tuple, etc.).
224+
"""
219225
cdef Py_ssize_t i
220226
cdef Py_ssize_t buf_size = self.vector_size * 4
221227
if buf_size == 0:
@@ -244,7 +250,12 @@ cdef class SerVectorType(Serializer):
244250
return result
245251

246252
cdef inline bytes _serialize_double(self, object values):
247-
"""Serialize a list of doubles into a contiguous big-endian buffer."""
253+
"""Serialize a list of doubles into a contiguous big-endian buffer.
254+
255+
Uses ``values[i]`` (``__getitem__``) intentionally rather than
256+
iteration so Cython can emit a tight indexed loop with minimal
257+
Python overhead. Callers must pass a sequence (list, tuple, etc.).
258+
"""
248259
cdef Py_ssize_t i
249260
cdef Py_ssize_t buf_size = self.vector_size * 8
250261
if buf_size == 0:
@@ -276,7 +287,12 @@ cdef class SerVectorType(Serializer):
276287
return result
277288

278289
cdef inline bytes _serialize_int32(self, object values):
279-
"""Serialize a list of int32 values into a contiguous big-endian buffer."""
290+
"""Serialize a list of int32 values into a contiguous big-endian buffer.
291+
292+
Uses ``values[i]`` (``__getitem__``) intentionally rather than
293+
iteration so Cython can emit a tight indexed loop with minimal
294+
Python overhead. Callers must pass a sequence (list, tuple, etc.).
295+
"""
280296
cdef Py_ssize_t i
281297
cdef Py_ssize_t buf_size = self.vector_size * 4
282298
if buf_size == 0:

tests/unit/test_serializers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
implementations, plus correct error behavior for edge cases.
2020
"""
2121

22-
import math
2322
import struct
2423
import unittest
2524

@@ -38,11 +37,10 @@
3837
UTF8Type,
3938
LongType,
4039
BooleanType,
41-
parse_casstype_args,
4240
)
4341

4442
# Import serializers only if Cython is available
45-
if HAVE_CYTHON:
43+
if HAVE_CYTHON or VERIFY_CYTHON:
4644
from cassandra.serializers import (
4745
Serializer,
4846
SerFloatType,

0 commit comments

Comments
 (0)