2014-11-20 07:39:37 -04:00
|
|
|
/* Python interpreter top-level routines, including init/exit */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
#include "Python-ast.h"
|
2018-11-22 13:38:38 -04:00
|
|
|
#undef Yield /* undefine macro conflicting with <winbase.h> */
|
2019-05-10 18:39:09 -03:00
|
|
|
#include "pycore_ceval.h"
|
2019-04-29 08:04:07 -03:00
|
|
|
#include "pycore_context.h"
|
2019-06-18 21:54:39 -03:00
|
|
|
#include "pycore_import.h" /* _PyImport_FindBuiltin */
|
2019-05-27 11:39:22 -03:00
|
|
|
#include "pycore_initconfig.h"
|
2018-11-23 08:08:26 -04:00
|
|
|
#include "pycore_fileutils.h"
|
2018-10-31 20:52:28 -03:00
|
|
|
#include "pycore_hamt.h"
|
2018-10-31 23:15:58 -03:00
|
|
|
#include "pycore_pathconfig.h"
|
2019-06-19 19:05:23 -03:00
|
|
|
#include "pycore_pyerrors.h"
|
2018-11-12 11:53:38 -04:00
|
|
|
#include "pycore_pylifecycle.h"
|
|
|
|
#include "pycore_pymem.h"
|
|
|
|
#include "pycore_pystate.h"
|
2019-05-19 19:14:57 -03:00
|
|
|
#include "pycore_traceback.h"
|
2014-11-20 07:39:37 -04:00
|
|
|
#include "grammar.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "parsetok.h"
|
|
|
|
#include "errcode.h"
|
|
|
|
#include "code.h"
|
|
|
|
#include "symtable.h"
|
|
|
|
#include "ast.h"
|
|
|
|
#include "marshal.h"
|
|
|
|
#include "osdefs.h"
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#include "malloc.h" /* for alloca */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_LANGINFO_H
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#undef BYTE
|
|
|
|
#include "windows.h"
|
2016-08-31 01:22:36 -03:00
|
|
|
|
|
|
|
extern PyTypeObject PyWindowsConsoleIO_Type;
|
|
|
|
#define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_Py_IDENTIFIER(flush);
|
|
|
|
_Py_IDENTIFIER(name);
|
|
|
|
_Py_IDENTIFIER(stdin);
|
|
|
|
_Py_IDENTIFIER(stdout);
|
|
|
|
_Py_IDENTIFIER(stderr);
|
2017-09-15 19:35:20 -03:00
|
|
|
_Py_IDENTIFIER(threading);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern grammar _PyParser_Grammar; /* From graminit.c */
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
/* Forward declarations */
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus add_main_module(PyInterpreterState *interp);
|
2019-06-19 19:05:23 -03:00
|
|
|
static PyStatus init_import_site(void);
|
|
|
|
static PyStatus init_sys_streams(PyThreadState *tstate);
|
|
|
|
static PyStatus init_signals(PyThreadState *tstate);
|
|
|
|
static void call_py_exitfuncs(PyThreadState *tstate);
|
|
|
|
static void wait_for_thread_shutdown(PyThreadState *tstate);
|
2019-04-24 12:24:01 -03:00
|
|
|
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-02-16 16:57:40 -04:00
|
|
|
int _Py_UnhandledKeyboardInterrupt = 0;
|
2017-11-15 19:48:08 -04:00
|
|
|
_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
|
2019-03-19 20:03:01 -03:00
|
|
|
static int runtime_initialized = 0;
|
2017-09-08 02:51:28 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime_Initialize(void)
|
|
|
|
{
|
|
|
|
/* XXX We only initialize once in the process, which aligns with
|
|
|
|
the static initialization of the former globals now found in
|
|
|
|
_PyRuntime. However, _PyRuntime *should* be initialized with
|
|
|
|
every Py_Initialize() call, but doing so breaks the runtime.
|
|
|
|
This is because the runtime state is not properly finalized
|
|
|
|
currently. */
|
2019-03-19 20:03:01 -03:00
|
|
|
if (runtime_initialized) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2019-03-19 20:03:01 -03:00
|
|
|
runtime_initialized = 1;
|
2017-11-15 19:48:08 -04:00
|
|
|
|
|
|
|
return _PyRuntimeState_Init(&_PyRuntime);
|
2017-09-08 02:51:28 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyRuntime_Finalize(void)
|
|
|
|
{
|
|
|
|
_PyRuntimeState_Fini(&_PyRuntime);
|
2019-03-19 20:03:01 -03:00
|
|
|
runtime_initialized = 0;
|
2017-09-08 02:51:28 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_Py_IsFinalizing(void)
|
|
|
|
{
|
|
|
|
return _PyRuntime.finalizing != NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Hack to force loading of object files */
|
|
|
|
int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
|
|
|
|
PyOS_mystrnicmp; /* Python/pystrcmp.o */
|
|
|
|
|
|
|
|
/* PyModule_GetWarningsModule is no longer necessary as of 2.6
|
|
|
|
since _warnings is builtin. This API should not be used. */
|
|
|
|
PyObject *
|
|
|
|
PyModule_GetWarningsModule(void)
|
|
|
|
{
|
|
|
|
return PyImport_ImportModule("warnings");
|
|
|
|
}
|
|
|
|
|
2017-05-24 03:00:52 -03:00
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
/* APIs to access the initialization flags
|
|
|
|
*
|
|
|
|
* Can be called prior to Py_Initialize.
|
|
|
|
*/
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
int
|
|
|
|
_Py_IsCoreInitialized(void)
|
|
|
|
{
|
2017-09-08 02:51:28 -03:00
|
|
|
return _PyRuntime.core_initialized;
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
Py_IsInitialized(void)
|
|
|
|
{
|
2017-09-08 02:51:28 -03:00
|
|
|
return _PyRuntime.initialized;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-11 00:16:15 -03:00
|
|
|
|
2015-11-29 23:18:29 -04:00
|
|
|
/* Global initializations. Can be undone by Py_FinalizeEx(). Don't
|
|
|
|
call this twice without an intervening Py_FinalizeEx() call. When
|
2014-11-20 07:39:37 -04:00
|
|
|
initializations fail, a fatal error is issued and the function does
|
|
|
|
not return. On return, the first thread and interpreter state have
|
|
|
|
been created.
|
|
|
|
|
|
|
|
Locking: you must hold the interpreter lock while calling this.
|
|
|
|
(If the lock has not yet been initialized, that's equivalent to
|
|
|
|
having the lock, but you cannot use multiple threads.)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-06-19 19:05:23 -03:00
|
|
|
init_importlib(PyThreadState *tstate, PyObject *sysmod)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
PyObject *importlib;
|
|
|
|
PyObject *impmod;
|
|
|
|
PyObject *value;
|
2019-06-19 19:05:23 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
2019-05-27 11:39:22 -03:00
|
|
|
int verbose = interp->config.verbose;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Import _importlib through its frozen version, _frozen_importlib. */
|
|
|
|
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't import _frozen_importlib");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2019-05-14 12:34:56 -03:00
|
|
|
else if (verbose) {
|
2014-11-20 07:39:37 -04:00
|
|
|
PySys_FormatStderr("import _frozen_importlib # frozen\n");
|
|
|
|
}
|
|
|
|
importlib = PyImport_AddModule("_frozen_importlib");
|
|
|
|
if (importlib == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("couldn't get _frozen_importlib from sys.modules");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
interp->importlib = importlib;
|
|
|
|
Py_INCREF(interp->importlib);
|
|
|
|
|
2016-08-02 16:51:21 -03:00
|
|
|
interp->import_func = PyDict_GetItemString(interp->builtins, "__import__");
|
|
|
|
if (interp->import_func == NULL)
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("__import__ not found");
|
2016-08-02 16:51:21 -03:00
|
|
|
Py_INCREF(interp->import_func);
|
|
|
|
|
2015-09-18 04:11:57 -03:00
|
|
|
/* Import the _imp module */
|
2018-01-29 15:33:57 -04:00
|
|
|
impmod = PyInit__imp();
|
2014-11-20 07:39:37 -04:00
|
|
|
if (impmod == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't import _imp");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2019-05-14 12:34:56 -03:00
|
|
|
else if (verbose) {
|
2015-09-18 04:11:57 -03:00
|
|
|
PySys_FormatStderr("import _imp # builtin\n");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2017-09-15 19:35:20 -03:00
|
|
|
if (_PyImport_SetModuleString("_imp", impmod) < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't save _imp to sys.modules");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 04:11:57 -03:00
|
|
|
/* Install importlib as the implementation of import */
|
2014-11-20 07:39:37 -04:00
|
|
|
value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
|
|
|
|
if (value == NULL) {
|
2019-06-19 19:05:23 -03:00
|
|
|
_PyErr_Print(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("importlib install failed");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(impmod);
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-06-18 21:54:39 -03:00
|
|
|
init_importlib_external(PyThreadState *tstate)
|
2017-05-24 01:46:51 -03:00
|
|
|
{
|
|
|
|
PyObject *value;
|
2019-06-18 21:54:39 -03:00
|
|
|
value = PyObject_CallMethod(tstate->interp->importlib,
|
2017-05-24 01:46:51 -03:00
|
|
|
"_install_external_importers", "");
|
|
|
|
if (value == NULL) {
|
2019-06-19 19:05:23 -03:00
|
|
|
_PyErr_Print(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("external importer setup failed");
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
2017-06-08 08:13:20 -03:00
|
|
|
Py_DECREF(value);
|
2019-06-18 21:54:39 -03:00
|
|
|
return _PyImportZip_Init(tstate);
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-06-11 00:16:15 -03:00
|
|
|
/* Helper functions to better handle the legacy C locale
|
|
|
|
*
|
|
|
|
* The legacy C locale assumes ASCII as the default text encoding, which
|
|
|
|
* causes problems not only for the CPython runtime, but also other
|
|
|
|
* components like GNU readline.
|
|
|
|
*
|
|
|
|
* Accordingly, when the CLI detects it, it attempts to coerce it to a
|
|
|
|
* more capable UTF-8 based alternative as follows:
|
|
|
|
*
|
|
|
|
* if (_Py_LegacyLocaleDetected()) {
|
|
|
|
* _Py_CoerceLegacyLocale();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* See the documentation of the PYTHONCOERCECLOCALE setting for more details.
|
|
|
|
*
|
|
|
|
* Locale coercion also impacts the default error handler for the standard
|
|
|
|
* streams: while the usual default is "strict", the default for the legacy
|
|
|
|
* C locale and for any of the coercion target locales is "surrogateescape".
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2019-05-20 12:16:38 -03:00
|
|
|
_Py_LegacyLocaleDetected(int warn)
|
2017-06-11 00:16:15 -03:00
|
|
|
{
|
|
|
|
#ifndef MS_WINDOWS
|
2019-05-20 12:16:38 -03:00
|
|
|
if (!warn) {
|
|
|
|
const char *locale_override = getenv("LC_ALL");
|
|
|
|
if (locale_override != NULL && *locale_override != '\0') {
|
|
|
|
/* Don't coerce C locale if the LC_ALL environment variable
|
|
|
|
is set */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 00:16:15 -03:00
|
|
|
/* On non-Windows systems, the C locale is considered a legacy locale */
|
2017-06-17 23:29:42 -03:00
|
|
|
/* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
|
|
|
|
* the POSIX locale as a simple alias for the C locale, so
|
|
|
|
* we may also want to check for that explicitly.
|
|
|
|
*/
|
2017-06-11 00:16:15 -03:00
|
|
|
const char *ctype_loc = setlocale(LC_CTYPE, NULL);
|
|
|
|
return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
|
|
|
|
#else
|
|
|
|
/* Windows uses code pages instead of locales, so no locale is legacy */
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-17 23:29:42 -03:00
|
|
|
static const char *_C_LOCALE_WARNING =
|
|
|
|
"Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
|
|
|
|
"encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
|
|
|
|
"C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
|
|
|
|
"locales is recommended.\n";
|
|
|
|
|
|
|
|
static void
|
2019-04-24 13:23:53 -03:00
|
|
|
emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
|
2017-06-17 23:29:42 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyPreConfig *preconfig = &runtime->preconfig;
|
2019-05-20 12:16:38 -03:00
|
|
|
if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
|
2018-08-29 17:56:06 -03:00
|
|
|
PySys_FormatStderr("%s", _C_LOCALE_WARNING);
|
2017-06-17 23:29:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 00:16:15 -03:00
|
|
|
typedef struct _CandidateLocale {
|
|
|
|
const char *locale_name; /* The locale to try as a coercion target */
|
|
|
|
} _LocaleCoercionTarget;
|
|
|
|
|
|
|
|
static _LocaleCoercionTarget _TARGET_LOCALES[] = {
|
|
|
|
{"C.UTF-8"},
|
|
|
|
{"C.utf8"},
|
2017-06-29 11:48:14 -03:00
|
|
|
{"UTF-8"},
|
2017-06-11 00:16:15 -03:00
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-29 06:47:29 -03:00
|
|
|
int
|
|
|
|
_Py_IsLocaleCoercionTarget(const char *ctype_loc)
|
|
|
|
{
|
|
|
|
const _LocaleCoercionTarget *target = NULL;
|
|
|
|
for (target = _TARGET_LOCALES; target->locale_name; target++) {
|
|
|
|
if (strcmp(ctype_loc, target->locale_name) == 0) {
|
|
|
|
return 1;
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
2018-08-28 20:29:06 -03:00
|
|
|
}
|
2018-08-29 06:47:29 -03:00
|
|
|
return 0;
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
|
|
|
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2017-06-11 00:16:15 -03:00
|
|
|
#ifdef PY_COERCE_C_LOCALE
|
2017-12-15 23:54:22 -04:00
|
|
|
static const char C_LOCALE_COERCION_WARNING[] =
|
2017-06-11 00:16:15 -03:00
|
|
|
"Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
|
|
|
|
"or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
|
|
|
|
|
2019-05-20 12:16:38 -03:00
|
|
|
static int
|
2018-08-29 08:25:36 -03:00
|
|
|
_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
|
2017-06-11 00:16:15 -03:00
|
|
|
{
|
|
|
|
const char *newloc = target->locale_name;
|
|
|
|
|
|
|
|
/* Reset locale back to currently configured defaults */
|
2017-11-12 07:45:59 -04:00
|
|
|
_Py_SetLocaleFromEnv(LC_ALL);
|
2017-06-11 00:16:15 -03:00
|
|
|
|
|
|
|
/* Set the relevant locale environment variable */
|
|
|
|
if (setenv("LC_CTYPE", newloc, 1)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Error setting LC_CTYPE, skipping C locale coercion\n");
|
2019-05-20 12:16:38 -03:00
|
|
|
return 0;
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
2018-08-29 08:25:36 -03:00
|
|
|
if (warn) {
|
2017-12-15 23:54:22 -04:00
|
|
|
fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
|
2017-06-17 23:29:42 -03:00
|
|
|
}
|
2017-06-11 00:16:15 -03:00
|
|
|
|
|
|
|
/* Reconfigure with the overridden environment variables */
|
2017-11-12 07:45:59 -04:00
|
|
|
_Py_SetLocaleFromEnv(LC_ALL);
|
2019-05-20 12:16:38 -03:00
|
|
|
return 1;
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-20 12:16:38 -03:00
|
|
|
int
|
2018-08-29 08:25:36 -03:00
|
|
|
_Py_CoerceLegacyLocale(int warn)
|
2017-06-11 00:16:15 -03:00
|
|
|
{
|
2019-05-20 12:16:38 -03:00
|
|
|
int coerced = 0;
|
2017-06-11 00:16:15 -03:00
|
|
|
#ifdef PY_COERCE_C_LOCALE
|
2018-09-03 12:05:18 -03:00
|
|
|
char *oldloc = NULL;
|
|
|
|
|
|
|
|
oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
|
|
|
|
if (oldloc == NULL) {
|
2019-05-20 12:16:38 -03:00
|
|
|
return coerced;
|
2018-09-03 12:05:18 -03:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:54:22 -04:00
|
|
|
const char *locale_override = getenv("LC_ALL");
|
|
|
|
if (locale_override == NULL || *locale_override == '\0') {
|
|
|
|
/* LC_ALL is also not set (or is set to an empty string) */
|
|
|
|
const _LocaleCoercionTarget *target = NULL;
|
|
|
|
for (target = _TARGET_LOCALES; target->locale_name; target++) {
|
|
|
|
const char *new_locale = setlocale(LC_CTYPE,
|
|
|
|
target->locale_name);
|
|
|
|
if (new_locale != NULL) {
|
2019-05-02 12:28:57 -03:00
|
|
|
#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
|
2017-12-15 23:54:22 -04:00
|
|
|
/* Also ensure that nl_langinfo works in this locale */
|
|
|
|
char *codeset = nl_langinfo(CODESET);
|
|
|
|
if (!codeset || *codeset == '\0') {
|
|
|
|
/* CODESET is not set or empty, so skip coercion */
|
|
|
|
new_locale = NULL;
|
|
|
|
_Py_SetLocaleFromEnv(LC_CTYPE);
|
|
|
|
continue;
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
2017-12-15 23:54:22 -04:00
|
|
|
#endif
|
|
|
|
/* Successfully configured locale, so make it the default */
|
2019-05-20 12:16:38 -03:00
|
|
|
coerced = _coerce_default_locale_settings(warn, target);
|
2018-09-03 12:05:18 -03:00
|
|
|
goto done;
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* No C locale warning here, as Py_Initialize will emit one later */
|
2018-09-03 12:05:18 -03:00
|
|
|
|
|
|
|
setlocale(LC_CTYPE, oldloc);
|
|
|
|
|
|
|
|
done:
|
|
|
|
PyMem_RawFree(oldloc);
|
2017-06-11 00:16:15 -03:00
|
|
|
#endif
|
2019-05-20 12:16:38 -03:00
|
|
|
return coerced;
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
|
|
|
|
2017-11-12 07:45:59 -04:00
|
|
|
/* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
|
|
|
|
* isolate the idiosyncrasies of different libc implementations. It reads the
|
|
|
|
* appropriate environment variable and uses its value to select the locale for
|
|
|
|
* 'category'. */
|
|
|
|
char *
|
|
|
|
_Py_SetLocaleFromEnv(int category)
|
|
|
|
{
|
2018-11-23 08:08:26 -04:00
|
|
|
char *res;
|
2017-11-12 07:45:59 -04:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
const char *locale;
|
|
|
|
const char **pvar;
|
|
|
|
#ifdef PY_COERCE_C_LOCALE
|
|
|
|
const char *coerce_c_locale;
|
|
|
|
#endif
|
|
|
|
const char *utf8_locale = "C.UTF-8";
|
|
|
|
const char *env_var_set[] = {
|
|
|
|
"LC_ALL",
|
|
|
|
"LC_CTYPE",
|
|
|
|
"LANG",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Android setlocale(category, "") doesn't check the environment variables
|
|
|
|
* and incorrectly sets the "C" locale at API 24 and older APIs. We only
|
|
|
|
* check the environment variables listed in env_var_set. */
|
|
|
|
for (pvar=env_var_set; *pvar; pvar++) {
|
|
|
|
locale = getenv(*pvar);
|
|
|
|
if (locale != NULL && *locale != '\0') {
|
|
|
|
if (strcmp(locale, utf8_locale) == 0 ||
|
|
|
|
strcmp(locale, "en_US.UTF-8") == 0) {
|
|
|
|
return setlocale(category, utf8_locale);
|
|
|
|
}
|
|
|
|
return setlocale(category, "C");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
|
|
|
|
* LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
|
|
|
|
* Quote from POSIX section "8.2 Internationalization Variables":
|
|
|
|
* "4. If the LANG environment variable is not set or is set to the empty
|
|
|
|
* string, the implementation-defined default locale shall be used." */
|
|
|
|
|
|
|
|
#ifdef PY_COERCE_C_LOCALE
|
|
|
|
coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
|
|
|
|
if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
|
|
|
|
/* Some other ported code may check the environment variables (e.g. in
|
|
|
|
* extension modules), so we make sure that they match the locale
|
|
|
|
* configuration */
|
|
|
|
if (setenv("LC_CTYPE", utf8_locale, 1)) {
|
|
|
|
fprintf(stderr, "Warning: failed setting the LC_CTYPE "
|
|
|
|
"environment variable to %s\n", utf8_locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-23 08:08:26 -04:00
|
|
|
res = setlocale(category, utf8_locale);
|
|
|
|
#else /* !defined(__ANDROID__) */
|
|
|
|
res = setlocale(category, "");
|
|
|
|
#endif
|
|
|
|
_Py_ResetForceASCII();
|
|
|
|
return res;
|
2017-11-12 07:45:59 -04:00
|
|
|
}
|
|
|
|
|
2017-06-11 00:16:15 -03:00
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
/* Global initializations. Can be undone by Py_Finalize(). Don't
|
|
|
|
call this twice without an intervening Py_Finalize() call.
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
|
2017-05-24 01:46:51 -03:00
|
|
|
must have a corresponding call to Py_Finalize.
|
|
|
|
|
|
|
|
Locking: you must hold the interpreter lock while calling these APIs.
|
|
|
|
(If the lock has not yet been initialized, that's equivalent to
|
|
|
|
having the lock, but you cannot use multiple threads.)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-05-22 19:57:57 -03:00
|
|
|
pyinit_core_reconfigure(_PyRuntimeState *runtime,
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState **tstate_p,
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyConfig *config)
|
2018-07-24 21:49:17 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-01-22 16:18:05 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
if (!tstate) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("failed to read thread state");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2019-06-19 19:05:23 -03:00
|
|
|
*tstate_p = tstate;
|
2019-01-22 16:18:05 -04:00
|
|
|
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
if (interp == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't make main interpreter");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyConfig_Write(config, runtime);
|
2018-07-24 21:49:17 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyConfig_Copy(&interp->config, config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
config = &interp->config;
|
2018-07-24 21:49:17 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config->_install_importlib) {
|
2019-09-26 10:51:50 -03:00
|
|
|
status = _PyConfig_WritePathConfig(config);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-04-24 13:23:53 -03:00
|
|
|
pycore_init_runtime(_PyRuntimeState *runtime,
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyConfig *config)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2019-04-24 13:23:53 -03:00
|
|
|
if (runtime->initialized) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("main interpreter already initialized");
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
2017-12-14 20:46:02 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyConfig_Write(config, runtime);
|
2017-09-08 02:51:28 -03:00
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
/* Py_Finalize leaves _Py_Finalizing set in order to help daemon
|
|
|
|
* threads behave a little more gracefully at interpreter shutdown.
|
|
|
|
* We clobber it here so the new interpreter can start with a clean
|
|
|
|
* slate.
|
|
|
|
*
|
|
|
|
* However, this may still lead to misbehaviour if there are daemon
|
|
|
|
* threads still hanging around from a previous Py_Initialize/Finalize
|
|
|
|
* pair :(
|
|
|
|
*/
|
2019-04-24 13:23:53 -03:00
|
|
|
runtime->finalizing = NULL;
|
2017-09-08 02:51:28 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _Py_HashRandomization_Init(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyInterpreterState_Enable(runtime);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-04-24 13:23:53 -03:00
|
|
|
pycore_create_interpreter(_PyRuntimeState *runtime,
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyConfig *config,
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState **tstate_p)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
|
|
|
PyInterpreterState *interp = PyInterpreterState_New();
|
2017-12-14 20:46:02 -04:00
|
|
|
if (interp == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't make main interpreter");
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _PyConfig_Copy(&interp->config, config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_New(interp);
|
2019-06-19 19:05:23 -03:00
|
|
|
if (tstate == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't make first thread");
|
2019-06-19 19:05:23 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
(void) PyThreadState_Swap(tstate);
|
|
|
|
|
2019-04-29 08:04:07 -03:00
|
|
|
/* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
|
|
|
|
destroying the GIL might fail when it is being referenced from
|
|
|
|
another running thread (see issue #9901).
|
2014-11-20 07:39:37 -04:00
|
|
|
Instead we destroy the previously created GIL here, which ensures
|
2015-11-29 23:18:29 -04:00
|
|
|
that we can call Py_Initialize / Py_FinalizeEx multiple times. */
|
2019-05-10 18:39:09 -03:00
|
|
|
_PyEval_FiniThreads(&runtime->ceval);
|
2018-01-29 06:57:45 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Auto-thread-state API */
|
2019-11-19 21:27:56 -04:00
|
|
|
_PyGILState_Init(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2018-01-29 06:57:45 -04:00
|
|
|
/* Create the GIL */
|
|
|
|
PyEval_InitThreads();
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
*tstate_p = tstate;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-11-19 21:27:56 -04:00
|
|
|
pycore_init_types(PyThreadState *tstate)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
2019-11-19 20:18:11 -04:00
|
|
|
PyStatus status;
|
|
|
|
|
2019-11-19 21:27:56 -04:00
|
|
|
status = _PyGC_Init(tstate);
|
2019-11-19 20:18:11 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = _PyTypes_Init();
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-11-19 19:38:03 -04:00
|
|
|
if (!_PyLong_Init()) {
|
|
|
|
return _PyStatus_ERR("can't init longs");
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyUnicode_Init();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyExc_Init();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_PyFloat_Init()) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't init float");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-11-19 19:38:03 -04:00
|
|
|
if (_PyStructSequence_Init() < 0) {
|
|
|
|
return _PyStatus_ERR("can't initialize structseq");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2019-05-22 06:28:22 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyErr_Init();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-22 06:28:22 -03:00
|
|
|
}
|
|
|
|
|
2019-11-19 19:38:03 -04:00
|
|
|
if (!_PyContext_Init()) {
|
|
|
|
return _PyStatus_ERR("can't init context");
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-06-19 19:05:23 -03:00
|
|
|
pycore_init_builtins(PyThreadState *tstate)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
2019-06-19 19:05:23 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
|
|
|
|
PyObject *bimod = _PyBuiltin_Init(tstate);
|
2019-01-22 16:18:05 -04:00
|
|
|
if (bimod == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't initialize builtins modules");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
_PyImport_FixupBuiltin(bimod, "builtins", interp->modules);
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
interp->builtins = PyModule_GetDict(bimod);
|
2019-01-22 16:18:05 -04:00
|
|
|
if (interp->builtins == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't initialize builtins dict");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_INCREF(interp->builtins);
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _PyBuiltins_AddExceptions(bimod);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 12:39:03 -04:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-06-19 19:05:23 -03:00
|
|
|
pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
2019-06-19 19:05:23 -03:00
|
|
|
const PyConfig *config = &tstate->interp->config;
|
|
|
|
|
|
|
|
PyStatus status = _PyImport_Init(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = _PyImportHooks_Init(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Initialize _warnings. */
|
2017-12-19 06:35:58 -04:00
|
|
|
if (_PyWarnings_Init() == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't initialize warnings");
|
2017-11-23 05:43:14 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
if (config->_install_importlib) {
|
2019-09-26 10:51:50 -03:00
|
|
|
status = _PyConfig_WritePathConfig(config);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-07-20 21:06:16 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
/* This call sets up builtin and frozen import support */
|
2019-06-19 19:05:23 -03:00
|
|
|
if (config->_install_importlib) {
|
|
|
|
status = init_importlib(tstate, sysmod);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
pyinit_config(_PyRuntimeState *runtime,
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState **tstate_p,
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyConfig *config)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyConfig_Write(config, runtime);
|
2019-03-25 22:31:11 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = pycore_init_runtime(runtime, config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState *tstate;
|
|
|
|
status = pycore_create_interpreter(runtime, config, &tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2019-06-19 19:05:23 -03:00
|
|
|
config = &tstate->interp->config;
|
|
|
|
*tstate_p = tstate;
|
2019-01-22 16:18:05 -04:00
|
|
|
|
2019-11-19 21:27:56 -04:00
|
|
|
status = pycore_init_types(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *sysmod;
|
2019-11-19 21:27:56 -04:00
|
|
|
status = _PySys_Create(tstate, &sysmod);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = pycore_init_builtins(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = pycore_init_import_warnings(tstate, sysmod);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2017-05-24 01:46:51 -03:00
|
|
|
|
|
|
|
/* Only when we get here is the runtime core fully initialized */
|
2019-04-24 13:23:53 -03:00
|
|
|
runtime->core_initialized = 1;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
|
|
|
|
2019-03-05 19:36:56 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
|
2019-03-05 19:36:56 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-19 22:20:13 -03:00
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
if (src_config == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("preinitialization config is NULL");
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyRuntime_Initialize();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-19 22:20:13 -03:00
|
|
|
}
|
2019-04-24 13:23:53 -03:00
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
2019-03-19 22:20:13 -03:00
|
|
|
|
2019-09-17 18:59:51 -03:00
|
|
|
if (runtime->preinitialized) {
|
2019-03-25 13:54:58 -03:00
|
|
|
/* If it's already configured: ignored the new configuration */
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-25 14:37:10 -03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:59:51 -03:00
|
|
|
/* Note: preinitialized remains 1 on error, it is only set to 0
|
|
|
|
at exit on success. */
|
|
|
|
runtime->preinitializing = 1;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig config;
|
2019-09-27 23:28:35 -03:00
|
|
|
|
|
|
|
status = _PyPreConfig_InitFromPreConfig(&config, src_config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-03-25 13:54:58 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyPreConfig_Read(&config, args);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-05 19:36:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyPreConfig_Write(&config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-19 22:20:13 -03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:59:51 -03:00
|
|
|
runtime->preinitializing = 0;
|
|
|
|
runtime->preinitialized = 1;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-19 22:20:13 -03:00
|
|
|
}
|
|
|
|
|
2019-05-02 16:25:34 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
|
2019-03-25 14:37:10 -03:00
|
|
|
{
|
2019-03-27 09:40:14 -03:00
|
|
|
_PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
|
2019-05-02 16:25:34 -03:00
|
|
|
return _Py_PreInitializeFromPyArgv(src_config, &args);
|
2019-03-25 14:37:10 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
|
2019-03-25 22:31:11 -03:00
|
|
|
{
|
2019-03-27 09:40:14 -03:00
|
|
|
_PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
|
2019-05-02 16:25:34 -03:00
|
|
|
return _Py_PreInitializeFromPyArgv(src_config, &args);
|
2019-03-25 22:31:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
Py_PreInitialize(const PyPreConfig *src_config)
|
2019-03-19 22:20:13 -03:00
|
|
|
{
|
2019-05-02 16:25:34 -03:00
|
|
|
return _Py_PreInitializeFromPyArgv(src_config, NULL);
|
2019-03-25 13:54:58 -03:00
|
|
|
}
|
2019-03-05 19:36:56 -04:00
|
|
|
|
2019-03-19 22:20:13 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
_Py_PreInitializeFromConfig(const PyConfig *config,
|
|
|
|
const _PyArgv *args)
|
2019-03-25 13:54:58 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
assert(config != NULL);
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _PyRuntime_Initialize();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
|
|
|
|
2019-09-17 18:59:51 -03:00
|
|
|
if (runtime->preinitialized) {
|
2019-05-20 06:02:00 -03:00
|
|
|
/* Already initialized: do nothing */
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
2019-09-27 23:28:35 -03:00
|
|
|
|
2019-10-01 05:56:37 -03:00
|
|
|
_PyPreConfig_InitFromConfig(&preconfig, config);
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (!config->parse_argv) {
|
|
|
|
return Py_PreInitialize(&preconfig);
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
|
|
|
else if (args == NULL) {
|
2019-05-17 14:01:14 -03:00
|
|
|
_PyArgv config_args = {
|
|
|
|
.use_bytes_argv = 0,
|
2019-05-27 11:39:22 -03:00
|
|
|
.argc = config->argv.length,
|
|
|
|
.wchar_argv = config->argv.items};
|
2019-05-20 06:02:00 -03:00
|
|
|
return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
else {
|
2019-05-20 06:02:00 -03:00
|
|
|
return _Py_PreInitializeFromPyArgv(&preconfig, args);
|
2019-05-02 16:25:34 -03:00
|
|
|
}
|
2019-03-05 19:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
/* Begin interpreter initialization
|
|
|
|
*
|
|
|
|
* On return, the first thread and interpreter state have been created,
|
|
|
|
* but the compiler, signal handling, multithreading and
|
|
|
|
* multiple interpreter support, and codec infrastructure are not yet
|
|
|
|
* available.
|
|
|
|
*
|
|
|
|
* The import system will support builtin and frozen modules only.
|
|
|
|
* The only supported io is writing to sys.stderr
|
|
|
|
*
|
|
|
|
* If any operation invoked by this function fails, a fatal error is
|
|
|
|
* issued and the function does not return.
|
|
|
|
*
|
|
|
|
* Any code invoked from this function should *not* assume it has access
|
|
|
|
* to the Python C API (unless the API is explicitly listed as being
|
|
|
|
* safe to call without calling Py_Initialize first)
|
|
|
|
*/
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-05-22 19:57:57 -03:00
|
|
|
pyinit_core(_PyRuntimeState *runtime,
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyConfig *src_config,
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState **tstate_p)
|
2018-07-24 21:49:17 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2018-07-24 21:49:17 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _Py_PreInitializeFromConfig(src_config, NULL);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
2019-10-01 07:06:16 -03:00
|
|
|
_PyConfig_InitCompatConfig(&config);
|
2019-05-22 19:57:57 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyConfig_Copy(&config, src_config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-05-22 19:57:57 -03:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_Read(&config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-05-22 19:57:57 -03:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!runtime->core_initialized) {
|
2019-06-19 19:05:23 -03:00
|
|
|
status = pyinit_config(runtime, tstate_p, &config);
|
2019-05-22 19:57:57 -03:00
|
|
|
}
|
|
|
|
else {
|
2019-06-19 19:05:23 -03:00
|
|
|
status = pyinit_core_reconfigure(runtime, tstate_p, &config);
|
2019-05-22 19:57:57 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-05-22 19:57:57 -03:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(&config);
|
|
|
|
return status;
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
|
|
|
|
2019-03-27 09:40:14 -03:00
|
|
|
|
2018-07-20 12:34:23 -03:00
|
|
|
/* Py_Initialize() has already been called: update the main interpreter
|
|
|
|
configuration. Example of bpo-34008: Py_Main() called after
|
|
|
|
Py_Initialize(). */
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-03-26 21:36:16 -03:00
|
|
|
_Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
|
2018-07-20 12:34:23 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig *config = &interp->config;
|
2019-03-26 21:36:16 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyObject *argv = _PyWideStringList_AsList(&config->argv);
|
2019-03-26 21:36:16 -03:00
|
|
|
if (argv == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY(); \
|
2019-03-26 21:36:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int res = PyDict_SetItemString(interp->sysdict, "argv", argv);
|
|
|
|
Py_DECREF(argv);
|
|
|
|
if (res < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("fail to set sys.argv");
|
2018-07-20 12:34:23 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-07-20 12:34:23 -03:00
|
|
|
}
|
|
|
|
|
2017-05-24 03:00:52 -03:00
|
|
|
/* 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.
|
|
|
|
*/
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-11-19 21:27:56 -04:00
|
|
|
pyinit_main(PyThreadState *tstate)
|
2017-05-24 01:46:51 -03:00
|
|
|
{
|
2019-11-19 21:27:56 -04:00
|
|
|
_PyRuntimeState *runtime = tstate->interp->runtime;
|
2019-04-24 13:23:53 -03:00
|
|
|
if (!runtime->core_initialized) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("runtime core not initialized");
|
2017-05-24 03:00:52 -03:00
|
|
|
}
|
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
/* Configure the main interpreter */
|
2019-06-19 19:05:23 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig *config = &interp->config;
|
2017-05-24 03:00:52 -03:00
|
|
|
|
2019-04-24 13:23:53 -03:00
|
|
|
if (runtime->initialized) {
|
2019-03-26 21:36:16 -03:00
|
|
|
return _Py_ReconfigureMainInterpreter(interp);
|
2018-07-20 12:34:23 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (!config->_install_importlib) {
|
2017-05-24 01:46:51 -03:00
|
|
|
/* 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.
|
|
|
|
*/
|
2019-04-24 13:23:53 -03:00
|
|
|
runtime->initialized = 1;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
2017-11-24 22:17:57 -04:00
|
|
|
|
2017-12-05 10:12:41 -04:00
|
|
|
if (_PyTime_Init() < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't initialize time");
|
2017-12-05 10:12:41 -04:00
|
|
|
}
|
2015-04-03 08:10:54 -03:00
|
|
|
|
2019-11-19 21:27:56 -04:00
|
|
|
if (_PySys_InitMain(tstate) < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't finish initializing sys");
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2019-06-18 21:54:39 -03:00
|
|
|
PyStatus status = init_importlib_external(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* initialize the faulthandler module */
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyFaulthandler_Init(config->faulthandler);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = _PyUnicode_InitEncodings(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config->install_signal_handlers) {
|
2019-06-19 19:05:23 -03:00
|
|
|
status = init_signals(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
|
|
|
|
return _PyStatus_ERR("can't initialize tracemalloc");
|
2018-07-24 08:55:48 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = add_main_module(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = init_sys_streams(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Initialize warnings. */
|
2018-11-16 06:55:35 -04:00
|
|
|
PyObject *warnoptions = PySys_GetObject("warnoptions");
|
|
|
|
if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
|
2017-12-19 06:35:58 -04:00
|
|
|
{
|
2014-11-20 07:39:37 -04:00
|
|
|
PyObject *warnings_module = PyImport_ImportModule("warnings");
|
|
|
|
if (warnings_module == NULL) {
|
|
|
|
fprintf(stderr, "'import warnings' failed; traceback:\n");
|
2019-06-19 19:05:23 -03:00
|
|
|
_PyErr_Print(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
Py_XDECREF(warnings_module);
|
|
|
|
}
|
|
|
|
|
2019-04-24 13:23:53 -03:00
|
|
|
runtime->initialized = 1;
|
2017-05-24 01:46:51 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config->site_import) {
|
2019-06-19 19:05:23 -03:00
|
|
|
status = init_import_site();
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 17:56:06 -03:00
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
2019-04-24 13:23:53 -03:00
|
|
|
emit_stderr_warning_for_legacy_locale(runtime);
|
2018-08-29 17:56:06 -03:00
|
|
|
#endif
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
2019-05-16 12:38:16 -03:00
|
|
|
_Py_InitializeMain(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _PyRuntime_Initialize();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-16 12:38:16 -03:00
|
|
|
}
|
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
2019-11-19 21:27:56 -04:00
|
|
|
return pyinit_main(tstate);
|
2019-05-16 12:38:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
Py_InitializeFromConfig(const PyConfig *config)
|
2017-05-24 01:46:51 -03:00
|
|
|
{
|
2019-05-20 06:02:00 -03:00
|
|
|
if (config == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("initialization config is NULL");
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-04-24 13:23:53 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyRuntime_Initialize();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-04-24 13:23:53 -03:00
|
|
|
}
|
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState *tstate = NULL;
|
|
|
|
status = pyinit_core(runtime, config, &tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2019-06-19 19:05:23 -03:00
|
|
|
config = &tstate->interp->config;
|
2017-11-15 19:48:08 -04:00
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
if (config->_init_main) {
|
2019-11-19 21:27:56 -04:00
|
|
|
status = pyinit_main(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-26 22:04:16 -03:00
|
|
|
}
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2019-03-26 22:04:16 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-27 09:40:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
void
|
|
|
|
Py_InitializeEx(int install_sigs)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-04-24 13:23:53 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyRuntime_Initialize();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-04-24 13:23:53 -03:00
|
|
|
}
|
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
|
|
|
|
|
|
|
if (runtime->initialized) {
|
2018-07-24 21:49:17 -03:00
|
|
|
/* bpo-33932: Calling Py_Initialize() twice does nothing. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
2019-10-01 07:06:16 -03:00
|
|
|
_PyConfig_InitCompatConfig(&config);
|
2019-09-27 23:28:35 -03:00
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
config.install_signal_handlers = install_sigs;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Py_Initialize(void)
|
|
|
|
{
|
|
|
|
Py_InitializeEx(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COUNT_ALLOCS
|
2018-10-28 12:02:17 -03:00
|
|
|
extern void _Py_dump_counts(FILE*);
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Flush stdout and stderr */
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_is_closed(PyObject *fobj)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
|
|
|
|
if (tmp == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
r = PyObject_IsTrue(tmp);
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
if (r < 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
return r > 0;
|
|
|
|
}
|
|
|
|
|
2015-11-29 23:18:29 -04:00
|
|
|
static int
|
2014-11-20 07:39:37 -04:00
|
|
|
flush_std_files(void)
|
|
|
|
{
|
|
|
|
PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
|
|
|
|
PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
|
|
|
|
PyObject *tmp;
|
2015-11-29 23:18:29 -04:00
|
|
|
int status = 0;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
|
2019-07-08 05:19:25 -03:00
|
|
|
tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
|
2015-11-29 23:18:29 -04:00
|
|
|
if (tmp == NULL) {
|
2014-11-20 07:39:37 -04:00
|
|
|
PyErr_WriteUnraisable(fout);
|
2015-11-29 23:18:29 -04:00
|
|
|
status = -1;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
else
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
|
2019-07-08 05:19:25 -03:00
|
|
|
tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
|
2015-11-29 23:18:29 -04:00
|
|
|
if (tmp == NULL) {
|
2014-11-20 07:39:37 -04:00
|
|
|
PyErr_Clear();
|
2015-11-29 23:18:29 -04:00
|
|
|
status = -1;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
else
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
}
|
2015-11-29 23:18:29 -04:00
|
|
|
|
|
|
|
return status;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Undo the effect of Py_Initialize().
|
|
|
|
|
|
|
|
Beware: if multiple interpreter and/or thread states exist, these
|
|
|
|
are not wiped out; only the current thread and interpreter state
|
|
|
|
are deleted. But since everything else is deleted, those other
|
|
|
|
interpreter and thread states should no longer be used.
|
|
|
|
|
|
|
|
(XXX We should do better, e.g. wipe out all interpreters and
|
|
|
|
threads.)
|
|
|
|
|
|
|
|
Locking: as above.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-11-29 23:18:29 -04:00
|
|
|
int
|
|
|
|
Py_FinalizeEx(void)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2015-11-29 23:18:29 -04:00
|
|
|
int status = 0;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-04-24 12:24:01 -03:00
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
|
|
|
if (!runtime->initialized) {
|
2015-11-29 23:18:29 -04:00
|
|
|
return status;
|
2019-04-24 12:24:01 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
/* Get current thread state and interpreter pointer */
|
|
|
|
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
|
2019-03-15 18:47:51 -03:00
|
|
|
// Wrap up existing "threading"-module-created, non-daemon threads.
|
2019-06-19 19:05:23 -03:00
|
|
|
wait_for_thread_shutdown(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-03-15 18:47:51 -03:00
|
|
|
// Make any remaining pending calls.
|
2019-06-03 13:14:24 -03:00
|
|
|
_Py_FinishPendingCalls(runtime);
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* The interpreter is still entirely intact at this point, and the
|
|
|
|
* exit funcs may be relying on that. In particular, if some thread
|
|
|
|
* or exit func is still waiting to do an import, the import machinery
|
|
|
|
* expects Py_IsInitialized() to return true. So don't say the
|
2019-03-15 18:47:51 -03:00
|
|
|
* runtime is uninitialized until after the exit funcs have run.
|
2014-11-20 07:39:37 -04:00
|
|
|
* Note that Threading.py uses an exit func to do a join on all the
|
|
|
|
* threads created thru it, so this also protects pending imports in
|
|
|
|
* the threads created via Threading.
|
|
|
|
*/
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
call_py_exitfuncs(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-12-14 20:46:02 -04:00
|
|
|
/* Copy the core config, PyInterpreterState_Delete() free
|
|
|
|
the core config memory */
|
2017-12-19 06:35:58 -04:00
|
|
|
#ifdef Py_REF_DEBUG
|
2019-05-27 11:39:22 -03:00
|
|
|
int show_ref_count = interp->config.show_ref_count;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
|
|
|
#ifdef Py_TRACE_REFS
|
2019-05-27 11:39:22 -03:00
|
|
|
int dump_refs = interp->config.dump_refs;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_PYMALLOC
|
2019-05-27 11:39:22 -03:00
|
|
|
int malloc_stats = interp->config.malloc_stats;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
2017-12-06 12:26:10 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Remaining threads (e.g. daemon threads) will automatically exit
|
|
|
|
after taking the GIL (in PyEval_RestoreThread()). */
|
2019-04-24 12:24:01 -03:00
|
|
|
runtime->finalizing = tstate;
|
|
|
|
runtime->initialized = 0;
|
|
|
|
runtime->core_initialized = 0;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2015-03-24 09:46:18 -03:00
|
|
|
/* Flush sys.stdout and sys.stderr */
|
2015-11-29 23:18:29 -04:00
|
|
|
if (flush_std_files() < 0) {
|
|
|
|
status = -1;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Disable signal handling */
|
|
|
|
PyOS_FiniInterrupts();
|
|
|
|
|
|
|
|
/* Collect garbage. This may call finalizers; it's nice to call these
|
|
|
|
* before all modules are destroyed.
|
|
|
|
* XXX If a __del__ or weakref callback is triggered here, and tries to
|
|
|
|
* XXX import a module, bad things can happen, because Python no
|
|
|
|
* XXX longer believes it's initialized.
|
|
|
|
* XXX Fatal Python error: Interpreter not initialized (version mismatch?)
|
|
|
|
* XXX is easy to provoke that way. I've also seen, e.g.,
|
|
|
|
* XXX Exception exceptions.ImportError: 'No module named sha'
|
|
|
|
* XXX in <function callback at 0x008F5718> ignored
|
|
|
|
* XXX but I'm unclear on exactly how that one happens. In any case,
|
|
|
|
* XXX I haven't seen a real-life report of either of these.
|
|
|
|
*/
|
2016-09-10 01:47:46 -03:00
|
|
|
_PyGC_CollectIfEnabled();
|
2014-11-20 07:39:37 -04:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
/* With COUNT_ALLOCS, it helps to run GC multiple times:
|
|
|
|
each collection might release some types from the type
|
|
|
|
list, so they become garbage. */
|
2016-09-10 01:47:46 -03:00
|
|
|
while (_PyGC_CollectIfEnabled() > 0)
|
2014-11-20 07:39:37 -04:00
|
|
|
/* nothing */;
|
|
|
|
#endif
|
2017-09-14 04:35:58 -03:00
|
|
|
|
2019-05-23 12:45:22 -03:00
|
|
|
/* Clear all loghooks */
|
|
|
|
/* We want minimal exposure of this function, so define the extern
|
|
|
|
* here. The linker should discover the correct function without
|
|
|
|
* exporting a symbol. */
|
|
|
|
extern void _PySys_ClearAuditHooks(void);
|
|
|
|
_PySys_ClearAuditHooks();
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Destroy all modules */
|
2019-06-19 05:36:10 -03:00
|
|
|
_PyImport_Cleanup(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-03 09:30:58 -03:00
|
|
|
/* Print debug stats if any */
|
|
|
|
_PyEval_Fini();
|
|
|
|
|
2015-03-24 09:46:18 -03:00
|
|
|
/* Flush sys.stdout and sys.stderr (again, in case more was printed) */
|
2015-11-29 23:18:29 -04:00
|
|
|
if (flush_std_files() < 0) {
|
|
|
|
status = -1;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Collect final garbage. This disposes of cycles created by
|
|
|
|
* class definitions, for example.
|
|
|
|
* XXX This is disabled because it caused too many problems. If
|
|
|
|
* XXX a __del__ or weakref callback triggers here, Python code has
|
|
|
|
* XXX a hard time running, because even the sys module has been
|
|
|
|
* XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
|
|
|
|
* XXX One symptom is a sequence of information-free messages
|
|
|
|
* XXX coming from threads (if a __del__ or callback is invoked,
|
|
|
|
* XXX other threads can execute too, and any exception they encounter
|
|
|
|
* XXX triggers a comedy of errors as subsystem after subsystem
|
|
|
|
* XXX fails to find what it *expects* to find in sys to help report
|
|
|
|
* XXX the exception and consequent unexpected failures). I've also
|
|
|
|
* XXX seen segfaults then, after adding print statements to the
|
|
|
|
* XXX Python code getting called.
|
|
|
|
*/
|
|
|
|
#if 0
|
2016-09-10 01:47:46 -03:00
|
|
|
_PyGC_CollectIfEnabled();
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Disable tracemalloc after all Python objects have been destroyed,
|
|
|
|
so it is possible to use tracemalloc in objects destructor. */
|
|
|
|
_PyTraceMalloc_Fini();
|
|
|
|
|
|
|
|
/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
|
|
|
|
_PyImport_Fini();
|
|
|
|
|
|
|
|
/* Cleanup typeobject.c's internal caches. */
|
|
|
|
_PyType_Fini();
|
|
|
|
|
|
|
|
/* unload faulthandler module */
|
|
|
|
_PyFaulthandler_Fini();
|
|
|
|
|
|
|
|
/* Debugging stuff */
|
|
|
|
#ifdef COUNT_ALLOCS
|
2018-10-28 12:02:17 -03:00
|
|
|
_Py_dump_counts(stderr);
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
/* dump hash stats */
|
|
|
|
_PyHash_Fini();
|
|
|
|
|
2017-09-14 04:35:58 -03:00
|
|
|
#ifdef Py_REF_DEBUG
|
2017-12-14 20:46:02 -04:00
|
|
|
if (show_ref_count) {
|
2017-11-20 22:12:22 -04:00
|
|
|
_PyDebug_PrintTotalRefs();
|
|
|
|
}
|
2017-09-14 04:35:58 -03:00
|
|
|
#endif
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
#ifdef Py_TRACE_REFS
|
|
|
|
/* Display all objects still alive -- this can invoke arbitrary
|
|
|
|
* __repr__ overrides, so requires a mostly-intact interpreter.
|
|
|
|
* Alas, a lot of stuff may still be alive now that will be cleaned
|
|
|
|
* up later.
|
|
|
|
*/
|
2017-12-14 20:46:02 -04:00
|
|
|
if (dump_refs) {
|
2014-11-20 07:39:37 -04:00
|
|
|
_Py_PrintReferences(stderr);
|
2017-12-06 12:26:10 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif /* Py_TRACE_REFS */
|
|
|
|
|
|
|
|
/* Clear interpreter state and all thread states. */
|
|
|
|
PyInterpreterState_Clear(interp);
|
|
|
|
|
|
|
|
/* Now we decref the exception classes. After this point nothing
|
|
|
|
can raise an exception. That's okay, because each Fini() method
|
|
|
|
below has been checked to make sure no exceptions are ever
|
|
|
|
raised.
|
|
|
|
*/
|
|
|
|
|
|
|
|
_PyExc_Fini();
|
|
|
|
|
|
|
|
/* Sundry finalizers */
|
2019-08-26 19:12:32 -03:00
|
|
|
_PyMethod_Fini();
|
|
|
|
_PyFrame_Fini();
|
|
|
|
_PyCFunction_Fini();
|
|
|
|
_PyTuple_Fini();
|
|
|
|
_PyList_Fini();
|
|
|
|
_PySet_Fini();
|
|
|
|
_PyBytes_Fini();
|
|
|
|
_PyLong_Fini();
|
|
|
|
_PyFloat_Fini();
|
|
|
|
_PyDict_Fini();
|
|
|
|
_PySlice_Fini();
|
2019-04-24 12:24:01 -03:00
|
|
|
_PyGC_Fini(runtime);
|
2019-05-10 14:29:55 -03:00
|
|
|
_PyWarnings_Fini(interp);
|
2017-05-23 01:36:03 -03:00
|
|
|
_Py_HashRandomization_Fini();
|
2016-08-14 04:52:18 -03:00
|
|
|
_PyArg_Fini();
|
2019-08-26 19:12:32 -03:00
|
|
|
_PyAsyncGen_Fini();
|
2018-01-22 20:11:18 -04:00
|
|
|
_PyContext_Fini();
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Cleanup Unicode implementation */
|
|
|
|
_PyUnicode_Fini();
|
|
|
|
|
2018-08-29 08:25:36 -03:00
|
|
|
_Py_ClearFileSystemEncoding();
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* XXX Still allocated:
|
|
|
|
- various static ad-hoc pointers to interned strings
|
|
|
|
- int and float free list blocks
|
|
|
|
- whatever various modules and libraries allocate
|
|
|
|
*/
|
|
|
|
|
|
|
|
PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
|
|
|
|
|
|
|
|
/* Cleanup auto-thread-state */
|
2019-04-24 12:24:01 -03:00
|
|
|
_PyGILState_Fini(runtime);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Delete current thread. After this, many C API calls become crashy. */
|
|
|
|
PyThreadState_Swap(NULL);
|
2016-03-14 18:07:55 -03:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
PyInterpreterState_Delete(interp);
|
|
|
|
|
|
|
|
#ifdef Py_TRACE_REFS
|
|
|
|
/* Display addresses (& refcnts) of all objects still alive.
|
|
|
|
* An address can be used to find the repr of the object, printed
|
|
|
|
* above by _Py_PrintReferences.
|
|
|
|
*/
|
2017-12-14 20:46:02 -04:00
|
|
|
if (dump_refs) {
|
2014-11-20 07:39:37 -04:00
|
|
|
_Py_PrintReferenceAddresses(stderr);
|
2017-12-06 12:26:10 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif /* Py_TRACE_REFS */
|
2016-03-14 08:04:26 -03:00
|
|
|
#ifdef WITH_PYMALLOC
|
2017-12-14 20:46:02 -04:00
|
|
|
if (malloc_stats) {
|
2017-12-06 12:26:10 -04:00
|
|
|
_PyObject_DebugMallocStats(stderr);
|
2016-03-14 08:04:26 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
|
2019-04-24 12:24:01 -03:00
|
|
|
call_ll_exitfuncs(runtime);
|
2017-11-24 22:17:57 -04:00
|
|
|
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime_Finalize();
|
2015-11-29 23:18:29 -04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Py_Finalize(void)
|
|
|
|
{
|
|
|
|
Py_FinalizeEx();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and initialize a new interpreter and thread, and return the
|
|
|
|
new thread. This requires that Py_Initialize() has been called
|
|
|
|
first.
|
|
|
|
|
|
|
|
Unsuccessful initialization yields a NULL pointer. Note that *no*
|
|
|
|
exception information is available even in this case -- the
|
|
|
|
exception information is held in the thread, and there is no
|
|
|
|
thread.
|
|
|
|
|
|
|
|
Locking: as above.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2017-11-15 22:11:45 -04:00
|
|
|
new_interpreter(PyThreadState **tstate_p)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyRuntime_Initialize();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-04-24 13:23:53 -03:00
|
|
|
}
|
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
|
|
|
|
|
|
|
if (!runtime->initialized) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("Py_Initialize must be called first");
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2016-03-14 18:07:55 -03:00
|
|
|
/* Issue #10915, #15751: The GIL API doesn't work with multiple
|
|
|
|
interpreters: disable PyGILState_Check(). */
|
|
|
|
_PyGILState_check_enabled = 0;
|
|
|
|
|
2019-04-24 13:23:53 -03:00
|
|
|
PyInterpreterState *interp = PyInterpreterState_New();
|
2017-11-15 22:11:45 -04:00
|
|
|
if (interp == NULL) {
|
|
|
|
*tstate_p = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-04-24 13:23:53 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_New(interp);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (tstate == NULL) {
|
|
|
|
PyInterpreterState_Delete(interp);
|
2017-11-15 22:11:45 -04:00
|
|
|
*tstate_p = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2019-04-24 13:23:53 -03:00
|
|
|
PyThreadState *save_tstate = PyThreadState_Swap(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
/* Copy the current interpreter config into the new interpreter */
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig *config;
|
2017-05-24 01:46:51 -03:00
|
|
|
if (save_tstate != NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
config = &save_tstate->interp->config;
|
2017-05-24 01:46:51 -03:00
|
|
|
} else {
|
|
|
|
/* No current thread state, copy from the main interpreter */
|
|
|
|
PyInterpreterState *main_interp = PyInterpreterState_Main();
|
2019-05-27 11:39:22 -03:00
|
|
|
config = &main_interp->config;
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyConfig_Copy(&interp->config, config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
config = &interp->config;
|
2017-05-24 01:46:51 -03:00
|
|
|
|
2019-11-19 21:27:56 -04:00
|
|
|
status = pycore_init_types(tstate);
|
2019-05-22 06:28:22 -03:00
|
|
|
|
2017-11-24 22:17:57 -04:00
|
|
|
/* XXX The following is lax in error checking */
|
2017-09-14 15:18:12 -03:00
|
|
|
PyObject *modules = PyDict_New();
|
2017-11-15 22:11:45 -04:00
|
|
|
if (modules == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't make modules dictionary");
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
interp->modules = modules;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-18 21:54:39 -03:00
|
|
|
PyObject *sysmod = _PyImport_FindBuiltin(tstate, "sys");
|
2017-09-14 15:18:12 -03:00
|
|
|
if (sysmod != NULL) {
|
|
|
|
interp->sysdict = PyModule_GetDict(sysmod);
|
2019-04-24 13:23:53 -03:00
|
|
|
if (interp->sysdict == NULL) {
|
2017-09-14 15:18:12 -03:00
|
|
|
goto handle_error;
|
2019-04-24 13:23:53 -03:00
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
Py_INCREF(interp->sysdict);
|
|
|
|
PyDict_SetItemString(interp->sysdict, "modules", modules);
|
2019-11-19 21:27:56 -04:00
|
|
|
if (_PySys_InitMain(tstate) < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't finish initializing sys");
|
2019-01-23 10:04:40 -04:00
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
}
|
2019-06-19 19:05:23 -03:00
|
|
|
else if (_PyErr_Occurred(tstate)) {
|
2018-12-11 02:38:03 -04:00
|
|
|
goto handle_error;
|
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
|
2019-06-18 21:54:39 -03:00
|
|
|
PyObject *bimod = _PyImport_FindBuiltin(tstate, "builtins");
|
2014-11-20 07:39:37 -04:00
|
|
|
if (bimod != NULL) {
|
|
|
|
interp->builtins = PyModule_GetDict(bimod);
|
|
|
|
if (interp->builtins == NULL)
|
|
|
|
goto handle_error;
|
|
|
|
Py_INCREF(interp->builtins);
|
|
|
|
}
|
2019-06-19 19:05:23 -03:00
|
|
|
else if (_PyErr_Occurred(tstate)) {
|
2018-12-11 02:38:03 -04:00
|
|
|
goto handle_error;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
if (bimod != NULL && sysmod != NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyBuiltins_AddExceptions(bimod);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PySys_SetPreliminaryStderr(interp->sysdict);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = _PyImportHooks_Init(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = init_importlib(tstate, sysmod);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-18 21:54:39 -03:00
|
|
|
status = init_importlib_external(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = _PyUnicode_InitEncodings(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
status = init_sys_streams(tstate);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = add_main_module(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config->site_import) {
|
2019-06-19 19:05:23 -03:00
|
|
|
status = init_import_site();
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
if (_PyErr_Occurred(tstate)) {
|
2017-11-15 22:11:45 -04:00
|
|
|
goto handle_error;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
*tstate_p = tstate;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2017-11-15 19:48:08 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
handle_error:
|
|
|
|
/* Oops, it didn't work. Undo it all. */
|
|
|
|
|
|
|
|
PyErr_PrintEx(0);
|
|
|
|
PyThreadState_Clear(tstate);
|
|
|
|
PyThreadState_Swap(save_tstate);
|
|
|
|
PyThreadState_Delete(tstate);
|
|
|
|
PyInterpreterState_Delete(interp);
|
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
*tstate_p = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadState *
|
|
|
|
Py_NewInterpreter(void)
|
|
|
|
{
|
2019-03-18 13:10:29 -03:00
|
|
|
PyThreadState *tstate = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = new_interpreter(&tstate);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
|
|
|
return tstate;
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete an interpreter and its last thread. This requires that the
|
|
|
|
given thread state is current, that the thread has no remaining
|
|
|
|
frames, and that it is its interpreter's only remaining thread.
|
|
|
|
It is a fatal error to violate these constraints.
|
|
|
|
|
2015-11-29 23:18:29 -04:00
|
|
|
(Py_FinalizeEx() doesn't have these constraints -- it zaps
|
2014-11-20 07:39:37 -04:00
|
|
|
everything, regardless.)
|
|
|
|
|
|
|
|
Locking: as above.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
Py_EndInterpreter(PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
if (tstate != _PyThreadState_GET()) {
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_FatalError("Py_EndInterpreter: thread is not current");
|
2019-06-19 19:05:23 -03:00
|
|
|
}
|
|
|
|
if (tstate->frame != NULL) {
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_FatalError("Py_EndInterpreter: thread still has a frame");
|
2019-06-19 19:05:23 -03:00
|
|
|
}
|
2019-03-09 01:47:07 -04:00
|
|
|
interp->finalizing = 1;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-03-15 18:47:51 -03:00
|
|
|
// Wrap up existing "threading"-module-created, non-daemon threads.
|
2019-06-19 19:05:23 -03:00
|
|
|
wait_for_thread_shutdown(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
call_py_exitfuncs(tstate);
|
2017-12-20 06:17:58 -04:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
if (tstate != interp->tstate_head || tstate->next != NULL) {
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_FatalError("Py_EndInterpreter: not the last thread");
|
2019-06-19 19:05:23 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-06-19 05:36:10 -03:00
|
|
|
_PyImport_Cleanup(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
PyInterpreterState_Clear(interp);
|
|
|
|
PyThreadState_Swap(NULL);
|
|
|
|
PyInterpreterState_Delete(interp);
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
/* Add the __main__ module */
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2017-11-15 19:48:08 -04:00
|
|
|
add_main_module(PyInterpreterState *interp)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2016-09-09 00:50:03 -03:00
|
|
|
PyObject *m, *d, *loader, *ann_dict;
|
2014-11-20 07:39:37 -04:00
|
|
|
m = PyImport_AddModule("__main__");
|
|
|
|
if (m == NULL)
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't create __main__ module");
|
2017-11-15 19:48:08 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
d = PyModule_GetDict(m);
|
2016-09-09 00:50:03 -03:00
|
|
|
ann_dict = PyDict_New();
|
|
|
|
if ((ann_dict == NULL) ||
|
|
|
|
(PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
|
2016-09-09 00:50:03 -03:00
|
|
|
}
|
|
|
|
Py_DECREF(ann_dict);
|
2017-11-15 19:48:08 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
|
|
|
|
PyObject *bimod = PyImport_ImportModule("builtins");
|
|
|
|
if (bimod == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("Failed to retrieve builtins module");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(bimod);
|
|
|
|
}
|
2017-11-15 19:48:08 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Main is a little special - imp.is_builtin("__main__") will return
|
|
|
|
* False, but BuiltinImporter is still the most appropriate initial
|
|
|
|
* setting for its __loader__ attribute. A more suitable value will
|
|
|
|
* be set if __main__ gets further initialized later in the startup
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
loader = PyDict_GetItemString(d, "__loader__");
|
|
|
|
if (loader == NULL || loader == Py_None) {
|
|
|
|
PyObject *loader = PyObject_GetAttrString(interp->importlib,
|
|
|
|
"BuiltinImporter");
|
|
|
|
if (loader == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("Failed to initialize __main__.__loader__");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(loader);
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Import the site module (not into __main__ though) */
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-06-19 19:05:23 -03:00
|
|
|
init_import_site(void)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
PyObject *m;
|
|
|
|
m = PyImport_ImportModule("site");
|
|
|
|
if (m == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("Failed to import the site module");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2017-11-15 19:48:08 -04:00
|
|
|
Py_DECREF(m);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 12:29:57 -03:00
|
|
|
/* Check if a file descriptor is valid or not.
|
|
|
|
Return 0 if the file descriptor is invalid, return non-zero otherwise. */
|
|
|
|
static int
|
|
|
|
is_valid_fd(int fd)
|
|
|
|
{
|
2019-04-17 13:09:12 -03:00
|
|
|
/* dup() is faster than fstat(): fstat() can require input/output operations,
|
|
|
|
whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
|
|
|
|
startup. Problem: dup() doesn't check if the file descriptor is valid on
|
|
|
|
some platforms.
|
|
|
|
|
|
|
|
bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
|
|
|
|
side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
|
|
|
|
EBADF. FreeBSD has similar issue (bpo-32849).
|
|
|
|
|
|
|
|
Only use dup() on platforms where dup() is enough to detect invalid FD in
|
|
|
|
corner cases: on Linux and Windows (bpo-32849). */
|
|
|
|
#if defined(__linux__) || defined(MS_WINDOWS)
|
|
|
|
if (fd < 0) {
|
2015-09-04 12:29:57 -03:00
|
|
|
return 0;
|
2019-04-17 13:09:12 -03:00
|
|
|
}
|
|
|
|
int fd2;
|
|
|
|
|
2015-09-04 12:29:57 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
|
|
|
fd2 = dup(fd);
|
2019-04-17 13:09:12 -03:00
|
|
|
if (fd2 >= 0) {
|
2015-09-04 12:29:57 -03:00
|
|
|
close(fd2);
|
2019-04-17 13:09:12 -03:00
|
|
|
}
|
2015-09-04 12:29:57 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2019-04-17 13:09:12 -03:00
|
|
|
|
|
|
|
return (fd2 >= 0);
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
return (fstat(fd, &st) == 0);
|
2017-05-03 19:45:56 -03:00
|
|
|
#endif
|
2015-09-04 12:29:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns Py_None if the fd is not valid */
|
2014-11-20 07:39:37 -04:00
|
|
|
static PyObject*
|
2019-05-27 11:39:22 -03:00
|
|
|
create_stdio(const PyConfig *config, PyObject* io,
|
2015-12-25 14:01:53 -04:00
|
|
|
int fd, int write_mode, const char* name,
|
2019-05-02 15:56:30 -03:00
|
|
|
const wchar_t* encoding, const wchar_t* errors)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
|
|
|
|
const char* mode;
|
|
|
|
const char* newline;
|
2017-10-04 14:25:40 -03:00
|
|
|
PyObject *line_buffering, *write_through;
|
2014-11-20 07:39:37 -04:00
|
|
|
int buffering, isatty;
|
|
|
|
_Py_IDENTIFIER(open);
|
|
|
|
_Py_IDENTIFIER(isatty);
|
|
|
|
_Py_IDENTIFIER(TextIOWrapper);
|
|
|
|
_Py_IDENTIFIER(mode);
|
2018-08-29 19:50:45 -03:00
|
|
|
const int buffered_stdio = config->buffered_stdio;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2015-09-04 12:29:57 -03:00
|
|
|
if (!is_valid_fd(fd))
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* stdin is always opened in buffered mode, first because it shouldn't
|
|
|
|
make a difference in common use cases, second because TextIOWrapper
|
|
|
|
depends on the presence of a read1() method which only exists on
|
|
|
|
buffered streams.
|
|
|
|
*/
|
2018-08-29 19:50:45 -03:00
|
|
|
if (!buffered_stdio && write_mode)
|
2014-11-20 07:39:37 -04:00
|
|
|
buffering = 0;
|
|
|
|
else
|
|
|
|
buffering = -1;
|
|
|
|
if (write_mode)
|
|
|
|
mode = "wb";
|
|
|
|
else
|
|
|
|
mode = "rb";
|
2019-09-01 06:16:51 -03:00
|
|
|
buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
|
2014-11-20 07:39:37 -04:00
|
|
|
fd, mode, buffering,
|
2015-11-29 23:18:29 -04:00
|
|
|
Py_None, Py_None, /* encoding, errors */
|
2019-09-01 06:16:51 -03:00
|
|
|
Py_None, Py_False); /* newline, closefd */
|
2014-11-20 07:39:37 -04:00
|
|
|
if (buf == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (buffering) {
|
|
|
|
_Py_IDENTIFIER(raw);
|
|
|
|
raw = _PyObject_GetAttrId(buf, &PyId_raw);
|
|
|
|
if (raw == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
raw = buf;
|
|
|
|
Py_INCREF(raw);
|
|
|
|
}
|
|
|
|
|
2016-08-31 01:22:36 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/* Windows console IO is always UTF-8 encoded */
|
|
|
|
if (PyWindowsConsoleIO_Check(raw))
|
2019-05-02 15:56:30 -03:00
|
|
|
encoding = L"utf-8";
|
2016-08-31 01:22:36 -03:00
|
|
|
#endif
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
text = PyUnicode_FromString(name);
|
|
|
|
if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
|
|
|
|
goto error;
|
2019-07-08 05:19:25 -03:00
|
|
|
res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (res == NULL)
|
|
|
|
goto error;
|
|
|
|
isatty = PyObject_IsTrue(res);
|
|
|
|
Py_DECREF(res);
|
|
|
|
if (isatty == -1)
|
|
|
|
goto error;
|
2018-08-29 19:50:45 -03:00
|
|
|
if (!buffered_stdio)
|
2017-10-04 14:25:40 -03:00
|
|
|
write_through = Py_True;
|
|
|
|
else
|
|
|
|
write_through = Py_False;
|
2018-08-29 19:50:45 -03:00
|
|
|
if (isatty && buffered_stdio)
|
2014-11-20 07:39:37 -04:00
|
|
|
line_buffering = Py_True;
|
|
|
|
else
|
|
|
|
line_buffering = Py_False;
|
|
|
|
|
|
|
|
Py_CLEAR(raw);
|
|
|
|
Py_CLEAR(text);
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
|
|
|
|
newlines to "\n".
|
|
|
|
sys.stdout and sys.stderr: translate "\n" to "\r\n". */
|
|
|
|
newline = NULL;
|
|
|
|
#else
|
|
|
|
/* sys.stdin: split lines at "\n".
|
|
|
|
sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
|
|
|
|
newline = "\n";
|
|
|
|
#endif
|
|
|
|
|
2019-05-02 15:56:30 -03:00
|
|
|
PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
|
|
|
|
if (encoding_str == NULL) {
|
|
|
|
Py_CLEAR(buf);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
|
|
|
|
if (errors_str == NULL) {
|
|
|
|
Py_CLEAR(buf);
|
|
|
|
Py_CLEAR(encoding_str);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
|
|
|
|
buf, encoding_str, errors_str,
|
2017-10-04 14:25:40 -03:00
|
|
|
newline, line_buffering, write_through);
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_CLEAR(buf);
|
2019-05-02 15:56:30 -03:00
|
|
|
Py_CLEAR(encoding_str);
|
|
|
|
Py_CLEAR(errors_str);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (stream == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (write_mode)
|
|
|
|
mode = "w";
|
|
|
|
else
|
|
|
|
mode = "r";
|
|
|
|
text = PyUnicode_FromString(mode);
|
|
|
|
if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
|
|
|
|
goto error;
|
|
|
|
Py_CLEAR(text);
|
|
|
|
return stream;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(buf);
|
|
|
|
Py_XDECREF(stream);
|
|
|
|
Py_XDECREF(text);
|
|
|
|
Py_XDECREF(raw);
|
|
|
|
|
2015-09-04 12:29:57 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
|
|
|
|
/* Issue #24891: the file descriptor was closed after the first
|
|
|
|
is_valid_fd() check was called. Ignore the OSError and set the
|
|
|
|
stream to None. */
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
return NULL;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize sys.stdin, stdout, stderr and builtins.open */
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-06-19 19:05:23 -03:00
|
|
|
init_sys_streams(PyThreadState *tstate)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
PyObject *iomod = NULL, *wrapper;
|
|
|
|
PyObject *bimod = NULL;
|
|
|
|
PyObject *m;
|
|
|
|
PyObject *std = NULL;
|
2017-11-15 22:11:45 -04:00
|
|
|
int fd;
|
2014-11-20 07:39:37 -04:00
|
|
|
PyObject * encoding_attr;
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus res = _PyStatus_OK();
|
2019-06-19 19:05:23 -03:00
|
|
|
const PyConfig *config = &tstate->interp->config;
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2019-01-22 12:39:03 -04:00
|
|
|
/* Check that stdin is not a directory
|
|
|
|
Using shell redirection, you can redirect stdin to a directory,
|
|
|
|
crashing the Python interpreter. Catch this common mistake here
|
|
|
|
and output a useful error message. Note that under MS Windows,
|
|
|
|
the shell already prevents that. */
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
struct _Py_stat_struct sb;
|
|
|
|
if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
|
|
|
|
S_ISDIR(sb.st_mode)) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("<stdin> is a directory, cannot continue");
|
2019-01-22 12:39:03 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Hack to avoid a nasty recursion issue when Python is invoked
|
|
|
|
in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
|
|
|
|
if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(m);
|
|
|
|
|
|
|
|
if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(m);
|
|
|
|
|
|
|
|
if (!(bimod = PyImport_ImportModule("builtins"))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(iomod = PyImport_ImportModule("io"))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set builtins.open */
|
|
|
|
if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
|
|
|
|
Py_DECREF(wrapper);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(wrapper);
|
|
|
|
|
|
|
|
/* Set sys.stdin */
|
|
|
|
fd = fileno(stdin);
|
|
|
|
/* Under some conditions stdin, stdout and stderr may not be connected
|
|
|
|
* and fileno() may point to an invalid file descriptor. For example
|
|
|
|
* GUI apps don't have valid standard streams by default.
|
|
|
|
*/
|
2018-08-29 19:50:45 -03:00
|
|
|
std = create_stdio(config, iomod, fd, 0, "<stdin>",
|
2018-08-29 06:47:29 -03:00
|
|
|
config->stdio_encoding,
|
|
|
|
config->stdio_errors);
|
2015-09-04 12:29:57 -03:00
|
|
|
if (std == NULL)
|
|
|
|
goto error;
|
2014-11-20 07:39:37 -04:00
|
|
|
PySys_SetObject("__stdin__", std);
|
|
|
|
_PySys_SetObjectId(&PyId_stdin, std);
|
|
|
|
Py_DECREF(std);
|
|
|
|
|
|
|
|
/* Set sys.stdout */
|
|
|
|
fd = fileno(stdout);
|
2018-08-29 19:50:45 -03:00
|
|
|
std = create_stdio(config, iomod, fd, 1, "<stdout>",
|
2018-08-29 06:47:29 -03:00
|
|
|
config->stdio_encoding,
|
|
|
|
config->stdio_errors);
|
2015-09-04 12:29:57 -03:00
|
|
|
if (std == NULL)
|
|
|
|
goto error;
|
2014-11-20 07:39:37 -04:00
|
|
|
PySys_SetObject("__stdout__", std);
|
|
|
|
_PySys_SetObjectId(&PyId_stdout, std);
|
|
|
|
Py_DECREF(std);
|
|
|
|
|
|
|
|
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
|
|
|
|
/* Set sys.stderr, replaces the preliminary stderr */
|
|
|
|
fd = fileno(stderr);
|
2018-08-29 19:50:45 -03:00
|
|
|
std = create_stdio(config, iomod, fd, 1, "<stderr>",
|
2018-08-29 06:47:29 -03:00
|
|
|
config->stdio_encoding,
|
2019-05-02 15:56:30 -03:00
|
|
|
L"backslashreplace");
|
2015-09-04 12:29:57 -03:00
|
|
|
if (std == NULL)
|
|
|
|
goto error;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Same as hack above, pre-import stderr's codec to avoid recursion
|
|
|
|
when import.c tries to write to stderr in verbose mode. */
|
|
|
|
encoding_attr = PyObject_GetAttrString(std, "encoding");
|
|
|
|
if (encoding_attr != NULL) {
|
2016-11-20 04:16:47 -04:00
|
|
|
const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (std_encoding != NULL) {
|
|
|
|
PyObject *codec_info = _PyCodec_Lookup(std_encoding);
|
|
|
|
Py_XDECREF(codec_info);
|
|
|
|
}
|
|
|
|
Py_DECREF(encoding_attr);
|
|
|
|
}
|
2019-06-19 19:05:23 -03:00
|
|
|
_PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
if (PySys_SetObject("__stderr__", std) < 0) {
|
|
|
|
Py_DECREF(std);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
|
|
|
|
Py_DECREF(std);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(std);
|
|
|
|
#endif
|
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
goto done;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
error:
|
2019-05-27 11:39:22 -03:00
|
|
|
res = _PyStatus_ERR("can't initialize sys standard streams");
|
2017-11-15 22:11:45 -04:00
|
|
|
|
|
|
|
done:
|
2018-08-28 20:29:06 -03:00
|
|
|
_Py_ClearStandardStreamEncoding();
|
2017-12-20 18:41:38 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_XDECREF(bimod);
|
|
|
|
Py_XDECREF(iomod);
|
2017-11-15 22:11:45 -04:00
|
|
|
return res;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-14 12:53:12 -03:00
|
|
|
static void
|
2019-09-17 20:35:33 -03:00
|
|
|
_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
|
|
|
|
PyThreadState *tstate)
|
2016-03-14 12:53:12 -03:00
|
|
|
{
|
|
|
|
fputc('\n', stderr);
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
/* display the current Python stack */
|
2019-09-17 20:35:33 -03:00
|
|
|
_Py_DumpTracebackThreads(fd, interp, tstate);
|
2016-03-14 12:53:12 -03:00
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:30 -03:00
|
|
|
/* Print the current exception (if an exception is set) with its traceback,
|
2016-03-14 12:53:12 -03:00
|
|
|
or display the current Python stack.
|
2015-03-24 08:01:30 -03:00
|
|
|
|
2016-03-14 12:53:12 -03:00
|
|
|
Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
|
|
|
|
called on catastrophic cases.
|
|
|
|
|
|
|
|
Return 1 if the traceback was displayed, 0 otherwise. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
_Py_FatalError_PrintExc(int fd)
|
2015-03-24 08:01:30 -03:00
|
|
|
{
|
2019-06-19 19:05:23 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2015-03-24 09:46:18 -03:00
|
|
|
PyObject *ferr, *res;
|
2015-03-24 08:01:30 -03:00
|
|
|
PyObject *exception, *v, *tb;
|
|
|
|
int has_tb;
|
2016-03-14 12:53:12 -03:00
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
_PyErr_Fetch(tstate, &exception, &v, &tb);
|
2015-03-24 08:01:30 -03:00
|
|
|
if (exception == NULL) {
|
|
|
|
/* No current exception */
|
2016-03-14 12:53:12 -03:00
|
|
|
return 0;
|
2015-03-24 08:01:30 -03:00
|
|
|
}
|
|
|
|
|
2015-03-24 09:46:18 -03:00
|
|
|
ferr = _PySys_GetObjectId(&PyId_stderr);
|
|
|
|
if (ferr == NULL || ferr == Py_None) {
|
|
|
|
/* sys.stderr is not set yet or set to None,
|
|
|
|
no need to try to display the exception */
|
2016-03-14 12:53:12 -03:00
|
|
|
return 0;
|
2015-03-24 09:46:18 -03:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
|
2015-03-24 08:01:30 -03:00
|
|
|
if (tb == NULL) {
|
|
|
|
tb = Py_None;
|
|
|
|
Py_INCREF(tb);
|
|
|
|
}
|
|
|
|
PyException_SetTraceback(v, tb);
|
|
|
|
if (exception == NULL) {
|
2015-03-24 09:46:18 -03:00
|
|
|
/* PyErr_NormalizeException() failed */
|
2016-03-14 12:53:12 -03:00
|
|
|
return 0;
|
2015-03-24 08:01:30 -03:00
|
|
|
}
|
|
|
|
|
2015-04-16 12:25:45 -03:00
|
|
|
has_tb = (tb != Py_None);
|
2015-03-24 08:01:30 -03:00
|
|
|
PyErr_Display(exception, v, tb);
|
|
|
|
Py_XDECREF(exception);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
2015-03-24 09:46:18 -03:00
|
|
|
|
|
|
|
/* sys.stderr may be buffered: call sys.stderr.flush() */
|
2019-07-08 05:19:25 -03:00
|
|
|
res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
|
2019-06-19 19:05:23 -03:00
|
|
|
if (res == NULL) {
|
|
|
|
_PyErr_Clear(tstate);
|
|
|
|
}
|
|
|
|
else {
|
2015-03-24 09:46:18 -03:00
|
|
|
Py_DECREF(res);
|
2019-06-19 19:05:23 -03:00
|
|
|
}
|
2015-03-24 09:46:18 -03:00
|
|
|
|
2016-03-14 12:53:12 -03:00
|
|
|
return has_tb;
|
2015-03-24 08:01:30 -03:00
|
|
|
}
|
2016-03-14 12:53:12 -03:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Print fatal error message and abort */
|
|
|
|
|
2017-10-04 13:50:12 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
static void
|
|
|
|
fatal_output_debug(const char *msg)
|
|
|
|
{
|
|
|
|
/* buffer of 256 bytes allocated on the stack */
|
|
|
|
WCHAR buffer[256 / sizeof(WCHAR)];
|
|
|
|
size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
|
|
|
|
size_t msglen;
|
|
|
|
|
|
|
|
OutputDebugStringW(L"Fatal Python error: ");
|
|
|
|
|
|
|
|
msglen = strlen(msg);
|
|
|
|
while (msglen) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (buflen > msglen) {
|
|
|
|
buflen = msglen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the message to wchar_t. This uses a simple one-to-one
|
|
|
|
conversion, assuming that the this error message actually uses
|
|
|
|
ASCII only. If this ceases to be true, we will have to convert. */
|
|
|
|
for (i=0; i < buflen; ++i) {
|
|
|
|
buffer[i] = msg[i];
|
|
|
|
}
|
|
|
|
buffer[i] = L'\0';
|
|
|
|
OutputDebugStringW(buffer);
|
|
|
|
|
|
|
|
msg += buflen;
|
|
|
|
msglen -= buflen;
|
|
|
|
}
|
|
|
|
OutputDebugStringW(L"\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-17 20:35:33 -03:00
|
|
|
|
|
|
|
static void
|
|
|
|
fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
|
|
|
|
{
|
|
|
|
fprintf(stream, "Python runtime state: ");
|
|
|
|
if (runtime->finalizing) {
|
|
|
|
fprintf(stream, "finalizing (tstate=%p)", runtime->finalizing);
|
|
|
|
}
|
|
|
|
else if (runtime->initialized) {
|
|
|
|
fprintf(stream, "initialized");
|
|
|
|
}
|
|
|
|
else if (runtime->core_initialized) {
|
|
|
|
fprintf(stream, "core initialized");
|
|
|
|
}
|
|
|
|
else if (runtime->preinitialized) {
|
|
|
|
fprintf(stream, "preinitialized");
|
|
|
|
}
|
|
|
|
else if (runtime->preinitializing) {
|
|
|
|
fprintf(stream, "preinitializing");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stream, "unknown");
|
|
|
|
}
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
fflush(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-25 17:02:55 -04:00
|
|
|
static void _Py_NO_RETURN
|
2017-11-15 19:48:08 -04:00
|
|
|
fatal_error(const char *prefix, const char *msg, int status)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2019-09-17 20:35:33 -03:00
|
|
|
FILE *stream = stderr;
|
|
|
|
const int fd = fileno(stream);
|
2015-03-24 21:55:14 -03:00
|
|
|
static int reentrant = 0;
|
|
|
|
|
|
|
|
if (reentrant) {
|
|
|
|
/* Py_FatalError() caused a second fatal error.
|
|
|
|
Example: flush_std_files() raises a recursion error. */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
reentrant = 1;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-09-17 20:35:33 -03:00
|
|
|
fprintf(stream, "Fatal Python error: ");
|
2017-11-15 19:48:08 -04:00
|
|
|
if (prefix) {
|
2019-09-17 20:35:33 -03:00
|
|
|
fputs(prefix, stream);
|
|
|
|
fputs(": ", stream);
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
if (msg) {
|
2019-09-17 20:35:33 -03:00
|
|
|
fputs(msg, stream);
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-09-17 20:35:33 -03:00
|
|
|
fprintf(stream, "<message not set>");
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2019-09-17 20:35:33 -03:00
|
|
|
fputs("\n", stream);
|
|
|
|
fflush(stream); /* it helps in Windows debug build */
|
2015-03-24 08:01:30 -03:00
|
|
|
|
2019-09-17 20:35:33 -03:00
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
|
|
|
fatal_error_dump_runtime(stream, runtime);
|
|
|
|
|
|
|
|
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
|
|
|
PyInterpreterState *interp = NULL;
|
|
|
|
if (tstate != NULL) {
|
|
|
|
interp = tstate->interp;
|
2018-10-31 20:26:41 -03:00
|
|
|
}
|
|
|
|
|
2019-09-17 20:35:33 -03:00
|
|
|
/* Check if the current thread has a Python thread state
|
|
|
|
and holds the GIL.
|
|
|
|
|
|
|
|
tss_tstate is NULL if Py_FatalError() is called from a C thread which
|
|
|
|
has no Python thread state.
|
|
|
|
|
|
|
|
tss_tstate != tstate if the current Python thread does not hold the GIL.
|
|
|
|
*/
|
|
|
|
PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
|
|
|
|
int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
|
2018-10-31 20:26:41 -03:00
|
|
|
if (has_tstate_and_gil) {
|
|
|
|
/* If an exception is set, print the exception with its traceback */
|
|
|
|
if (!_Py_FatalError_PrintExc(fd)) {
|
|
|
|
/* No exception is set, or an exception is set without traceback */
|
2019-09-17 20:35:33 -03:00
|
|
|
_Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
|
2018-10-31 20:26:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-09-17 20:35:33 -03:00
|
|
|
_Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
|
2017-10-04 13:50:12 -03:00
|
|
|
}
|
2015-03-24 08:01:30 -03:00
|
|
|
|
2017-10-04 13:50:12 -03:00
|
|
|
/* The main purpose of faulthandler is to display the traceback.
|
|
|
|
This function already did its best to display a traceback.
|
|
|
|
Disable faulthandler to prevent writing a second traceback
|
|
|
|
on abort(). */
|
2016-03-16 19:19:15 -03:00
|
|
|
_PyFaulthandler_Fini();
|
|
|
|
|
2016-03-14 12:53:12 -03:00
|
|
|
/* Check if the current Python thread hold the GIL */
|
2018-10-31 20:26:41 -03:00
|
|
|
if (has_tstate_and_gil) {
|
2016-03-14 12:53:12 -03:00
|
|
|
/* Flush sys.stdout and sys.stderr */
|
|
|
|
flush_std_files();
|
|
|
|
}
|
2015-03-24 09:46:18 -03:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
#ifdef MS_WINDOWS
|
2017-10-04 13:50:12 -03:00
|
|
|
fatal_output_debug(msg);
|
2015-03-24 21:55:14 -03:00
|
|
|
#endif /* MS_WINDOWS */
|
|
|
|
|
|
|
|
exit:
|
2017-11-15 19:48:08 -04:00
|
|
|
if (status < 0) {
|
2015-03-24 21:55:14 -03:00
|
|
|
#if defined(MS_WINDOWS) && defined(_DEBUG)
|
2017-11-15 19:48:08 -04:00
|
|
|
DebugBreak();
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
2017-11-15 19:48:08 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:41:59 -04:00
|
|
|
void _Py_NO_RETURN
|
2017-11-15 19:48:08 -04:00
|
|
|
Py_FatalError(const char *msg)
|
|
|
|
{
|
|
|
|
fatal_error(NULL, msg, -1);
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:41:59 -04:00
|
|
|
void _Py_NO_RETURN
|
2019-05-27 11:39:22 -03:00
|
|
|
Py_ExitStatusException(PyStatus status)
|
2017-11-15 19:48:08 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_IS_EXIT(status)) {
|
|
|
|
exit(status.exitcode);
|
2019-03-01 07:14:41 -04:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
else if (_PyStatus_IS_ERROR(status)) {
|
|
|
|
fatal_error(status.func, status.err_msg, 1);
|
2019-03-01 07:14:41 -04:00
|
|
|
}
|
2019-05-16 11:39:26 -03:00
|
|
|
else {
|
2019-05-27 11:39:22 -03:00
|
|
|
Py_FatalError("Py_ExitStatusException() must not be called on success");
|
2019-05-16 11:39:26 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up and exit */
|
|
|
|
|
2016-06-17 07:29:00 -03:00
|
|
|
# include "pythread.h"
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* For the atexit module. */
|
2017-12-20 06:17:58 -04:00
|
|
|
void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2019-06-19 19:05:23 -03:00
|
|
|
PyInterpreterState *is = _PyInterpreterState_GET_UNSAFE();
|
2017-12-20 06:17:58 -04:00
|
|
|
|
2017-12-12 21:29:07 -04:00
|
|
|
/* Guard against API misuse (see bpo-17852) */
|
2017-12-20 06:17:58 -04:00
|
|
|
assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
|
|
|
|
|
|
|
|
is->pyexitfunc = func;
|
|
|
|
is->pyexitmodule = module;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-19 19:05:23 -03:00
|
|
|
call_py_exitfuncs(PyThreadState *tstate)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2019-06-19 19:05:23 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
if (interp->pyexitfunc == NULL)
|
2014-11-20 07:39:37 -04:00
|
|
|
return;
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
(*interp->pyexitfunc)(interp->pyexitmodule);
|
|
|
|
_PyErr_Clear(tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until threading._shutdown completes, provided
|
|
|
|
the threading module was imported in the first place.
|
|
|
|
The shutdown routine will wait until all non-daemon
|
|
|
|
"threading" threads have completed. */
|
|
|
|
static void
|
2019-06-19 19:05:23 -03:00
|
|
|
wait_for_thread_shutdown(PyThreadState *tstate)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(_shutdown);
|
|
|
|
PyObject *result;
|
2017-09-15 19:35:20 -03:00
|
|
|
PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (threading == NULL) {
|
2019-06-19 19:05:23 -03:00
|
|
|
if (_PyErr_Occurred(tstate)) {
|
2019-03-25 17:50:58 -03:00
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
/* else: threading not imported */
|
2014-11-20 07:39:37 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-07-08 05:19:25 -03:00
|
|
|
result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (result == NULL) {
|
|
|
|
PyErr_WriteUnraisable(threading);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_DECREF(result);
|
|
|
|
}
|
|
|
|
Py_DECREF(threading);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NEXITFUNCS 32
|
|
|
|
int Py_AtExit(void (*func)(void))
|
|
|
|
{
|
2017-09-08 02:51:28 -03:00
|
|
|
if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
|
2014-11-20 07:39:37 -04:00
|
|
|
return -1;
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
|
2014-11-20 07:39:37 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-04-24 12:24:01 -03:00
|
|
|
call_ll_exitfuncs(_PyRuntimeState *runtime)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2019-04-24 12:24:01 -03:00
|
|
|
while (runtime->nexitfuncs > 0) {
|
2019-04-26 00:49:26 -03:00
|
|
|
/* pop last function from the list */
|
|
|
|
runtime->nexitfuncs--;
|
|
|
|
void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
|
|
|
|
runtime->exitfuncs[runtime->nexitfuncs] = NULL;
|
|
|
|
|
|
|
|
exitfunc();
|
2019-04-24 12:24:01 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
2018-08-01 11:41:25 -03:00
|
|
|
void _Py_NO_RETURN
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_Exit(int sts)
|
|
|
|
{
|
2015-11-29 23:18:29 -04:00
|
|
|
if (Py_FinalizeEx() < 0) {
|
|
|
|
sts = 120;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
exit(sts);
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-06-19 19:05:23 -03:00
|
|
|
init_signals(PyThreadState *tstate)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
PyOS_setsig(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFZ
|
|
|
|
PyOS_setsig(SIGXFZ, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
PyOS_setsig(SIGXFSZ, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
PyOS_InitInterrupts(); /* May imply initsignal() */
|
2019-06-19 19:05:23 -03:00
|
|
|
if (_PyErr_Occurred(tstate)) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("can't import signal");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
|
|
|
|
*
|
|
|
|
* All of the code in this function must only use async-signal-safe functions,
|
|
|
|
* listed at `man 7 signal` or
|
|
|
|
* http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_Py_RestoreSignals(void)
|
|
|
|
{
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
PyOS_setsig(SIGPIPE, SIG_DFL);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFZ
|
|
|
|
PyOS_setsig(SIGXFZ, SIG_DFL);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
PyOS_setsig(SIGXFSZ, SIG_DFL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The file descriptor fd is considered ``interactive'' if either
|
|
|
|
* a) isatty(fd) is TRUE, or
|
|
|
|
* b) the -i flag was given, and the filename associated with
|
|
|
|
* the descriptor is NULL or "<stdin>" or "???".
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
Py_FdIsInteractive(FILE *fp, const char *filename)
|
|
|
|
{
|
|
|
|
if (isatty((int)fileno(fp)))
|
|
|
|
return 1;
|
|
|
|
if (!Py_InteractiveFlag)
|
|
|
|
return 0;
|
|
|
|
return (filename == NULL) ||
|
|
|
|
(strcmp(filename, "<stdin>") == 0) ||
|
|
|
|
(strcmp(filename, "???") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Wrappers around sigaction() or signal(). */
|
|
|
|
|
|
|
|
PyOS_sighandler_t
|
|
|
|
PyOS_getsig(int sig)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
struct sigaction context;
|
|
|
|
if (sigaction(sig, NULL, &context) == -1)
|
|
|
|
return SIG_ERR;
|
|
|
|
return context.sa_handler;
|
|
|
|
#else
|
|
|
|
PyOS_sighandler_t handler;
|
|
|
|
/* Special signal handling for the secure CRT in Visual Studio 2005 */
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
|
|
|
switch (sig) {
|
|
|
|
/* Only these signals are valid */
|
|
|
|
case SIGINT:
|
|
|
|
case SIGILL:
|
|
|
|
case SIGFPE:
|
|
|
|
case SIGSEGV:
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGBREAK:
|
|
|
|
case SIGABRT:
|
|
|
|
break;
|
|
|
|
/* Don't call signal() with other values or it will assert */
|
|
|
|
default:
|
|
|
|
return SIG_ERR;
|
|
|
|
}
|
|
|
|
#endif /* _MSC_VER && _MSC_VER >= 1400 */
|
|
|
|
handler = signal(sig, SIG_IGN);
|
|
|
|
if (handler != SIG_ERR)
|
|
|
|
signal(sig, handler);
|
|
|
|
return handler;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All of the code in this function must only use async-signal-safe functions,
|
|
|
|
* listed at `man 7 signal` or
|
|
|
|
* http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
|
|
|
|
*/
|
|
|
|
PyOS_sighandler_t
|
|
|
|
PyOS_setsig(int sig, PyOS_sighandler_t handler)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
/* Some code in Modules/signalmodule.c depends on sigaction() being
|
|
|
|
* used here if HAVE_SIGACTION is defined. Fix that if this code
|
|
|
|
* changes to invalidate that assumption.
|
|
|
|
*/
|
|
|
|
struct sigaction context, ocontext;
|
|
|
|
context.sa_handler = handler;
|
|
|
|
sigemptyset(&context.sa_mask);
|
|
|
|
context.sa_flags = 0;
|
|
|
|
if (sigaction(sig, &context, &ocontext) == -1)
|
|
|
|
return SIG_ERR;
|
|
|
|
return ocontext.sa_handler;
|
|
|
|
#else
|
|
|
|
PyOS_sighandler_t oldhandler;
|
|
|
|
oldhandler = signal(sig, handler);
|
|
|
|
#ifdef HAVE_SIGINTERRUPT
|
|
|
|
siginterrupt(sig, 1);
|
|
|
|
#endif
|
|
|
|
return oldhandler;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|