https://github.com/python/cpython/commit/37434b1b06260076f6e33960e5f493bdb6aaf0bd
commit: 37434b1b06260076f6e33960e5f493bdb6aaf0bd
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T11:12:58-07:00
summary:

[3.15] gh-159739: Read resized ctypes scalars using their declared size 
(GH-159767) (#159898)



(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 b7df08f6079cd31..c497c09cf2b7271 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 3882b9a5ddff3c6..f3c8a91004cb424 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]

Reply via email to