Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (GH-144108
)

(cherry picked from commit 8f45925)

Co-authored-by: VanshAgarwal24036 <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
  • Loading branch information
2 people authored and miss-islington committed Jan 26, 2026
commit 5d3553fd9b4c0521900aa53d047e3e543f358e7b
10 changes: 10 additions & 0 deletions Lib/test/test_ctypes/test_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 7 additions & 1 deletion Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,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 */
Expand Down
Loading