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-02-28 22:44:13 -04:00
|
|
|
#include "pycore_coreconfig.h"
|
2018-10-31 20:52:28 -03:00
|
|
|
#include "pycore_context.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"
|
2018-11-12 11:53:38 -04:00
|
|
|
#include "pycore_pylifecycle.h"
|
|
|
|
#include "pycore_pymem.h"
|
|
|
|
#include "pycore_pystate.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 */
|
|
|
|
|
|
|
|
/* Forward */
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError add_main_module(PyInterpreterState *interp);
|
|
|
|
static _PyInitError initfsencoding(PyInterpreterState *interp);
|
|
|
|
static _PyInitError initsite(void);
|
2017-12-13 07:29:09 -04:00
|
|
|
static _PyInitError init_sys_streams(PyInterpreterState *interp);
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError initsigs(void);
|
2017-12-20 06:17:58 -04:00
|
|
|
static void call_py_exitfuncs(PyInterpreterState *);
|
2014-11-20 07:39:37 -04:00
|
|
|
static void wait_for_thread_shutdown(void);
|
|
|
|
static void call_ll_exitfuncs(void);
|
|
|
|
|
2019-02-16 16:57:40 -04:00
|
|
|
int _Py_UnhandledKeyboardInterrupt = 0;
|
2017-11-15 19:48:08 -04:00
|
|
|
_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
|
2017-09-08 02:51:28 -03:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
_PyInitError
|
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. */
|
|
|
|
static int initialized = 0;
|
2017-11-15 19:48:08 -04:00
|
|
|
if (initialized) {
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
2017-09-08 02:51:28 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char*
|
|
|
|
get_codec_name(const char *encoding)
|
|
|
|
{
|
2016-11-20 04:16:47 -04:00
|
|
|
const char *name_utf8;
|
|
|
|
char *name_str;
|
2014-11-20 07:39:37 -04:00
|
|
|
PyObject *codec, *name = NULL;
|
|
|
|
|
|
|
|
codec = _PyCodec_Lookup(encoding);
|
|
|
|
if (!codec)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
name = _PyObject_GetAttrId(codec, &PyId_name);
|
|
|
|
Py_CLEAR(codec);
|
|
|
|
if (!name)
|
|
|
|
goto error;
|
|
|
|
|
2016-11-20 03:13:07 -04:00
|
|
|
name_utf8 = PyUnicode_AsUTF8(name);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (name_utf8 == NULL)
|
|
|
|
goto error;
|
|
|
|
name_str = _PyMem_RawStrdup(name_utf8);
|
|
|
|
Py_DECREF(name);
|
|
|
|
if (name_str == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return name_str;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(codec);
|
|
|
|
Py_XDECREF(name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError
|
2017-05-24 01:46:51 -03:00
|
|
|
initimport(PyInterpreterState *interp, PyObject *sysmod)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
PyObject *importlib;
|
|
|
|
PyObject *impmod;
|
|
|
|
PyObject *value;
|
|
|
|
|
|
|
|
/* Import _importlib through its frozen version, _frozen_importlib. */
|
|
|
|
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't import _frozen_importlib");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
else if (Py_VerboseFlag) {
|
|
|
|
PySys_FormatStderr("import _frozen_importlib # frozen\n");
|
|
|
|
}
|
|
|
|
importlib = PyImport_AddModule("_frozen_importlib");
|
|
|
|
if (importlib == NULL) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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)
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't import _imp");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
else if (Py_VerboseFlag) {
|
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) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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) {
|
|
|
|
PyErr_Print();
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("importlib install failed");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(impmod);
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError
|
2017-05-24 01:46:51 -03:00
|
|
|
initexternalimport(PyInterpreterState *interp)
|
|
|
|
{
|
|
|
|
PyObject *value;
|
|
|
|
value = PyObject_CallMethod(interp->importlib,
|
|
|
|
"_install_external_importers", "");
|
|
|
|
if (value == NULL) {
|
|
|
|
PyErr_Print();
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("external importer setup failed");
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
2017-06-08 08:13:20 -03:00
|
|
|
Py_DECREF(value);
|
2018-09-18 16:22:29 -03:00
|
|
|
return _PyImportZip_Init();
|
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
|
|
|
|
_Py_LegacyLocaleDetected(void)
|
|
|
|
{
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
/* 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
|
2017-12-15 23:54:22 -04:00
|
|
|
_emit_stderr_warning_for_legacy_locale(const _PyCoreConfig *core_config)
|
2017-06-17 23:29:42 -03:00
|
|
|
{
|
2019-03-05 07:32:09 -04:00
|
|
|
if (core_config->preconfig.coerce_c_locale_warn && _Py_LegacyLocaleDetected()) {
|
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";
|
|
|
|
|
|
|
|
static void
|
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");
|
|
|
|
return;
|
|
|
|
}
|
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);
|
2017-06-11 00:16:15 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2018-08-29 08:25:36 -03:00
|
|
|
_Py_CoerceLegacyLocale(int warn)
|
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) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-11-12 07:45:59 -04:00
|
|
|
#if !defined(__APPLE__) && !defined(__ANDROID__) && \
|
2017-12-15 23:54:22 -04:00
|
|
|
defined(HAVE_LANGINFO_H) && defined(CODESET)
|
|
|
|
/* 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 */
|
2018-08-29 08:25:36 -03:00
|
|
|
_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
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
Every call to _Py_InitializeCore, 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.)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
static _PyInitError
|
2019-01-22 16:18:05 -04:00
|
|
|
_Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p,
|
2018-07-24 21:49:17 -03:00
|
|
|
const _PyCoreConfig *core_config)
|
|
|
|
{
|
2019-01-22 16:18:05 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
if (!tstate) {
|
|
|
|
return _Py_INIT_ERR("failed to read thread state");
|
|
|
|
}
|
|
|
|
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
if (interp == NULL) {
|
|
|
|
return _Py_INIT_ERR("can't make main interpreter");
|
|
|
|
}
|
|
|
|
*interp_p = interp;
|
|
|
|
|
|
|
|
/* bpo-34008: For backward compatibility reasons, calling Py_Main() after
|
|
|
|
Py_Initialize() ignores the new configuration. */
|
2019-03-05 12:37:44 -04:00
|
|
|
if (core_config->preconfig.allocator != NULL) {
|
2018-07-24 21:49:17 -03:00
|
|
|
const char *allocator = _PyMem_GetAllocatorsName();
|
2019-03-05 12:37:44 -04:00
|
|
|
if (allocator == NULL || strcmp(core_config->preconfig.allocator, allocator) != 0) {
|
2018-07-24 21:49:17 -03:00
|
|
|
return _Py_INIT_USER_ERR("cannot modify memory allocator "
|
|
|
|
"after first Py_Initialize()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_PyCoreConfig_SetGlobalConfig(core_config);
|
|
|
|
|
|
|
|
if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) {
|
|
|
|
return _Py_INIT_ERR("failed to copy core config");
|
|
|
|
}
|
|
|
|
core_config = &interp->core_config;
|
|
|
|
|
|
|
|
if (core_config->_install_importlib) {
|
|
|
|
_PyInitError err = _PyCoreConfig_SetPathConfig(core_config);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _PyInitError
|
2019-01-22 16:18:05 -04:00
|
|
|
pycore_init_runtime(const _PyCoreConfig *core_config)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2018-07-24 21:49:17 -03:00
|
|
|
if (_PyRuntime.initialized) {
|
|
|
|
return _Py_INIT_ERR("main interpreter already initialized");
|
|
|
|
}
|
2017-12-14 20:46:02 -04:00
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
_PyCoreConfig_SetGlobalConfig(core_config);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
_PyInitError err = _PyRuntime_Initialize();
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-09-08 02:51:28 -03:00
|
|
|
|
2019-03-05 12:37:44 -04:00
|
|
|
if (core_config->preconfig.allocator != NULL) {
|
|
|
|
if (_PyMem_SetupAllocators(core_config->preconfig.allocator) < 0) {
|
2017-12-20 18:41:38 -04:00
|
|
|
return _Py_INIT_USER_ERR("Unknown PYTHONMALLOC allocator");
|
|
|
|
}
|
2017-11-15 19:48:08 -04: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 :(
|
|
|
|
*/
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime.finalizing = NULL;
|
|
|
|
|
2017-12-14 20:46:02 -04:00
|
|
|
err = _Py_HashRandomization_Init(core_config);
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
err = _PyInterpreterState_Enable(&_PyRuntime);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2019-01-22 16:18:05 -04:00
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
|
|
|
|
static _PyInitError
|
|
|
|
pycore_create_interpreter(const _PyCoreConfig *core_config,
|
|
|
|
PyInterpreterState **interp_p)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = PyInterpreterState_New();
|
2017-12-14 20:46:02 -04:00
|
|
|
if (interp == NULL) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't make main interpreter");
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
2018-07-24 21:49:17 -03:00
|
|
|
*interp_p = interp;
|
2017-12-14 20:46:02 -04:00
|
|
|
|
|
|
|
if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) {
|
|
|
|
return _Py_INIT_ERR("failed to copy core config");
|
|
|
|
}
|
2018-07-24 08:55:48 -03:00
|
|
|
core_config = &interp->core_config;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_New(interp);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (tstate == NULL)
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't make first thread");
|
2014-11-20 07:39:37 -04:00
|
|
|
(void) PyThreadState_Swap(tstate);
|
|
|
|
|
2015-11-29 23:18:29 -04:00
|
|
|
/* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
|
2014-11-20 07:39:37 -04:00
|
|
|
destroying the GIL might fail when it is being referenced from
|
|
|
|
another running thread (see issue #9901).
|
|
|
|
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. */
|
2014-11-20 07:39:37 -04:00
|
|
|
_PyEval_FiniThreads();
|
2018-01-29 06:57:45 -04:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Auto-thread-state API */
|
|
|
|
_PyGILState_Init(interp, tstate);
|
|
|
|
|
2018-01-29 06:57:45 -04:00
|
|
|
/* Create the GIL */
|
|
|
|
PyEval_InitThreads();
|
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _PyInitError
|
|
|
|
pycore_init_types(void)
|
|
|
|
{
|
2019-01-23 10:04:40 -04:00
|
|
|
_PyInitError err = _PyTypes_Init();
|
2019-01-22 16:18:05 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
err = _PyUnicode_Init();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyStructSequence_Init() < 0) {
|
|
|
|
return _Py_INIT_ERR("can't initialize structseq");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_PyLong_Init()) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't init longs");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
err = _PyExc_Init();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_PyFloat_Init()) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't init float");
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
if (!_PyContext_Init()) {
|
|
|
|
return _Py_INIT_ERR("can't init context");
|
|
|
|
}
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _PyInitError
|
|
|
|
pycore_init_builtins(PyInterpreterState *interp)
|
|
|
|
{
|
2018-07-24 08:55:48 -03:00
|
|
|
PyObject *bimod = _PyBuiltin_Init();
|
2019-01-22 16:18:05 -04:00
|
|
|
if (bimod == NULL) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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-01-22 16:18:05 -04:00
|
|
|
_PyInitError err = _PyBuiltins_AddExceptions(bimod);
|
2019-01-22 12:39:03 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2019-01-22 16:18:05 -04:00
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
static _PyInitError
|
|
|
|
pycore_init_import_warnings(PyInterpreterState *interp, PyObject *sysmod)
|
|
|
|
{
|
|
|
|
_PyInitError err = _PyImport_Init(interp);
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
err = _PyImportHooks_Init();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* Initialize _warnings. */
|
2017-12-19 06:35:58 -04:00
|
|
|
if (_PyWarnings_Init() == NULL) {
|
2017-11-23 05:43:14 -04:00
|
|
|
return _Py_INIT_ERR("can't initialize warnings");
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
if (interp->core_config._install_importlib) {
|
|
|
|
err = _PyCoreConfig_SetPathConfig(&interp->core_config);
|
2018-07-20 21:06:16 -03:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
/* This call sets up builtin and frozen import support */
|
2019-01-22 16:18:05 -04:00
|
|
|
if (interp->core_config._install_importlib) {
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initimport(interp, sysmod);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
2019-01-22 16:18:05 -04:00
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _PyInitError
|
|
|
|
_Py_InitializeCore_impl(PyInterpreterState **interp_p,
|
|
|
|
const _PyCoreConfig *core_config)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp;
|
|
|
|
|
|
|
|
_PyInitError err = pycore_init_runtime(core_config);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pycore_create_interpreter(core_config, &interp);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
core_config = &interp->core_config;
|
|
|
|
*interp_p = interp;
|
|
|
|
|
|
|
|
err = pycore_init_types();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *sysmod;
|
2019-01-23 10:04:40 -04:00
|
|
|
err = _PySys_Create(interp, &sysmod);
|
2019-01-22 16:18:05 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pycore_init_builtins(interp);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pycore_init_import_warnings(interp, sysmod);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-05-24 01:46:51 -03:00
|
|
|
|
|
|
|
/* Only when we get here is the runtime core fully initialized */
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime.core_initialized = 1;
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_OK();
|
2017-05-24 01:46:51 -03: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)
|
|
|
|
*/
|
2018-07-24 21:49:17 -03:00
|
|
|
_PyInitError
|
|
|
|
_Py_InitializeCore(PyInterpreterState **interp_p,
|
|
|
|
const _PyCoreConfig *src_config)
|
|
|
|
{
|
|
|
|
assert(src_config != NULL);
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyInitError err;
|
|
|
|
|
|
|
|
/* Copy the configuration, since _PyCoreConfig_Read() modifies it
|
|
|
|
(and the input configuration is read only). */
|
|
|
|
_PyCoreConfig config = _PyCoreConfig_INIT;
|
|
|
|
|
2018-08-29 06:25:15 -03:00
|
|
|
/* Set LC_CTYPE to the user preferred locale */
|
2018-08-28 19:16:53 -03:00
|
|
|
_Py_SetLocaleFromEnv(LC_CTYPE);
|
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
if (_PyCoreConfig_Copy(&config, src_config) >= 0) {
|
2019-03-04 21:44:12 -04:00
|
|
|
err = _PyCoreConfig_Read(&config, NULL);
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = _Py_INIT_ERR("failed to copy core config");
|
|
|
|
}
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
if (!_PyRuntime.core_initialized) {
|
|
|
|
err = _Py_InitializeCore_impl(interp_p, &config);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = _Py_Initialize_ReconfigureCore(interp_p, &config);
|
|
|
|
}
|
2018-07-24 21:49:17 -03:00
|
|
|
|
|
|
|
done:
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
_PyCoreConfig_Clear(&config);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
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(). */
|
|
|
|
static _PyInitError
|
|
|
|
_Py_ReconfigureMainInterpreter(PyInterpreterState *interp,
|
|
|
|
const _PyMainInterpreterConfig *config)
|
|
|
|
{
|
|
|
|
if (config->argv != NULL) {
|
|
|
|
int res = PyDict_SetItemString(interp->sysdict, "argv", config->argv);
|
|
|
|
if (res < 0) {
|
|
|
|
return _Py_INIT_ERR("fail to set sys.argv");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2017-11-15 19:48:08 -04:00
|
|
|
_PyInitError
|
2018-07-24 21:49:17 -03:00
|
|
|
_Py_InitializeMainInterpreter(PyInterpreterState *interp,
|
|
|
|
const _PyMainInterpreterConfig *config)
|
2017-05-24 01:46:51 -03:00
|
|
|
{
|
2017-09-08 02:51:28 -03:00
|
|
|
if (!_PyRuntime.core_initialized) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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 */
|
2017-12-14 20:46:02 -04:00
|
|
|
if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) {
|
|
|
|
return _Py_INIT_ERR("failed to copy main interpreter config");
|
|
|
|
}
|
2018-07-24 21:49:17 -03:00
|
|
|
config = &interp->config;
|
|
|
|
_PyCoreConfig *core_config = &interp->core_config;
|
2017-05-24 03:00:52 -03:00
|
|
|
|
2018-07-20 12:34:23 -03:00
|
|
|
if (_PyRuntime.initialized) {
|
|
|
|
return _Py_ReconfigureMainInterpreter(interp, config);
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
if (!core_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.
|
|
|
|
*/
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime.initialized = 1;
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't initialize time");
|
2017-12-05 10:12:41 -04:00
|
|
|
}
|
2015-04-03 08:10:54 -03:00
|
|
|
|
2019-01-23 10:04:40 -04:00
|
|
|
if (_PySys_InitMain(interp) < 0) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't finish initializing sys");
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
_PyInitError err = initexternalimport(interp);
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* initialize the faulthandler module */
|
2018-07-24 08:55:48 -03:00
|
|
|
err = _PyFaulthandler_Init(core_config->faulthandler);
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initfsencoding(interp);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-23 05:43:14 -04:00
|
|
|
if (interp->config.install_signal_handlers) {
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initsigs(); /* Signal handling stuff, including initintr() */
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
if (_PyTraceMalloc_Init(core_config->tracemalloc) < 0) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't initialize tracemalloc");
|
2018-07-24 08:55:48 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
err = add_main_module(interp);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2017-12-13 07:29:09 -04:00
|
|
|
err = init_sys_streams(interp);
|
2017-11-15 22:11:45 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
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");
|
|
|
|
PyErr_Print();
|
|
|
|
}
|
|
|
|
Py_XDECREF(warnings_module);
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime.initialized = 1;
|
2017-05-24 01:46:51 -03:00
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
if (core_config->site_import) {
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initsite(); /* Module site */
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2018-08-29 17:56:06 -03:00
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
_emit_stderr_warning_for_legacy_locale(core_config);
|
|
|
|
#endif
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2017-05-24 03:00:52 -03:00
|
|
|
#undef _INIT_DEBUG_PRINT
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
_PyInitError
|
2018-07-24 21:49:17 -03:00
|
|
|
_Py_InitializeFromConfig(const _PyCoreConfig *config)
|
2017-05-24 01:46:51 -03:00
|
|
|
{
|
2018-09-11 19:11:06 -03:00
|
|
|
PyInterpreterState *interp = NULL;
|
2017-11-15 19:48:08 -04:00
|
|
|
_PyInitError err;
|
2018-07-24 21:49:17 -03:00
|
|
|
err = _Py_InitializeCore(&interp, config);
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2018-07-24 21:49:17 -03:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2018-07-24 21:49:17 -03:00
|
|
|
config = &interp->core_config;
|
2017-11-15 19:48:08 -04:00
|
|
|
|
2017-12-20 14:36:46 -04:00
|
|
|
_PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
|
2018-07-24 21:49:17 -03:00
|
|
|
err = _PyMainInterpreterConfig_Read(&main_config, config);
|
2017-12-20 14:36:46 -04:00
|
|
|
if (!_Py_INIT_FAILED(err)) {
|
2018-07-24 21:49:17 -03:00
|
|
|
err = _Py_InitializeMainInterpreter(interp, &main_config);
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2017-12-20 14:36:46 -04:00
|
|
|
_PyMainInterpreterConfig_Clear(&main_config);
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2018-07-24 21:49:17 -03:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2018-07-24 21:49:17 -03:00
|
|
|
return _Py_INIT_OK();
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
void
|
|
|
|
Py_InitializeEx(int install_sigs)
|
|
|
|
{
|
2018-07-24 21:49:17 -03:00
|
|
|
if (_PyRuntime.initialized) {
|
|
|
|
/* bpo-33932: Calling Py_Initialize() twice does nothing. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_PyInitError err;
|
|
|
|
_PyCoreConfig config = _PyCoreConfig_INIT;
|
|
|
|
config.install_signal_handlers = install_sigs;
|
|
|
|
|
|
|
|
err = _Py_InitializeFromConfig(&config);
|
|
|
|
_PyCoreConfig_Clear(&config);
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2019-03-01 07:14:41 -04:00
|
|
|
_Py_ExitInitError(err);
|
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)) {
|
2016-09-05 22:16:01 -03:00
|
|
|
tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
|
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)) {
|
2016-09-05 22:16:01 -03:00
|
|
|
tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
|
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
|
|
|
{
|
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyThreadState *tstate;
|
2015-11-29 23:18:29 -04:00
|
|
|
int status = 0;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-09-08 02:51:28 -03:00
|
|
|
if (!_PyRuntime.initialized)
|
2015-11-29 23:18:29 -04:00
|
|
|
return status;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
wait_for_thread_shutdown();
|
|
|
|
|
2017-12-20 06:17:58 -04:00
|
|
|
/* Get current thread state and interpreter pointer */
|
2018-10-31 21:51:40 -03:00
|
|
|
tstate = _PyThreadState_GET();
|
2017-12-20 06:17:58 -04:00
|
|
|
interp = tstate->interp;
|
|
|
|
|
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
|
|
|
|
* interpreter is uninitialized until after the exit funcs have run.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 06:17:58 -04:00
|
|
|
call_py_exitfuncs(interp);
|
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
|
2017-12-14 20:46:02 -04:00
|
|
|
int show_ref_count = interp->core_config.show_ref_count;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
|
|
|
#ifdef Py_TRACE_REFS
|
2017-12-14 20:46:02 -04:00
|
|
|
int dump_refs = interp->core_config.dump_refs;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_PYMALLOC
|
2017-12-14 20:46:02 -04:00
|
|
|
int malloc_stats = interp->core_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()). */
|
2017-09-08 02:51:28 -03:00
|
|
|
_PyRuntime.finalizing = tstate;
|
|
|
|
_PyRuntime.initialized = 0;
|
|
|
|
_PyRuntime.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
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Destroy all modules */
|
|
|
|
PyImport_Cleanup();
|
|
|
|
|
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 */
|
|
|
|
PyMethod_Fini();
|
|
|
|
PyFrame_Fini();
|
|
|
|
PyCFunction_Fini();
|
|
|
|
PyTuple_Fini();
|
|
|
|
PyList_Fini();
|
|
|
|
PySet_Fini();
|
|
|
|
PyBytes_Fini();
|
|
|
|
PyLong_Fini();
|
|
|
|
PyFloat_Fini();
|
|
|
|
PyDict_Fini();
|
|
|
|
PySlice_Fini();
|
|
|
|
_PyGC_Fini();
|
2017-05-23 01:36:03 -03:00
|
|
|
_Py_HashRandomization_Fini();
|
2016-08-14 04:52:18 -03:00
|
|
|
_PyArg_Fini();
|
2016-09-09 02:01:51 -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 */
|
|
|
|
_PyGILState_Fini();
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
|
call_ll_exitfuncs();
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
static _PyInitError
|
|
|
|
new_interpreter(PyThreadState **tstate_p)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyThreadState *tstate, *save_tstate;
|
|
|
|
PyObject *bimod, *sysmod;
|
2017-11-24 22:17:57 -04:00
|
|
|
_PyInitError err;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
if (!_PyRuntime.initialized) {
|
|
|
|
return _Py_INIT_ERR("Py_Initialize must be called first");
|
|
|
|
}
|
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;
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
interp = PyInterpreterState_New();
|
2017-11-15 22:11:45 -04:00
|
|
|
if (interp == NULL) {
|
|
|
|
*tstate_p = NULL;
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
tstate = PyThreadState_New(interp);
|
|
|
|
if (tstate == NULL) {
|
|
|
|
PyInterpreterState_Delete(interp);
|
2017-11-15 22:11:45 -04:00
|
|
|
*tstate_p = NULL;
|
|
|
|
return _Py_INIT_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
save_tstate = PyThreadState_Swap(tstate);
|
|
|
|
|
2017-05-24 01:46:51 -03:00
|
|
|
/* Copy the current interpreter config into the new interpreter */
|
2017-12-14 20:46:02 -04:00
|
|
|
_PyCoreConfig *core_config;
|
|
|
|
_PyMainInterpreterConfig *config;
|
2017-05-24 01:46:51 -03:00
|
|
|
if (save_tstate != NULL) {
|
2017-12-14 20:46:02 -04:00
|
|
|
core_config = &save_tstate->interp->core_config;
|
|
|
|
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();
|
2017-12-14 20:46:02 -04:00
|
|
|
core_config = &main_interp->core_config;
|
|
|
|
config = &main_interp->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) {
|
|
|
|
return _Py_INIT_ERR("failed to copy core config");
|
|
|
|
}
|
2018-07-24 08:55:48 -03:00
|
|
|
core_config = &interp->core_config;
|
2017-12-14 20:46:02 -04:00
|
|
|
if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) {
|
|
|
|
return _Py_INIT_ERR("failed to copy main interpreter config");
|
2017-05-24 01:46:51 -03:00
|
|
|
}
|
|
|
|
|
2019-01-22 16:18:05 -04:00
|
|
|
err = _PyExc_Init();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return _Py_INIT_ERR("can't make modules dictionary");
|
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
interp->modules = modules;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-09-14 15:18:12 -03:00
|
|
|
sysmod = _PyImport_FindBuiltin("sys", modules);
|
|
|
|
if (sysmod != NULL) {
|
|
|
|
interp->sysdict = PyModule_GetDict(sysmod);
|
|
|
|
if (interp->sysdict == NULL)
|
|
|
|
goto handle_error;
|
|
|
|
Py_INCREF(interp->sysdict);
|
|
|
|
PyDict_SetItemString(interp->sysdict, "modules", modules);
|
2019-01-23 10:04:40 -04:00
|
|
|
if (_PySys_InitMain(interp) < 0) {
|
|
|
|
return _Py_INIT_ERR("can't finish initializing sys");
|
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
}
|
2018-12-11 02:38:03 -04:00
|
|
|
else if (PyErr_Occurred()) {
|
|
|
|
goto handle_error;
|
|
|
|
}
|
2017-09-14 15:18:12 -03:00
|
|
|
|
|
|
|
bimod = _PyImport_FindBuiltin("builtins", modules);
|
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);
|
|
|
|
}
|
2018-12-11 02:38:03 -04:00
|
|
|
else if (PyErr_Occurred()) {
|
|
|
|
goto handle_error;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
if (bimod != NULL && sysmod != NULL) {
|
2019-01-22 16:18:05 -04:00
|
|
|
err = _PyBuiltins_AddExceptions(bimod);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-01-23 10:04:40 -04:00
|
|
|
err = _PySys_SetPreliminaryStderr(interp->sysdict);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
err = _PyImportHooks_Init();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2017-11-15 22:11:45 -04:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initimport(interp, sysmod);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2017-11-15 22:11:45 -04:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initexternalimport(interp);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2017-11-15 22:11:45 -04:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initfsencoding(interp);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2017-11-15 22:11:45 -04:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2017-12-13 07:29:09 -04:00
|
|
|
err = init_sys_streams(interp);
|
2017-11-15 22:11:45 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = add_main_module(interp);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2017-11-15 22:11:45 -04:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
if (core_config->site_import) {
|
2017-11-15 19:48:08 -04:00
|
|
|
err = initsite();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2017-11-15 22:11:45 -04:00
|
|
|
return err;
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
goto handle_error;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2017-11-15 22:11:45 -04:00
|
|
|
*tstate_p = tstate;
|
|
|
|
return _Py_INIT_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;
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadState *
|
|
|
|
Py_NewInterpreter(void)
|
|
|
|
{
|
|
|
|
PyThreadState *tstate;
|
|
|
|
_PyInitError err = new_interpreter(&tstate);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2019-03-01 07:14:41 -04:00
|
|
|
_Py_ExitInitError(err);
|
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;
|
|
|
|
|
2018-10-31 21:51:40 -03:00
|
|
|
if (tstate != _PyThreadState_GET())
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_FatalError("Py_EndInterpreter: thread is not current");
|
|
|
|
if (tstate->frame != NULL)
|
|
|
|
Py_FatalError("Py_EndInterpreter: thread still has a frame");
|
|
|
|
|
|
|
|
wait_for_thread_shutdown();
|
|
|
|
|
2017-12-20 06:17:58 -04:00
|
|
|
call_py_exitfuncs(interp);
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
if (tstate != interp->tstate_head || tstate->next != NULL)
|
|
|
|
Py_FatalError("Py_EndInterpreter: not the last thread");
|
|
|
|
|
|
|
|
PyImport_Cleanup();
|
|
|
|
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
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError
|
|
|
|
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)
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't create __main__ module");
|
|
|
|
|
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)) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("Failed to retrieve builtins module");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("Failed to retrieve BuiltinImporter");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("Failed to initialize __main__.__loader__");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(loader);
|
|
|
|
}
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError
|
2014-11-20 07:39:37 -04:00
|
|
|
initfsencoding(PyInterpreterState *interp)
|
|
|
|
{
|
2018-08-29 08:25:36 -03:00
|
|
|
_PyCoreConfig *config = &interp->core_config;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2018-08-29 08:25:36 -03:00
|
|
|
char *encoding = get_codec_name(config->filesystem_encoding);
|
|
|
|
if (encoding == NULL) {
|
|
|
|
/* Such error can only occurs in critical situations: no more
|
|
|
|
memory, import a module of the standard library failed, etc. */
|
|
|
|
return _Py_INIT_ERR("failed to get the Python codec "
|
|
|
|
"of the filesystem encoding");
|
2016-09-08 14:35:16 -03:00
|
|
|
}
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2018-08-29 08:25:36 -03:00
|
|
|
/* Update the filesystem encoding to the normalized Python codec name.
|
|
|
|
For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii"
|
|
|
|
(Python codec name). */
|
|
|
|
PyMem_RawFree(config->filesystem_encoding);
|
|
|
|
config->filesystem_encoding = encoding;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2018-08-29 08:25:36 -03:00
|
|
|
/* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
|
|
|
|
global configuration variables. */
|
|
|
|
if (_Py_SetFileSystemEncoding(config->filesystem_encoding,
|
|
|
|
config->filesystem_errors) < 0) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2018-08-29 08:25:36 -03:00
|
|
|
/* PyUnicode can now use the Python codec rather than C implementation
|
|
|
|
for the filesystem encoding */
|
2014-11-20 07:39:37 -04:00
|
|
|
interp->fscodec_initialized = 1;
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_OK();
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Import the site module (not into __main__ though) */
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError
|
2014-11-20 07:39:37 -04:00
|
|
|
initsite(void)
|
|
|
|
{
|
|
|
|
PyObject *m;
|
|
|
|
m = PyImport_ImportModule("site");
|
|
|
|
if (m == NULL) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_USER_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);
|
|
|
|
return _Py_INIT_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)
|
|
|
|
{
|
2017-05-03 19:45:56 -03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
/* 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. Prefer fstat() over dup() to detect
|
|
|
|
such error. */
|
|
|
|
struct stat st;
|
|
|
|
return (fstat(fd, &st) == 0);
|
|
|
|
#else
|
2015-09-04 12:29:57 -03:00
|
|
|
int fd2;
|
2016-09-08 15:21:54 -03:00
|
|
|
if (fd < 0)
|
2015-09-04 12:29:57 -03:00
|
|
|
return 0;
|
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2015-09-29 08:59:50 -03:00
|
|
|
/* Prefer dup() over fstat(). fstat() can require input/output whereas
|
|
|
|
dup() doesn't, there is a low risk of EMFILE/ENFILE at Python
|
|
|
|
startup. */
|
2015-09-04 12:29:57 -03:00
|
|
|
fd2 = dup(fd);
|
|
|
|
if (fd2 >= 0)
|
|
|
|
close(fd2);
|
|
|
|
_Py_END_SUPPRESS_IPH
|
|
|
|
return fd2 >= 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*
|
2018-08-29 19:50:45 -03:00
|
|
|
create_stdio(const _PyCoreConfig *config, PyObject* io,
|
2015-12-25 14:01:53 -04:00
|
|
|
int fd, int write_mode, const char* name,
|
|
|
|
const char* encoding, const char* 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";
|
|
|
|
buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
|
|
|
|
fd, mode, buffering,
|
2015-11-29 23:18:29 -04:00
|
|
|
Py_None, Py_None, /* encoding, errors */
|
|
|
|
Py_None, 0); /* 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))
|
|
|
|
encoding = "utf-8";
|
|
|
|
#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;
|
2016-09-05 22:16:01 -03:00
|
|
|
res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
|
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
|
|
|
|
|
2017-10-04 14:25:40 -03:00
|
|
|
stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssOO",
|
2014-11-20 07:39:37 -04:00
|
|
|
buf, encoding, errors,
|
2017-10-04 14:25:40 -03:00
|
|
|
newline, line_buffering, write_through);
|
2014-11-20 07:39:37 -04:00
|
|
|
Py_CLEAR(buf);
|
|
|
|
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 */
|
2017-11-15 22:11:45 -04:00
|
|
|
static _PyInitError
|
2017-12-13 07:29:09 -04:00
|
|
|
init_sys_streams(PyInterpreterState *interp)
|
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;
|
2017-11-15 22:11:45 -04:00
|
|
|
_PyInitError res = _Py_INIT_OK();
|
2018-08-29 06:47:29 -03:00
|
|
|
_PyCoreConfig *config = &interp->core_config;
|
|
|
|
|
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)) {
|
|
|
|
return _Py_INIT_USER_ERR("<stdin> is a directory, "
|
|
|
|
"cannot continue");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-29 06:47:29 -03:00
|
|
|
char *codec_name = get_codec_name(config->stdio_encoding);
|
|
|
|
if (codec_name == NULL) {
|
|
|
|
return _Py_INIT_ERR("failed to get the Python codec name "
|
|
|
|
"of the stdio encoding");
|
|
|
|
}
|
|
|
|
PyMem_RawFree(config->stdio_encoding);
|
|
|
|
config->stdio_encoding = codec_name;
|
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,
|
|
|
|
"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);
|
|
|
|
}
|
|
|
|
PyErr_Clear(); /* Not a fatal error if codec isn't available */
|
|
|
|
|
|
|
|
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:
|
|
|
|
res = _Py_INIT_ERR("can't initialize sys standard streams");
|
|
|
|
|
|
|
|
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
|
|
|
|
_Py_FatalError_DumpTracebacks(int fd)
|
|
|
|
{
|
|
|
|
fputc('\n', stderr);
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
/* display the current Python stack */
|
2016-03-16 18:45:24 -03:00
|
|
|
_Py_DumpTracebackThreads(fd, NULL, NULL);
|
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
|
|
|
{
|
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
|
|
|
|
2015-03-24 08:01:30 -03:00
|
|
|
PyErr_Fetch(&exception, &v, &tb);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:30 -03:00
|
|
|
PyErr_NormalizeException(&exception, &v, &tb);
|
|
|
|
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() */
|
2016-09-05 22:16:01 -03:00
|
|
|
res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
|
2015-03-24 09:46:18 -03:00
|
|
|
if (res == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
Py_DECREF(res);
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
{
|
|
|
|
const int fd = fileno(stderr);
|
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
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
fprintf(stderr, "Fatal Python error: ");
|
|
|
|
if (prefix) {
|
|
|
|
fputs(prefix, stderr);
|
|
|
|
fputs(": ", stderr);
|
|
|
|
}
|
|
|
|
if (msg) {
|
|
|
|
fputs(msg, stderr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "<message not set>");
|
|
|
|
}
|
|
|
|
fputs("\n", stderr);
|
2014-11-20 07:39:37 -04:00
|
|
|
fflush(stderr); /* it helps in Windows debug build */
|
2015-03-24 08:01:30 -03:00
|
|
|
|
2018-10-31 20:26:41 -03:00
|
|
|
/* Check if the current thread has a Python thread state
|
|
|
|
and holds the GIL */
|
|
|
|
PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
|
|
|
|
if (tss_tstate != NULL) {
|
2018-10-31 21:51:40 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2018-10-31 20:26:41 -03:00
|
|
|
if (tss_tstate != tstate) {
|
|
|
|
/* The Python thread does not hold the GIL */
|
|
|
|
tss_tstate = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Py_FatalError() has been called from a C thread
|
|
|
|
which has no Python thread state. */
|
|
|
|
}
|
|
|
|
int has_tstate_and_gil = (tss_tstate != NULL);
|
|
|
|
|
|
|
|
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 */
|
|
|
|
_Py_FatalError_DumpTracebacks(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-03-14 12:53:12 -03:00
|
|
|
_Py_FatalError_DumpTracebacks(fd);
|
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-03-01 07:14:41 -04:00
|
|
|
_Py_ExitInitError(_PyInitError err)
|
2017-11-15 19:48:08 -04:00
|
|
|
{
|
2019-03-01 07:14:41 -04:00
|
|
|
if (err.exitcode >= 0) {
|
|
|
|
exit(err.exitcode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* On "user" error: exit with status 1.
|
|
|
|
For all other errors, call abort(). */
|
|
|
|
int status = err.user_err ? 1 : -1;
|
|
|
|
fatal_error(err.prefix, err.msg, status);
|
|
|
|
}
|
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
|
|
|
{
|
2018-08-03 10:33:52 -03:00
|
|
|
PyInterpreterState *is = _PyInterpreterState_Get();
|
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
|
2017-12-20 06:17:58 -04:00
|
|
|
call_py_exitfuncs(PyInterpreterState *istate)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2017-12-20 06:17:58 -04:00
|
|
|
if (istate->pyexitfunc == NULL)
|
2014-11-20 07:39:37 -04:00
|
|
|
return;
|
|
|
|
|
2017-12-20 06:17:58 -04:00
|
|
|
(*istate->pyexitfunc)(istate->pyexitmodule);
|
2014-11-20 07:39:37 -04:00
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
wait_for_thread_shutdown(void)
|
|
|
|
{
|
|
|
|
_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) {
|
|
|
|
/* threading not imported */
|
|
|
|
PyErr_Clear();
|
|
|
|
return;
|
|
|
|
}
|
2016-09-05 22:16:01 -03:00
|
|
|
result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
|
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
|
|
|
|
call_ll_exitfuncs(void)
|
|
|
|
{
|
2017-09-08 02:51:28 -03:00
|
|
|
while (_PyRuntime.nexitfuncs > 0)
|
|
|
|
(*_PyRuntime.exitfuncs[--_PyRuntime.nexitfuncs])();
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:48:08 -04:00
|
|
|
static _PyInitError
|
2014-11-20 07:39:37 -04:00
|
|
|
initsigs(void)
|
|
|
|
{
|
|
|
|
#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() */
|
|
|
|
if (PyErr_Occurred()) {
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_ERR("can't import signal");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2017-11-15 19:48:08 -04:00
|
|
|
return _Py_INIT_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
|