diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py index f67cdf5f7d4e945..0e29cc141892e88 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 43f416c141fd8ad..8ce6bd38882fc87 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5456,7 +5456,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; }