Skip to content

Instantly share code, notes, and snippets.

@bhavishyagopesh
Created July 21, 2017 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhavishyagopesh/0531fe5d0271491965c6ab5f68495ca9 to your computer and use it in GitHub Desktop.
Save bhavishyagopesh/0531fe5d0271491965c6ab5f68495ca9 to your computer and use it in GitHub Desktop.
_Py_InitializeMainInterpreter Definition
/* Update interpreter state based on supplied configuration settings
*
* After calling this function, most of the restrictions on the interpreter
* are lifted. The only remaining incomplete settings are those related
* to the main module (sys.argv[0], __main__ metadata)
*
* Calling this when the interpreter is not initializing, is already
* initialized or without a valid current thread state is a fatal error.
* Other errors should be reported as normal Python exceptions with a
* non-zero return code.
*/
int _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
{
PyInterpreterState *interp;
PyThreadState *tstate;
if (!_Py_CoreInitialized) {
Py_FatalError("Py_InitializeMainInterpreter: runtime core not initialized");
}
if (_Py_Initialized) {
Py_FatalError("Py_InitializeMainInterpreter: main interpreter already initialized");
}
/* Get current thread state and interpreter pointer */
tstate = PyThreadState_GET();
if (!tstate)
Py_FatalError("Py_InitializeMainInterpreter: failed to read thread state");
interp = tstate->interp;
if (!interp)
Py_FatalError("Py_InitializeMainInterpreter: failed to get interpreter");
/* Now finish configuring the main interpreter */
interp->config = *config;
if (interp->core_config._disable_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.
*/
_Py_Initialized = 1;
return 0;
}
/* TODO: Report exceptions rather than fatal errors below here */
if (_PyTime_Init() < 0)
Py_FatalError("Py_InitializeMainInterpreter: can't initialize time");
/* Finish setting up the sys module and import system */
/* GetPath may initialize state that _PySys_EndInit locks
in, and so has to be called first. */
/* TODO: Call Py_GetPath() in Py_ReadConfig, rather than here */
PySys_SetPath(Py_GetPath());
if (_PySys_EndInit(interp->sysdict) < 0)
Py_FatalError("Py_InitializeMainInterpreter: can't finish initializing sys");
initexternalimport(interp);
/* initialize the faulthandler module */
if (_PyFaulthandler_Init())
Py_FatalError("Py_InitializeMainInterpreter: can't initialize faulthandler");
if (initfsencoding(interp) < 0)
Py_FatalError("Py_InitializeMainInterpreter: unable to load the file system codec");
if (config->install_signal_handlers)
initsigs(); /* Signal handling stuff, including initintr() */
if (_PyTraceMalloc_Init() < 0)
Py_FatalError("Py_InitializeMainInterpreter: can't initialize tracemalloc");
initmain(interp); /* Module __main__ */
if (initstdio() < 0)
Py_FatalError(
"Py_InitializeMainInterpreter: can't initialize sys standard streams");
/* Initialize warnings. */
if (PySys_HasWarnOptions()) {
PyObject *warnings_module = PyImport_ImportModule("warnings");
if (warnings_module == NULL) {
fprintf(stderr, "'import warnings' failed; traceback:\n");
PyErr_Print();
}
Py_XDECREF(warnings_module);
}
_Py_Initialized = 1;
if (!Py_NoSiteFlag)
initsite(); /* Module site */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment