Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
23af5f5
Add test suggested by ncoghlan
LewisGaul Nov 21, 2019
433663c
Finalise sub-interpreters in Py_FinalizeEx()
LewisGaul Dec 11, 2019
48e1cfc
Improve test name
LewisGaul Dec 13, 2019
0400634
Switch back to main threadstate in test_audit_subinterpreter before c…
LewisGaul Dec 13, 2019
b79649c
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 14, 2019
8b1e7d9
Markups including: switch from 'finalizing' flag to 'allow_new', add …
LewisGaul Jan 21, 2020
fd6073a
Merge branch 'finalise-subinterps' of github.com:LewisGaul/cpython in…
LewisGaul Jan 21, 2020
4bbd58f
Merge branch 'master' into finalise-subinterps
LewisGaul Oct 20, 2020
1095e66
Use '_' for unused variable in test_embed.py
LewisGaul Oct 20, 2020
675285d
Fix struct position of 'allow_new' flag
LewisGaul Oct 22, 2020
8e21788
Add handling for unsupported case of calling Py_Finalize() from a sub…
LewisGaul Oct 22, 2020
606c068
Emit resource warning when calling Py_Finalize() with unfinalized sub…
LewisGaul Oct 22, 2020
e0789b0
Update Py_FinalizeEx() docs
LewisGaul Oct 22, 2020
dda99ce
Update test for resource warning when implicitly finalizing subinterp…
LewisGaul Oct 23, 2020
847e8d2
Tidy up test_finalize_subinterps() testcase
LewisGaul Oct 23, 2020
a2fb0fc
Add testcase for calling Py_Finalize() from a subinterpreter
LewisGaul Oct 23, 2020
d234528
Tweak subinterpreters still running ResourceWarning handling
LewisGaul Nov 23, 2020
46a8619
Make calling PyFinalizeEx() from a subinterpreter a Py_FatalError
LewisGaul Nov 23, 2020
c89c0e5
Acquire interpreters mutex before setting allow_new=0 in PyFinalizeEx()
LewisGaul Nov 23, 2020
c285f52
Merge remote-tracking branch 'upstream/master' into finalise-subinterps
LewisGaul Nov 23, 2020
95cbfd4
Add back in the 'interp' variable to PyFinalizeEx() to fix the build
LewisGaul Nov 23, 2020
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
Merge branch 'master' into finalise-subinterps
  • Loading branch information
LewisGaul committed Oct 20, 2020
commit 4bbd58fe07ae6e63c1c5000237a1d2be3ddef2b1
77 changes: 6 additions & 71 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,77 +38,12 @@ _Py_ThreadCanHandleSignals(PyInterpreterState *interp)
}


/* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
int preinitialized;

/* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
int core_initialized;

/* Is Python fully initialized? Set to 1 by Py_Initialize() */
int initialized;

/* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
is called again. */
PyThreadState *finalizing;

struct pyinterpreters {
PyThread_type_lock mutex;
PyInterpreterState *head;
PyInterpreterState *main;
/* _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. */
int64_t next_id;
int allow_new;
} interpreters;
// XXX Remove this field once we have a tp_* slot.
struct _xidregistry {
PyThread_type_lock mutex;
struct _xidregitem *head;
} xidregistry;

unsigned long main_thread;

#define NEXITFUNCS 32
void (*exitfuncs[NEXITFUNCS])(void);
int nexitfuncs;

struct _ceval_runtime_state ceval;
struct _gilstate_runtime_state gilstate;

PyPreConfig preconfig;

Py_OpenCodeHookFunction open_code_hook;
void *open_code_userdata;
_Py_AuditHookEntry *audit_hook_head;

// XXX Consolidate globals found via the check-c-globals script.
} _PyRuntimeState;

#define _PyRuntimeState_INIT \
{.preinitialized = 0, .core_initialized = 0, .initialized = 0}
/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */

PyAPI_DATA(_PyRuntimeState) _PyRuntime;
PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime);
PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime);
PyAPI_FUNC(void) _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);

/* Initialize _PyRuntimeState.
Return NULL on success, or return an error message on failure. */
PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void);

PyAPI_FUNC(void) _PyRuntime_Finalize(void);

#define _Py_CURRENTLY_FINALIZING(runtime, tstate) \
(runtime->finalizing == tstate)

PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate);
/* Only execute pending calls on the main thread. */
static inline int
_Py_ThreadCanHandlePendingCalls(void)
{
return _Py_IsMainThread();
}


/* Variable and macro for in-line access to current thread
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ typedef struct pyruntimestate {
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. */
int allow_new;
Comment thread
LewisGaul marked this conversation as resolved.
Outdated
Comment thread
LewisGaul marked this conversation as resolved.
int64_t next_id;
} interpreters;
// XXX Remove this field once we have a tp_* slot.
Expand Down
82 changes: 3 additions & 79 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ init_interp_main(PyThreadState *tstate)
*/
if (is_main_interp) {
interp->runtime->initialized = 1;
interp->runtime->interpreters.allow_new = 1;
}
return _PyStatus_OK();
}
Expand Down Expand Up @@ -1061,6 +1062,7 @@ init_interp_main(PyThreadState *tstate)
}

interp->runtime->initialized = 1;
interp->runtime->interpreters.allow_new = 1;
}

if (config->site_import) {
Expand Down Expand Up @@ -1105,88 +1107,10 @@ pyinit_main(PyThreadState *tstate)
return _Py_ReconfigureMainInterpreter(tstate);
}

if (!config->_install_importlib) {
/* Special mode for freeze_importlib: run with no import system
*
* This means anything which needs support from extension modules
* or pure Python code in the standard library won't work.
*/
runtime->initialized = 1;
runtime->interpreters.allow_new = 1;
return _PyStatus_OK();
}

if (_PyTime_Init() < 0) {
return _PyStatus_ERR("can't initialize time");
}

if (_PySys_InitMain(tstate) < 0) {
return _PyStatus_ERR("can't finish initializing sys");
}

PyStatus status = init_importlib_external(tstate);
PyStatus status = init_interp_main(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

/* initialize the faulthandler module */
status = _PyFaulthandler_Init(config->faulthandler);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

status = _PyUnicode_InitEncodings(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

if (config->install_signal_handlers) {
status = init_signals(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}

if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
return _PyStatus_ERR("can't initialize tracemalloc");
}

status = add_main_module(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

status = init_sys_streams(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

/* Initialize warnings. */
PyObject *warnoptions = PySys_GetObject("warnoptions");
if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
{
PyObject *warnings_module = PyImport_ImportModule("warnings");
if (warnings_module == NULL) {
fprintf(stderr, "'import warnings' failed; traceback:\n");
_PyErr_Print(tstate);
}
Py_XDECREF(warnings_module);
}

runtime->initialized = 1;
runtime->interpreters.allow_new = 1;

if (config->site_import) {
status = init_import_site();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}

#ifndef MS_WINDOWS
emit_stderr_warning_for_legacy_locale(runtime);
#endif

return _PyStatus_OK();
}

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.