[Python-checkins] GH-81061: Fix refcount issue when returning `None` from a `ctypes.py_object` callback (GH-13364)
miss-islington
webhook-mailer at python.org
Mon Jan 9 11:06:27 EST 2023
https://github.com/python/cpython/commit/2d1128e9eb9be56e3cd105f4a38564ac4c01e68e
commit: 2d1128e9eb9be56e3cd105f4a38564ac4c01e68e
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-01-09T08:06:17-08:00
summary:
GH-81061: Fix refcount issue when returning `None` from a `ctypes.py_object` callback (GH-13364)
(cherry picked from commit 837ba052672d1a5f85a46c1b6d4b6e7d192af6f3)
Co-authored-by: dgelessus <dgelessus at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2019-05-13-11-37-30.bpo-36880.ZgBgH0.rst
M Lib/ctypes/test/test_refcounts.py
M Modules/_ctypes/callbacks.c
diff --git a/Lib/ctypes/test/test_refcounts.py b/Lib/ctypes/test/test_refcounts.py
index f2edfa6400ef..48958cd2a601 100644
--- a/Lib/ctypes/test/test_refcounts.py
+++ b/Lib/ctypes/test/test_refcounts.py
@@ -97,5 +97,20 @@ 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):
+ # bpo-36880: test that returning None from a py_object callback
+ # does not decrement the refcount of None.
+
+ for FUNCTYPE in (ctypes.CFUNCTYPE, ctypes.PYFUNCTYPE):
+ with self.subTest(FUNCTYPE=FUNCTYPE):
+ @FUNCTYPE(ctypes.py_object)
+ def func():
+ return None
+
+ # Check that calling func does not affect None's refcount.
+ for _ in range(10000):
+ func()
+
if __name__ == '__main__':
unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2019-05-13-11-37-30.bpo-36880.ZgBgH0.rst b/Misc/NEWS.d/next/Library/2019-05-13-11-37-30.bpo-36880.ZgBgH0.rst
new file mode 100644
index 000000000000..f65323813d05
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-05-13-11-37-30.bpo-36880.ZgBgH0.rst
@@ -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.
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index c28762d49ba4..b46067bf2fa6 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -276,15 +276,14 @@ static void _CallPythonObject(void *mem,
"of ctypes callback function",
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))
- {
+ if (keep == Py_None) {
+ /* Nothing to keep */
+ Py_DECREF(keep);
+ }
+ else if (PyErr_WarnEx(PyExc_RuntimeWarning,
+ "memory leak in callback function.",
+ 1) == -1) {
_PyErr_WriteUnraisableMsg("on converting result "
"of ctypes callback function",
callable);
More information about the Python-checkins
mailing list