Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Add PyInterpreterState._whence.
  • Loading branch information
ericsnowcurrently committed Apr 11, 2024
commit 19b68ebd15f9930fd4c579ae097bbb7819550b25
1 change: 1 addition & 0 deletions Include/internal/pycore_crossinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ PyAPI_FUNC(int) _PyXI_HasCapturedException(_PyXI_session *session);
// Export for _testinternalcapi shared extension
PyAPI_FUNC(PyInterpreterState *) _PyXI_NewInterpreter(
PyInterpreterConfig *config,
long *maybe_whence,
PyThreadState **p_tstate,
PyThreadState **p_save_tstate);
PyAPI_FUNC(void) _PyXI_EndInterpreter(
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ struct _is {
#define _PyInterpreterState_WHENCE_LEGACY_CAPI 2
#define _PyInterpreterState_WHENCE_CAPI 3
#define _PyInterpreterState_WHENCE_XI 4
#define _PyInterpreterState_WHENCE_MAX 4
#define _PyInterpreterState_WHENCE_STDLIB 5
#define _PyInterpreterState_WHENCE_MAX 5
long _whence;

/* Has been initialized to a safe state.
Expand Down
4 changes: 2 additions & 2 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ static PyInterpreterState *
_new_interpreter(PyInterpreterConfig *config, long whence)
{
if (whence == _PyInterpreterState_WHENCE_XI) {
return _PyXI_NewInterpreter(config, NULL, NULL);
return _PyXI_NewInterpreter(config, &whence, NULL, NULL);
}
PyObject *exc = NULL;
PyInterpreterState *interp = NULL;
Expand Down Expand Up @@ -1610,7 +1610,7 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)

/* Create an interpreter, staying switched to it. */
PyInterpreterState *interp = \
_PyXI_NewInterpreter(&config, &tstate, &save_tstate);
_PyXI_NewInterpreter(&config, NULL, &tstate, &save_tstate);
if (interp == NULL) {
return NULL;
}
Expand Down
17 changes: 4 additions & 13 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,21 +658,10 @@ _globals_fini(void)

/* module level code ********************************************************/

#define _PyInterpreterState_WHENCE_STDLIB 5

static long
get_whence(PyInterpreterState *interp)
{
long whence = _PyInterpreterState_GetWhence(interp);
if (whence == _PyInterpreterState_WHENCE_XI) {
if (is_owned(&_globals.owned, interp)) {
whence = _PyInterpreterState_WHENCE_STDLIB;
}
}
else {
assert(!is_owned(&_globals.owned, interp));
}
return whence;
return _PyInterpreterState_GetWhence(interp);
}


Expand Down Expand Up @@ -786,7 +775,9 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = _PyXI_NewInterpreter(&config, NULL, NULL);
long whence = _PyInterpreterState_WHENCE_STDLIB;
PyInterpreterState *interp = \
_PyXI_NewInterpreter(&config, &whence, NULL, NULL);
if (interp == NULL) {
// XXX Move the chained exception to interpreters.create()?
PyObject *exc = PyErr_GetRaisedException();
Expand Down
8 changes: 6 additions & 2 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,7 @@ _PyXI_FiniTypes(PyInterpreterState *interp)
/*************/

PyInterpreterState *
_PyXI_NewInterpreter(PyInterpreterConfig *config,
_PyXI_NewInterpreter(PyInterpreterConfig *config, long *maybe_whence,
PyThreadState **p_tstate, PyThreadState **p_save_tstate)
{
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
Expand All @@ -1845,7 +1845,11 @@ _PyXI_NewInterpreter(PyInterpreterConfig *config,
assert(tstate != NULL);
PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate);

_PyInterpreterState_SetWhence(interp, _PyInterpreterState_WHENCE_XI);
long whence = _PyInterpreterState_WHENCE_XI;
if (maybe_whence != NULL) {
whence = *maybe_whence;
}
_PyInterpreterState_SetWhence(interp, whence);

if (p_tstate != NULL) {
// We leave the new thread state as the current one.
Expand Down