https://github.com/python/cpython/commit/ec5f5418f9165d9e3f0841e68f7882bf64c492fb
commit: ec5f5418f9165d9e3f0841e68f7882bf64c492fb
branch: 3.13
author: Victor Stinner <[email protected]>
committer: encukou <[email protected]>
date: 2025-10-27T14:43:59+01:00
summary:

[3.13] gh-140487: Fix Py_RETURN_NOTIMPLEMENTED in limited C API 3.11 
(GH-140636) (GH-140670)

Py_RETURN_NONE, Py_RETURN_TRUE and Py_RETURN_FALSE have already been
fixed by commit 9258f3da9175134d03f2c8c7c7eed223802ad945 (issue gh-134989).

(cherry picked from commit c6364775236e3c634c3393c7f50fece50611245f)

files:
A Misc/NEWS.d/next/C API/2025-10-26-16-45-06.gh-issue-140487.fGOqss.rst
M Include/object.h

diff --git a/Include/object.h b/Include/object.h
index 0266d25230a2b4..e59f78789844e7 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -1130,8 +1130,13 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't 
use this directly */
 #  define Py_NotImplemented (&_Py_NotImplementedStruct)
 #endif
 
-/* Macro for returning Py_NotImplemented from a function */
-#define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented
+/* Macro for returning Py_NotImplemented from a function. Only treat
+ * Py_NotImplemented as immortal in the limited C API 3.12 and newer. */
+#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000
+#  define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
+#else
+#  define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented
+#endif
 
 /* Rich comparison opcodes */
 #define Py_LT 0
diff --git a/Misc/NEWS.d/next/C 
API/2025-10-26-16-45-06.gh-issue-140487.fGOqss.rst b/Misc/NEWS.d/next/C 
API/2025-10-26-16-45-06.gh-issue-140487.fGOqss.rst
new file mode 100644
index 00000000000000..16b0d9d4084ba0
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2025-10-26-16-45-06.gh-issue-140487.fGOqss.rst     
@@ -0,0 +1,2 @@
+Fix :c:macro:`Py_RETURN_NOTIMPLEMENTED` in limited C API 3.11 and older:
+don't treat ``Py_NotImplemented`` as immortal. Patch by Victor Stinner.

_______________________________________________
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