https://github.com/python/cpython/commit/8b0fd99e4d06802f27a829108201a694b903ad99
commit: 8b0fd99e4d06802f27a829108201a694b903ad99
branch: main
author: Jelle Zijlstra <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T10:19:03-07:00
summary:

gh-159739: Read resized ctypes scalars using their declared size (#159767)



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 f67cdf5f7d4e94..0e29cc141892e8 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 00000000000000..3574193b51f343
--- /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 605925a8eaaae7..ae49cbf3e57da7 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -5476,7 +5476,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]

Reply via email to