https://github.com/python/cpython/commit/39af0268ee92d4837cd0c2f80eaa2d5759a87f14
commit: 39af0268ee92d4837cd0c2f80eaa2d5759a87f14
branch: main
author: Yusuke Hayashi <[email protected]>
committer: ZeroIntensity <[email protected]>
date: 2026-11-22T16:42:32-05:00
summary:
gh-161840: Propagate `__len__` exceptions in ctypes array slice assignment
(GH-161859)
files:
A Misc/NEWS.d/next/Library/2026-11-12-08-38-09.gh-issue-161840.YcEVjO.rst
M Lib/test/test_ctypes/test_slicing.py
M Modules/_ctypes/_ctypes.c
diff --git a/Lib/test/test_ctypes/test_slicing.py
b/Lib/test/test_ctypes/test_slicing.py
index 66f9e530104cacc..b8c981ca2b17faa 100644
--- a/Lib/test/test_ctypes/test_slicing.py
+++ b/Lib/test/test_ctypes/test_slicing.py
@@ -63,6 +63,35 @@ def test_setslice_cint(self):
with self.assertRaises(ValueError):
a[:5] = range(32)
+ def test_setslice_len_error(self):
+ # gh-161840: exceptions from PySequence_Length() must propagate
+ # instead of being replaced with ValueError.
+ class CustomError(Exception):
+ pass
+
+ class BadLen:
+ def __len__(self):
+ raise CustomError("boom")
+
+ class BadValueErrorLen:
+ def __len__(self):
+ raise ValueError("custom value error")
+
+ a = (c_int * 3)(1, 2, 3)
+ with self.assertRaisesRegex(CustomError, "boom"):
+ a[0:1] = BadLen()
+ with self.assertRaisesRegex(CustomError, "boom"):
+ a[0:0] = BadLen()
+ with self.assertRaisesRegex(CustomError, "boom"):
+ a[::2] = BadLen()
+ with self.assertRaisesRegex(ValueError, "custom value error"):
+ a[0:1] = BadValueErrorLen()
+ with self.assertRaisesRegex(TypeError, "object of type 'int' has no
len"):
+ a[0:1] = 42
+ with self.assertRaisesRegex(TypeError, "dict is not a sequence"):
+ a[0:1] = {"x": 1}
+ self.assertEqual(a[:], [1, 2, 3])
+
def test_char_ptr(self):
s = b"abcdefghijklmnopqrstuvwxyz"
diff --git
a/Misc/NEWS.d/next/Library/2026-11-12-08-38-09.gh-issue-161840.YcEVjO.rst
b/Misc/NEWS.d/next/Library/2026-11-12-08-38-09.gh-issue-161840.YcEVjO.rst
new file mode 100644
index 000000000000000..f1e63af06fef87c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-12-08-38-09.gh-issue-161840.YcEVjO.rst
@@ -0,0 +1,3 @@
+Propagate exceptions raised while getting the length of the assigned value
+in :mod:`ctypes` array slice assignment instead of replacing them with a
+:exc:`ValueError`.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 14614dca1c02cee..301c28d2eb21154 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -5373,6 +5373,9 @@ Array_ass_subscript_impl(PyObject *myself, PyObject
*item, PyObject *value)
stop = start;
otherlen = PySequence_Length(value);
+ if (otherlen < 0) {
+ return -1;
+ }
if (otherlen != slicelen) {
PyErr_SetString(PyExc_ValueError,
"Can only assign sequence of same size");
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]