Skip to content

Commit 6eb358c

Browse files
committed
pythongh-124074: Add _Py_NewImmortalRef() function
1 parent b46c65e commit 6eb358c

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

Include/internal/pycore_long.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ extern void _PyLong_FiniTypes(PyInterpreterState *interp);
6363
# error "_PY_NSMALLPOSINTS must be greater than or equal to 257"
6464
#endif
6565

66-
// Return a reference to the immortal zero singleton.
66+
// Return a borrowed reference to the immortal zero singleton.
6767
// The function cannot return NULL.
6868
static inline PyObject* _PyLong_GetZero(void)
6969
{ return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS]; }
7070

71-
// Return a reference to the immortal one singleton.
71+
// Return a borrowed reference to the immortal one singleton.
7272
// The function cannot return NULL.
7373
static inline PyObject* _PyLong_GetOne(void)
7474
{ return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+1]; }

Include/internal/pycore_object.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,15 @@ PyAPI_DATA(int) _Py_SwappedOp[];
874874

875875
extern void _Py_GetConstant_Init(void);
876876

877+
878+
// Similar to Py_NewRef() but for immortal objects.
879+
// obj must be immortal.
880+
static inline PyObject* _Py_NewImmortalRef(PyObject *obj)
881+
{
882+
assert(_Py_IsImmortalLoose(obj));
883+
return obj;
884+
}
885+
877886
#ifdef __cplusplus
878887
}
879888
#endif

Objects/boolobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ bool_repr(PyObject *self)
2020

2121
PyObject *PyBool_FromLong(long ok)
2222
{
23-
return ok ? Py_True : Py_False;
23+
PyObject *result = ok ? Py_True : Py_False;
24+
return _Py_NewImmortalRef(result);
2425
}
2526

2627
/* We define bool_new to always return either Py_True or Py_False */

Objects/object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3025,7 +3025,7 @@ PyObject*
30253025
Py_GetConstant(unsigned int constant_id)
30263026
{
30273027
if (constant_id < Py_ARRAY_LENGTH(constants)) {
3028-
return constants[constant_id];
3028+
return _Py_NewImmortalRef(constants[constant_id]);
30293029
}
30303030
else {
30313031
PyErr_BadInternalCall();

0 commit comments

Comments
 (0)