https://github.com/python/cpython/commit/5c50b6a1a31d9ce2a74fd40b94828eebf4a49e19
commit: 5c50b6a1a31d9ce2a74fd40b94828eebf4a49e19
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-23T12:33:22-07:00
summary:

[3.15] gh-159739: Keep descriptor metadata dictionaries alive during insertion 
(GH-160193) (#160382)



The classmethod and staticmethod metadata setters dropped their reference to 
the descriptor dictionary before inserting the new value. A key comparison or 
value finalizer can replace the dictionary during insertion, leaving the 
operation using released memory. Keep the dictionary alive until insertion 
finishes. Backport to 3.15 and 3.14.
(cherry picked from commit a076a55ad2a318cd8b7010d85ccf21ccd0a98555)

Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.CbTE4N.rst
M Objects/funcobject.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.CbTE4N.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.CbTE4N.rst
new file mode 100644
index 00000000000000..20072791808dd1
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.CbTE4N.rst
@@ -0,0 +1 @@
+Fix a possible crash when assigning metadata to :class:`classmethod` or 
:class:`staticmethod` objects.
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 0481adadf668f8..9cac2e77845a4f 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -1465,8 +1465,9 @@ descriptor_set_wrapped_attribute(PyObject *oobj, PyObject 
*name, PyObject *value
         return 0;
     }
     else {
+        int result = PyDict_SetItem(dict, name, value);
         Py_DECREF(dict);
-        return PyDict_SetItem(dict, name, value);
+        return result;
     }
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to