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
12 changes: 10 additions & 2 deletions Lib/test/test_ctypes/test_as_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,16 @@ class A:

a = A()
a._as_parameter_ = a
with self.assertRaises(RecursionError):
c_int.from_param(a)
for c_type in (
ctypes.c_wchar_p,
ctypes.c_char_p,
ctypes.c_void_p,
ctypes.c_int, # PyCSimpleType
POINT, # CDataType
):
with self.subTest(c_type=c_type):
with self.assertRaises(RecursionError):
c_type.from_param(a)


class AsParamWrapper:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Detect recursive calls in ctypes ``_as_parameter_`` handling.
Patch by Victor Stinner.
22 changes: 21 additions & 1 deletion Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1054,8 +1054,13 @@ CDataType_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = CDataType_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -1842,8 +1847,13 @@ c_wchar_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = c_wchar_p_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -1926,8 +1936,13 @@ c_char_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = c_char_p_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -2078,8 +2093,13 @@ c_void_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = c_void_p_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -2435,9 +2455,9 @@ PyCSimpleType_from_param_impl(PyObject *type, PyTypeObject *cls,
return NULL;
}
value = PyCSimpleType_from_param_impl(type, cls, as_parameter);
_Py_LeaveRecursiveCall();
Py_DECREF(as_parameter);
Py_XDECREF(exc);
_Py_LeaveRecursiveCall();
return value;
}
if (exc) {
Expand Down
Loading