Skip to content

Commit 922dc2c

Browse files
authored
[3.13] gh-134989: Fix Py_RETURN_NONE in the limited C API (GH-135165) (#135182)
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. (cherry picked from commit 9258f3d)
1 parent e2a9a3f commit 922dc2c

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Include/boolobject.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,16 @@ PyAPI_FUNC(int) Py_IsTrue(PyObject *x);
3434
PyAPI_FUNC(int) Py_IsFalse(PyObject *x);
3535
#define Py_IsFalse(x) Py_Is((x), Py_False)
3636

37-
/* Macros for returning Py_True or Py_False, respectively */
38-
#define Py_RETURN_TRUE return Py_True
39-
#define Py_RETURN_FALSE return Py_False
37+
/* Macros for returning Py_True or Py_False, respectively.
38+
* Only treat Py_True and Py_False as immortal in the limited C API 3.12
39+
* and newer. */
40+
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000
41+
# define Py_RETURN_TRUE return Py_NewRef(Py_True)
42+
# define Py_RETURN_FALSE return Py_NewRef(Py_False)
43+
#else
44+
# define Py_RETURN_TRUE return Py_True
45+
# define Py_RETURN_FALSE return Py_False
46+
#endif
4047

4148
/* Function to return a bool from a C long */
4249
PyAPI_FUNC(PyObject *) PyBool_FromLong(long);

Include/object.h

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

1113-
/* Macro for returning Py_None from a function */
1114-
#define Py_RETURN_NONE return Py_None
1113+
/* Macro for returning Py_None from a function.
1114+
* Only treat Py_None as immortal in the limited C API 3.12 and newer. */
1115+
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000
1116+
# define Py_RETURN_NONE return Py_NewRef(Py_None)
1117+
#else
1118+
# define Py_RETURN_NONE return Py_None
1119+
#endif
11151120

11161121
/*
11171122
Py_NotImplemented is a singleton used to signal that an operation is
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``Py_RETURN_NONE``, ``Py_RETURN_TRUE`` and ``Py_RETURN_FALSE`` macros in
2+
the limited C API 3.11 and older: don't treat ``Py_None``, ``Py_True`` and
3+
``Py_False`` as immortal. Patch by Victor Stinner.

0 commit comments

Comments
 (0)