]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (#144108) (...
authorVictor Stinner <vstinner@python.org>
Tue, 27 Jan 2026 11:59:55 +0000 (12:59 +0100)
committerGitHub <noreply@github.com>
Tue, 27 Jan 2026 11:59:55 +0000 (12:59 +0100)
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (#144108)

(cherry picked from commit 8f459255eba2b6639f1912e5c5e318a7cdafada1)

Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>
Lib/test/test_ctypes/test_pointers.py
Misc/NEWS.d/next/Library/2026-01-21-19-39-07.gh-issue-144100.hLMZ8Y.rst [new file with mode: 0644]
Modules/_ctypes/_ctypes.c

index ed4541335dfca43e63c986a59710910bc2fc80b8..3e904e17fab8066b6795b7a3426bd91a78421e2b 100644 (file)
@@ -235,6 +235,17 @@ class PointersTestCase(unittest.TestCase):
         ptr.set_type(c_int)
         self.assertIs(ptr._type_, c_int)
 
+    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())
+
 
 if __name__ == '__main__':
     unittest.main()
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 (file)
index 0000000..7093b75
--- /dev/null
@@ -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.
index 7419d629399e2c9d556856cb4560eee41562ae95..aac812813d95432d874bdef0e71fe6ddd263e62b 100644 (file)
@@ -1338,7 +1338,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 */