@@ -44,14 +44,19 @@ from cassandra.util import is_little_endian
4444# ---------------------------------------------------------------------------
4545
4646cdef inline void _check_float_range(double value) except * :
47- """ Raise OverflowError for finite values outside float32 range.
47+ """ Raise OverflowError for finite values that overflow float32.
48+
49+ Uses the same semantics as ``struct.pack('>f', value)``: cast the
50+ double to float and reject only if the result is ±inf while the
51+ input was finite. This correctly accepts values slightly above
52+ ``FLT_MAX`` that round down (e.g. 3.4028235e38).
4853
4954 Intentionally raises OverflowError (not struct.error) so callers
50- can catch a single exception type. The accepted range matches
51- struct.pack('>f', ...). inf, -inf, and nan pass through unchanged.
55+ can catch a single exception type. inf, -inf, and nan pass
56+ through unchanged.
5257 """
5358 if not isinf(value) and not isnan(value):
54- if value > < double > FLT_MAX or value < - < double > FLT_MAX :
59+ if isinf( < float > value) :
5560 raise OverflowError (
5661 " Value %r too large for float32 (max %r )" % (value, FLT_MAX))
5762
@@ -179,7 +184,7 @@ cdef class SerVectorType(Serializer):
179184 per-element Python serialization.
180185 """
181186
182- cdef int vector_size
187+ cdef Py_ssize_t vector_size
183188 cdef object subtype
184189 # 0 = generic, 1 = float, 2 = double, 3 = int32
185190 cdef int type_code
@@ -199,7 +204,11 @@ cdef class SerVectorType(Serializer):
199204 self .type_code = 0
200205
201206 cpdef bytes serialize(self , object value, int protocol_version):
202- cdef int v_length = len (value)
207+ # Normalize to tuple so indexing works for any iterable with __len__.
208+ # The Python VectorType.serialize() only requires len() + iteration,
209+ # so we must accept the same inputs.
210+ value = tuple (value)
211+ cdef Py_ssize_t v_length = len (value)
203212 if v_length != self .vector_size:
204213 raise ValueError (
205214 " Expected sequence of size %d for vector of type %s and "
@@ -219,9 +228,8 @@ cdef class SerVectorType(Serializer):
219228 cdef inline bytes _serialize_float(self , object values):
220229 """ Serialize a list of floats into a contiguous big-endian buffer.
221230
222- Uses ``values[i]`` (``__getitem__``) intentionally rather than
223- iteration so Cython can emit a tight indexed loop with minimal
224- Python overhead. Callers must pass a sequence (list, tuple, etc.).
231+ ``values`` is already a tuple (normalized in ``serialize()``), so
232+ indexing is always safe and fast.
225233 """
226234 cdef Py_ssize_t i
227235 cdef Py_ssize_t buf_size = self .vector_size * 4
@@ -255,9 +263,8 @@ cdef class SerVectorType(Serializer):
255263 cdef inline bytes _serialize_double(self , object values):
256264 """ Serialize a list of doubles into a contiguous big-endian buffer.
257265
258- Uses ``values[i]`` (``__getitem__``) intentionally rather than
259- iteration so Cython can emit a tight indexed loop with minimal
260- Python overhead. Callers must pass a sequence (list, tuple, etc.).
266+ ``values`` is already a tuple (normalized in ``serialize()``), so
267+ indexing is always safe and fast.
261268 """
262269 cdef Py_ssize_t i
263270 cdef Py_ssize_t buf_size = self .vector_size * 8
@@ -292,9 +299,8 @@ cdef class SerVectorType(Serializer):
292299 cdef inline bytes _serialize_int32(self , object values):
293300 """ Serialize a list of int32 values into a contiguous big-endian buffer.
294301
295- Uses ``values[i]`` (``__getitem__``) intentionally rather than
296- iteration so Cython can emit a tight indexed loop with minimal
297- Python overhead. Callers must pass a sequence (list, tuple, etc.).
302+ ``values`` is already a tuple (normalized in ``serialize()``), so
303+ indexing is always safe and fast.
298304 """
299305 cdef Py_ssize_t i
300306 cdef Py_ssize_t buf_size = self .vector_size * 4
@@ -304,12 +310,14 @@ cdef class SerVectorType(Serializer):
304310 cdef object result = PyBytes_FromStringAndSize(NULL , buf_size)
305311 cdef char * buf = PyBytes_AS_STRING(result)
306312 cdef int32_t val
313+ cdef object item
307314 cdef char * src
308315 cdef char * dst
309316
310317 for i in range (self .vector_size):
311- _check_int32_range(values[i])
312- val = < int32_t> values[i]
318+ item = values[i]
319+ _check_int32_range(item)
320+ val = < int32_t> item
313321 src = < char * > & val
314322 dst = buf + i * 4
315323
0 commit comments