gh-134989: Fix Py_RETURN_NONE in the limited C API (GH-135165) · python/cpython@9258f3d · GitHub
Skip to content

Commit 9258f3d

Browse files
authored
gh-134989: Fix Py_RETURN_NONE in the limited C API (GH-135165)
Fix Py_RETURN_NONE, Py_RETURN_TRUE and Py_RETURN_FALSE macros in the limited C API 3.11 and older: Don't treat Py_None, Py_True and Py_False as immortal.
1 parent 4b44b34 commit 9258f3d

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

Include/boolobject.h

Lines changed: 10 additions & 3 deletions

Include/object.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,13 @@ PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
660660
PyAPI_FUNC(int) Py_IsNone(PyObject *x);
661661
#define Py_IsNone(x) Py_Is((x), Py_None)
662662

663-
/* Macro for returning Py_None from a function */
664-
#define Py_RETURN_NONE return Py_None
663+
/* Macro for returning Py_None from a function.
664+
* Only treat Py_None as immortal in the limited C API 3.12 and newer. */
665+
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000
666+
# define Py_RETURN_NONE return Py_NewRef(Py_None)
667+
#else
668+
# define Py_RETURN_NONE return Py_None
669+
#endif
665670

666671
/*
667672
Py_NotImplemented is a singleton used to signal that an operation is
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)