https://github.com/python/cpython/commit/8f459255eba2b6639f1912e5c5e318a7cdafada1
commit: 8f459255eba2b6639f1912e5c5e318a7cdafada1
branch: main
author: VanshAgarwal24036 <[email protected]>
committer: vstinner <[email protected]>
date: 2026-01-26T13:27:42+01:00
summary:

gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (#144108)

Co-authored-by: Victor Stinner <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-01-21-19-39-07.gh-issue-144100.hLMZ8Y.rst
M Lib/test/test_ctypes/test_pointers.py
M Modules/_ctypes/_ctypes.c

diff --git a/Lib/test/test_ctypes/test_pointers.py 
b/Lib/test/test_ctypes/test_pointers.py
index a8d243a45de0f4..771cc8fbe0ec93 100644
--- a/Lib/test/test_ctypes/test_pointers.py
+++ b/Lib/test/test_ctypes/test_pointers.py
@@ -403,6 +403,16 @@ class Cls(Structure):
         self.assertEqual(len(ws_typ), 0, ws_typ)
         self.assertEqual(len(ws_ptr), 0, ws_ptr)
 
+    def test_pointer_proto_missing_argtypes_error(self):
+        class BadType(ctypes._Pointer):
+            # _type_ is intentionally missing
+            pass
+
+        func = ctypes.pythonapi.Py_GetVersion
+        func.argtypes = (BadType,)
+
+        with self.assertRaises(ctypes.ArgumentError):
+            func(object())
 
 class PointerTypeCacheTestCase(unittest.TestCase):
     # dummy tests to check warnings and base behavior
diff --git 
a/Misc/NEWS.d/next/Library/2026-01-21-19-39-07.gh-issue-144100.hLMZ8Y.rst 
b/Misc/NEWS.d/next/Library/2026-01-21-19-39-07.gh-issue-144100.hLMZ8Y.rst
new file mode 100644
index 00000000000000..7093b753141fb8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-01-21-19-39-07.gh-issue-144100.hLMZ8Y.rst
@@ -0,0 +1,3 @@
+Fixed a crash in ctypes when using a deprecated ``POINTER(str)`` type in
+``argtypes``. Instead of aborting, ctypes now raises a proper Python
+exception when the pointer target type is unresolved.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 563e95a762599b..6d9193af657e2a 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -1419,7 +1419,13 @@ PyCPointerType_from_param_impl(PyObject *type, 
PyTypeObject *cls,
     /* If we expect POINTER(<type>), but receive a <type> instance, accept
        it by calling byref(<type>).
     */
-    assert(typeinfo->proto);
+    if (typeinfo->proto == NULL) {
+        PyErr_SetString(
+            PyExc_TypeError,
+            "cannot convert argument: POINTER _type_ type is not set"
+        );
+        return NULL;
+    }
     switch (PyObject_IsInstance(value, typeinfo->proto)) {
     case 1:
         Py_INCREF(value); /* _byref steals a refcount */

_______________________________________________
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