summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/pylifecycle.c1
-rw-r--r--Python/pystate.c37
2 files changed, 38 insertions, 0 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index c0f41b3..90f8551 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -344,6 +344,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
_PyRandom_Init();
+ _PyInterpreterState_Init();
interp = PyInterpreterState_New();
if (interp == NULL)
Py_FatalError("Py_Initialize: can't make first interpreter");
diff --git a/Python/pystate.c b/Python/pystate.c
index 52899f1..99a579a 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -65,6 +65,23 @@ PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
static void _PyGILState_NoteThreadState(PyThreadState* tstate);
#endif
+/* _next_interp_id is an auto-numbered sequence of small integers.
+ It gets initialized in _PyInterpreterState_Init(), which is called
+ in Py_Initialize(), and used in PyInterpreterState_New(). A negative
+ interpreter ID indicates an error occurred. The main interpreter
+ will always have an ID of 0. Overflow results in a RuntimeError.
+ If that becomes a problem later then we can adjust, e.g. by using
+ a Python int.
+
+ We initialize this to -1 so that the pre-Py_Initialize() value
+ results in an error. */
+static int64_t _next_interp_id = -1;
+
+void
+_PyInterpreterState_Init(void)
+{
+ _next_interp_id = 0;
+}
PyInterpreterState *
PyInterpreterState_New(void)
@@ -103,6 +120,15 @@ PyInterpreterState_New(void)
HEAD_LOCK();
interp->next = interp_head;
interp_head = interp;
+ if (_next_interp_id < 0) {
+ /* overflow or Py_Initialize() not called! */
+ PyErr_SetString(PyExc_RuntimeError,
+ "failed to get an interpreter ID");
+ interp = NULL;
+ } else {
+ interp->id = _next_interp_id;
+ _next_interp_id += 1;
+ }
HEAD_UNLOCK();
}
@@ -170,6 +196,17 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
}
+int64_t
+PyInterpreterState_GetID(PyInterpreterState *interp)
+{
+ if (interp == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
+ return -1;
+ }
+ return interp->id;
+}
+
+
/* Default implementation for _PyThreadState_GetFrame */
static struct _frame *
threadstate_getframe(PyThreadState *self)