forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpo-42161: Add _PyLong_GetZero() and _PyLong_GetOne() (pythonGH-22993)
Add _PyLong_GetZero() and _PyLong_GetOne() functions and a new internal pycore_long.h header file. Python cannot be built without small integer singletons anymore.
- Loading branch information
Showing
6 changed files
with
67 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef Py_INTERNAL_LONG_H | ||
#define Py_INTERNAL_LONG_H | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_BUILD_CORE | ||
# error "this header requires Py_BUILD_CORE define" | ||
#endif | ||
|
||
#include "pycore_interp.h" // PyInterpreterState.small_ints | ||
#include "pycore_pystate.h" // _PyThreadState_GET() | ||
|
||
// Don't call this function but _PyLong_GetZero() and _PyLong_GetOne() | ||
static inline PyObject* __PyLong_GetSmallInt_internal(int value) | ||
{ | ||
PyThreadState *tstate = _PyThreadState_GET(); | ||
#ifdef Py_DEBUG | ||
_Py_EnsureTstateNotNULL(tstate); | ||
#endif | ||
assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS); | ||
size_t index = _PY_NSMALLNEGINTS + value; | ||
PyObject *obj = (PyObject*)tstate->interp->small_ints[index]; | ||
// _PyLong_GetZero() and _PyLong_GetOne() must not be called | ||
// before _PyLong_Init() nor after _PyLong_Fini() | ||
assert(obj != NULL); | ||
return obj; | ||
} | ||
|
||
// Return a borrowed reference to the zero singleton. | ||
// The function cannot return NULL. | ||
static inline PyObject* _PyLong_GetZero(void) | ||
{ return __PyLong_GetSmallInt_internal(0); } | ||
|
||
// Return a borrowed reference to the one singleton. | ||
// The function cannot return NULL. | ||
static inline PyObject* _PyLong_GetOne(void) | ||
{ return __PyLong_GetSmallInt_internal(1); } | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif /* !Py_INTERNAL_LONG_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters