Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-124074: Add _Py_NewImmortalRef() function #124076

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ extern void _PyLong_FiniTypes(PyInterpreterState *interp);
# error "_PY_NSMALLPOSINTS must be greater than or equal to 257"
#endif

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

// Return a reference to the immortal one singleton.
// Return a borrowed reference to the immortal one singleton.
// The function cannot return NULL.
static inline PyObject* _PyLong_GetOne(void)
{ return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+1]; }
Expand Down
9 changes: 9 additions & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,15 @@ PyAPI_DATA(int) _Py_SwappedOp[];

extern void _Py_GetConstant_Init(void);


// Similar to Py_NewRef() but for immortal objects.
// obj must be immortal.
static inline PyObject* _Py_NewImmortalRef(PyObject *obj)
Comment on lines +878 to +880
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 7:

Suggested change
// Similar to Py_NewRef() but for immortal objects.
// obj must be immortal.
static inline PyObject* _Py_NewImmortalRef(PyObject *obj)
// Similar to Py_NewRef() but for immortal objects.
// obj must be immortal.
static inline PyObject *
_Py_NewImmortalRef(PyObject *obj)

{
assert(_Py_IsImmortalLoose(obj));
return obj;
}

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ bool_repr(PyObject *self)

PyObject *PyBool_FromLong(long ok)
{
return ok ? Py_True : Py_False;
PyObject *result = ok ? Py_True : Py_False;
return _Py_NewImmortalRef(result);
}

/* We define bool_new to always return either Py_True or Py_False */
Expand Down
2 changes: 1 addition & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3025,7 +3025,7 @@ PyObject*
Py_GetConstant(unsigned int constant_id)
{
if (constant_id < Py_ARRAY_LENGTH(constants)) {
return constants[constant_id];
return _Py_NewImmortalRef(constants[constant_id]);
}
else {
PyErr_BadInternalCall();
Expand Down
Loading