Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
_PyInterpreterState_next_id -> _PyInterpreterState_Init().
  • Loading branch information
ericsnowcurrently committed May 22, 2017
commit 6c6a6c597e10aecb5fa25e7622847899d2e6db52
10 changes: 3 additions & 7 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ struct _frame; /* Forward declaration for PyFrameObject. */
#ifdef Py_LIMITED_API
typedef struct _is PyInterpreterState;
#else
/* This is initialized to 0 in Py_Initialize(). 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. */
PyAPI_DATA(int_fast64_t) _PyInterpreterState_next_id;

typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);

typedef struct _is {
Expand Down Expand Up @@ -163,6 +156,9 @@ typedef struct _ts {
#endif


#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyInterpreterState_Init(void);
#endif /* !Py_LIMITED_API */
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
Expand Down
2 changes: 1 addition & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)

_PyRandom_Init();

_PyInterpreterState_next_id = 0;
_PyInterpreterState_Init();
interp = PyInterpreterState_New();
if (interp == NULL)
Py_FatalError("Py_Initialize: can't make first interpreter");
Expand Down
25 changes: 19 additions & 6 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,23 @@ PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
static void _PyGILState_NoteThreadState(PyThreadState* tstate);
#endif


/* We initialize this to -1 so that the pre-Py_Initialize() value
/* _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. */
int_fast64_t _PyInterpreterState_next_id = -1;
static int_fast64_t _next_interp_id = -1;

void
_PyInterpreterState_Init(void)
{
_next_interp_id = 0;
}

PyInterpreterState *
PyInterpreterState_New(void)
Expand Down Expand Up @@ -107,14 +120,14 @@ PyInterpreterState_New(void)
HEAD_LOCK();
interp->next = interp_head;
interp_head = interp;
if (_PyInterpreterState_next_id < 0) {
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 = _PyInterpreterState_next_id;
_PyInterpreterState_next_id += 1;
interp->id = _next_interp_id;
_next_interp_id += 1;
}
HEAD_UNLOCK();
}
Expand Down