8000 GH-81061: Fix refcount issue when returning `None` from a `ctypes.py_object` callback by dgelessus · Pull Request #13364 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-81061: Fix refcount issue when returning None from a ctypes.py_object callback #13364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions Lib/ctypes/test/test_refcounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,31 @@ def func(a, b):
f(1, 2)
self.assertEqual(sys.getrefcount(ctypes.c_int), a)

@support.refcount_test
def test_callback_py_object_none_return(self):
"""Test that returning ``None`` from a ``py_object`` callback
does not affect ``None``'s refcount (bpo-36880)."""

import sys

for FUNCTYPE in (ctypes.CFUNCTYPE, ctypes.PYFUNCTYPE):
with self.subTest(FUNCTYPE=FUNCTYPE):
proto = FUNCTYPE(ctypes.py_object)
@proto
def func():
return None

# Check that calling func does not affect None's refcount.
none_refcount = sys.getrefcount(None)
# Because None's refcount can also change for other reasons,
# we call func in a loop to ensure that any effects on None's
# refcount are clearly visible.
for _ in range(10000):
func()
# Allow for small variations in None's refcount from other
# sources.
self.assertAlmostEqual(
sys.getrefcount(None), none_refcount, delta=50)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a reference counting issue when a :mod:`ctypes` callback with return
type :class:`~ctypes.py_object` returns ``None``, which could cause crashes.
15 changes: 8 additions & 7 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,16 @@ if (x == NULL) _PyTraceback_Add(what, "_ctypes/callbacks.c", __LINE__ - 1), PyEr
be the result. EXCEPT when restype is py_object - Python
itself knows how to manage the refcount of these objects.
*/
if (keep == NULL) /* Could not convert callback result. */
if (keep == NULL) { /* Could not convert callback result. */
PyErr_WriteUnraisable(callable);
else if (keep == Py_None) /* Nothing to keep */
Py_DECREF(keep);
else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) {
if (-1 == PyErr_WarnEx(PyExc_RuntimeWarning,
"memory leak in callback function.",
1))
} else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) {
if (keep == Py_None) { /* Nothing to keep */
Py_DECREF(keep);
} else if (PyErr_WarnEx(PyExc_RuntimeWarning,
"memory leak in callback function.",
1) == -1) {
PyErr_WriteUnraisable(callable);
}
}
}
Py_XDECREF(result);
Expand Down
0