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

Skip to content

Commit 412c59c

Browse files
Add PyInterpreterState._whence.
1 parent 92c4f47 commit 412c59c

5 files changed

Lines changed: 15 additions & 18 deletions

File tree

Include/internal/pycore_crossinterp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ PyAPI_FUNC(int) _PyXI_HasCapturedException(_PyXI_session *session);
325325
// Export for _testinternalcapi shared extension
326326
PyAPI_FUNC(PyInterpreterState *) _PyXI_NewInterpreter(
327327
PyInterpreterConfig *config,
328+
long *maybe_whence,
328329
PyThreadState **p_tstate,
329330
PyThreadState **p_save_tstate);
330331
PyAPI_FUNC(void) _PyXI_EndInterpreter(

Include/internal/pycore_interp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ struct _is {
103103
#define _PyInterpreterState_WHENCE_LEGACY_CAPI 2
104104
#define _PyInterpreterState_WHENCE_CAPI 3
105105
#define _PyInterpreterState_WHENCE_XI 4
106-
#define _PyInterpreterState_WHENCE_MAX 4
106+
#define _PyInterpreterState_WHENCE_STDLIB 5
107+
#define _PyInterpreterState_WHENCE_MAX 5
107108
long _whence;
108109

109110
/* Has been initialized to a safe state.

Modules/_testinternalcapi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ static PyInterpreterState *
13821382
_new_interpreter(PyInterpreterConfig *config, long whence)
13831383
{
13841384
if (whence == _PyInterpreterState_WHENCE_XI) {
1385-
return _PyXI_NewInterpreter(config, NULL, NULL);
1385+
return _PyXI_NewInterpreter(config, &whence, NULL, NULL);
13861386
}
13871387
PyObject *exc = NULL;
13881388
PyInterpreterState *interp = NULL;
@@ -1598,7 +1598,7 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
15981598

15991599
/* Create an interpreter, staying switched to it. */
16001600
PyInterpreterState *interp = \
1601-
_PyXI_NewInterpreter(&config, &tstate, &save_tstate);
1601+
_PyXI_NewInterpreter(&config, NULL, &tstate, &save_tstate);
16021602
if (interp == NULL) {
16031603
return NULL;
16041604
}

Modules/_xxsubinterpretersmodule.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -658,21 +658,10 @@ _globals_fini(void)
658658

659659
/* module level code ********************************************************/
660660

661-
#define _PyInterpreterState_WHENCE_STDLIB 5
662-
663661
static long
664662
get_whence(PyInterpreterState *interp)
665663
{
666-
long whence = _PyInterpreterState_GetWhence(interp);
667-
if (whence == _PyInterpreterState_WHENCE_XI) {
668-
if (is_owned(&_globals.owned, interp)) {
669-
whence = _PyInterpreterState_WHENCE_STDLIB;
670-
}
671-
}
672-
else {
673-
assert(!is_owned(&_globals.owned, interp));
674-
}
675-
return whence;
664+
return _PyInterpreterState_GetWhence(interp);
676665
}
677666

678667

@@ -786,7 +775,9 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
786775
return NULL;
787776
}
788777

789-
PyInterpreterState *interp = _PyXI_NewInterpreter(&config, NULL, NULL);
778+
long whence = _PyInterpreterState_WHENCE_STDLIB;
779+
PyInterpreterState *interp = \
780+
_PyXI_NewInterpreter(&config, &whence, NULL, NULL);
790781
if (interp == NULL) {
791782
// XXX Move the chained exception to interpreters.create()?
792783
PyObject *exc = PyErr_GetRaisedException();

Python/crossinterp.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ _PyXI_FiniTypes(PyInterpreterState *interp)
18221822
/*************/
18231823

18241824
PyInterpreterState *
1825-
_PyXI_NewInterpreter(PyInterpreterConfig *config,
1825+
_PyXI_NewInterpreter(PyInterpreterConfig *config, long *maybe_whence,
18261826
PyThreadState **p_tstate, PyThreadState **p_save_tstate)
18271827
{
18281828
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
@@ -1845,7 +1845,11 @@ _PyXI_NewInterpreter(PyInterpreterConfig *config,
18451845
assert(tstate != NULL);
18461846
PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate);
18471847

1848-
_PyInterpreterState_SetWhence(interp, _PyInterpreterState_WHENCE_XI);
1848+
long whence = _PyInterpreterState_WHENCE_XI;
1849+
if (maybe_whence != NULL) {
1850+
whence = *maybe_whence;
1851+
}
1852+
_PyInterpreterState_SetWhence(interp, whence);
18491853

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

0 commit comments

Comments
 (0)