https://github.com/python/cpython/commit/7354d53bb65022e968da0633aed9700b6abf6db1 commit: 7354d53bb65022e968da0633aed9700b6abf6db1 branch: 3.14 author: Miss Islington (bot) <[email protected]> committer: JelleZijlstra <[email protected]> date: 2026-10-20T11:13:03-07:00 summary:
[3.14] gh-159739: Read resized ctypes scalars using their declared size (GH-159767) (#159899) (cherry picked from commit 8b0fd99e4d06802f27a829108201a694b903ad99) Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]> files: A Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.08caa7.rst M Lib/test/test_ctypes/test_numbers.py M Modules/_ctypes/_ctypes.c diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py index c57c58eb002328e..dcbc7970458f033 100644 --- a/Lib/test/test_ctypes/test_numbers.py +++ b/Lib/test/test_ctypes/test_numbers.py @@ -69,6 +69,11 @@ def test_default_init(self): for t in signed_types + unsigned_types + float_types: self.assertEqual(t().value, 0) + def test_resized_scalar_value(self): + value = c_int(17) + ctypes.resize(value, 65536) + self.assertEqual(value.value, 17) + def test_unsigned_values(self): # the value given to the constructor is available # as the 'value' attribute diff --git a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.08caa7.rst b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.08caa7.rst new file mode 100644 index 000000000000000..3574193b51f343e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.08caa7.rst @@ -0,0 +1,2 @@ +Fix incorrect :mod:`ctypes` scalar values after :func:`ctypes.resize` enlarges +the object. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index ed767832258874b..ac0ffb00e785c31 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5477,7 +5477,7 @@ _ctypes_Simple_value_get_impl(CDataObject *self) assert(info); /* Cannot be NULL for CDataObject instances */ assert(info->getfunc); PyObject *res; - res = info->getfunc(self->b_ptr, self->b_size); + res = info->getfunc(self->b_ptr, info->size); return res; } _______________________________________________ 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]
