Skip to content

Commit ee511ea

Browse files
committed
(improvement) query, serializers: address PR review feedback
- Replace malloc/free with PyBytes_FromStringAndSize(NULL, ...) pattern in vector fast-paths, eliminating extra buffer copy and malloc(0) edge case - Change _check_int32_range to raise OverflowError instead of struct.error, consistent with _check_float_range - Remove unused imports (struct, malloc, free) - Differentiate error messages in _raise_bind_serialize_error: 'invalid type' for TypeError vs 'value out of range' for OverflowError/struct.error - Replace unused loop variable with while loop in UNSET_VALUE fill - Expand __getitem__ docstrings explaining performance rationale - Fix copyright header in test_parameter_binding.py
1 parent 941424e commit ee511ea

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

cassandra/query.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ def _serializers(self):
489489
490490
The column_encryption_policy check is performed on every access (not
491491
cached) so that serializers are correctly bypassed if a policy is set
492-
after construction.
492+
after construction. This means the cache never goes stale: once a CE
493+
policy is present, we always return None and fall through to the
494+
encryption-aware bind path.
493495
"""
494496
if self.column_encryption_policy:
495497
return None
@@ -573,8 +575,12 @@ def _raise_bind_serialize_error(col_spec, value, exc):
573575
without wrapping.
574576
"""
575577
actual_type = type(value)
576-
message = ('Received an argument of invalid type for column "%s". '
577-
'Expected: %s, Got: %s; (%s)' % (col_spec.name, col_spec.type, actual_type, exc))
578+
if isinstance(exc, (OverflowError, struct.error)):
579+
reason = 'value out of range'
580+
else:
581+
reason = 'invalid type'
582+
message = ('Received an argument with %s for column "%s". '
583+
'Expected: %s, Got: %s; (%s)' % (reason, col_spec.name, col_spec.type, actual_type, exc))
578584
raise TypeError(message) from exc
579585

580586

@@ -752,7 +758,7 @@ def bind(self, values):
752758

753759
if proto_version >= 4:
754760
# Fill remaining unbound columns with UNSET_VALUE (v4+ feature).
755-
for i in range(idx, col_meta_len):
761+
while idx < col_meta_len:
756762
idx = self._append_unset_value(idx)
757763
elif idx < col_meta_len:
758764
# Pre-v4: trim trailing unused slots (no UNSET_VALUE support)

tests/unit/test_parameter_binding.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright DataStax, Inc.
1+
# Copyright ScyllaDB, Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -326,8 +326,9 @@ def test_cython_path_type_error_wrapped(self):
326326
assert 'Int32Type' in msg
327327

328328
def test_plain_path_overflow_error_wrapped(self):
329-
"""Out-of-range int in the plain Python path raises struct.error (caught
330-
alongside OverflowError) and is wrapped with column context."""
329+
"""Out-of-range int in the plain Python path raises OverflowError (from
330+
the Cython serializer) or struct.error (from the pure-Python fallback)
331+
and is wrapped with column context."""
331332
column_metadata = [ColumnMetadata('keyspace', 'cf', 'v0', Int32Type)]
332333
# Force the plain Python path (no Cython serializers)
333334
prepared = self._make_prepared(column_metadata, serializers=None)

0 commit comments

Comments
 (0)