Skip to content

Commit 3df9e31

Browse files
[3.13] gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (#144108) (#144245)
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (#144108) (cherry picked from commit 8f45925) Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>
1 parent d6f4fb8 commit 3df9e31

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Lib/test/test_ctypes/test_pointers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ class C(Structure):
235235
ptr.set_type(c_int)
236236
self.assertIs(ptr._type_, c_int)
237237

238+
def test_pointer_proto_missing_argtypes_error(self):
239+
class BadType(ctypes._Pointer):
240+
# _type_ is intentionally missing
241+
pass
242+
243+
func = ctypes.pythonapi.Py_GetVersion
244+
func.argtypes = (BadType,)
245+
246+
with self.assertRaises(ctypes.ArgumentError):
247+
func(object())
248+
238249

239250
if __name__ == '__main__':
240251
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a crash in ctypes when using a deprecated ``POINTER(str)`` type in
2+
``argtypes``. Instead of aborting, ctypes now raises a proper Python
3+
exception when the pointer target type is unresolved.

Modules/_ctypes/_ctypes.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,13 @@ PyCPointerType_from_param_impl(PyObject *type, PyTypeObject *cls,
13381338
/* If we expect POINTER(<type>), but receive a <type> instance, accept
13391339
it by calling byref(<type>).
13401340
*/
1341-
assert(typeinfo->proto);
1341+
if (typeinfo->proto == NULL) {
1342+
PyErr_SetString(
1343+
PyExc_TypeError,
1344+
"cannot convert argument: POINTER _type_ type is not set"
1345+
);
1346+
return NULL;
1347+
}
13421348
switch (PyObject_IsInstance(value, typeinfo->proto)) {
13431349
case 1:
13441350
Py_INCREF(value); /* _byref steals a refcount */

0 commit comments

Comments
 (0)