summaryrefslogtreecommitdiffstats
path: root/Include/internal/pycore_long.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-10-26 23:00:03 (GMT)
committerGitHub <noreply@github.com>2020-10-26 23:00:03 (GMT)
commit8e3b9f92835654943bb59d9658bb52e1b0f40a22 (patch)
treed22c0d39d03dcfc082dc01b8364cb6e5fe850fef /Include/internal/pycore_long.h
parentbca701403253379409dece03053dbd739c0bd059 (diff)
downloadcpython-8e3b9f92835654943bb59d9658bb52e1b0f40a22.zip
cpython-8e3b9f92835654943bb59d9658bb52e1b0f40a22.tar.gz
cpython-8e3b9f92835654943bb59d9658bb52e1b0f40a22.tar.bz2
bpo-42161: Add _PyLong_GetZero() and _PyLong_GetOne() (GH-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.
Diffstat (limited to 'Include/internal/pycore_long.h')
-rw-r--r--Include/internal/pycore_long.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h
new file mode 100644
index 0000000..ec95786
--- /dev/null
+++ b/Include/internal/pycore_long.h
@@ -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 */