2014-11-20 07:39:37 -04:00
|
|
|
/* Python interpreter top-level routines, including init/exit */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
#include "pycore_bytesobject.h" // _PyBytes_InitTypes()
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_ceval.h" // _PyEval_FiniGIL()
|
|
|
|
#include "pycore_context.h" // _PyContext_Init()
|
2021-12-09 15:59:26 -04:00
|
|
|
#include "pycore_exceptions.h" // _PyExc_InitTypes()
|
|
|
|
#include "pycore_dict.h" // _PyDict_Fini()
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_fileutils.h" // _Py_ResetForceASCII()
|
2021-12-09 15:59:26 -04:00
|
|
|
#include "pycore_floatobject.h" // _PyFloat_InitTypes()
|
|
|
|
#include "pycore_genobject.h" // _PyAsyncGen_Fini()
|
2020-11-18 18:18:29 -04:00
|
|
|
#include "pycore_import.h" // _PyImport_BootstrapImp()
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_initconfig.h" // _PyStatus_OK()
|
2021-12-09 15:59:26 -04:00
|
|
|
#include "pycore_list.h" // _PyList_Fini()
|
|
|
|
#include "pycore_long.h" // _PyLong_InitTypes()
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
|
|
|
|
#include "pycore_pathconfig.h" // _PyConfig_WritePathConfig()
|
|
|
|
#include "pycore_pyerrors.h" // _PyErr_Occurred()
|
|
|
|
#include "pycore_pylifecycle.h" // _PyErr_Print()
|
2022-01-27 16:23:22 -04:00
|
|
|
#include "pycore_pymem.h" // _PyObject_DebugMallocStats()
|
2020-04-14 12:52:15 -03:00
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
2022-02-08 16:39:07 -04:00
|
|
|
#include "pycore_runtime.h" // _Py_ID()
|
2022-01-13 18:54:36 -04:00
|
|
|
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
|
2021-12-09 15:59:26 -04:00
|
|
|
#include "pycore_sliceobject.h" // _PySlice_Fini()
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
|
|
|
|
#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
|
2021-12-09 15:59:26 -04:00
|
|
|
#include "pycore_tuple.h" // _PyTuple_InitTypes()
|
|
|
|
#include "pycore_typeobject.h" // _PyTypes_InitTypes()
|
|
|
|
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2022-01-22 18:22:20 -04:00
|
|
|
extern void _PyIO_Fini(void);
|
|
|
|
|
2020-04-14 23:01:58 -03:00
|
|
|
#include <locale.h> // setlocale()
|
2021-10-13 14:25:53 -03:00
|
|
|
#include <stdlib.h> // getenv()
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2021-06-03 20:37:29 -03:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <mach-o/loader.h>
|
|
|
|
#endif
|
|
|
|
|
2020-04-14 23:01:58 -03:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
# include <signal.h> // SIG_IGN
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_LANGINFO_H
|
2020-04-14 23:01:58 -03:00
|
|
|
# include <langinfo.h> // nl_langinfo(CODESET)
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
|
2021-11-28 14:40:27 -04:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h> // F_GETFD
|
|
|
|
#endif
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
#ifdef MS_WINDOWS
|
2020-04-14 23:01:58 -03:00
|
|
|
# undef BYTE
|
|
|
|
# include "windows.h"
|
2016-08-31 01:22:36 -03:00
|
|
|
|
2020-04-14 23:01:58 -03:00
|
|
|
extern PyTypeObject PyWindowsConsoleIO_Type;
|
|
|
|
# define PyWindowsConsoleIO_Check(op) \
|
|
|
|
(PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif
|
|
|
|
|
2021-01-18 13:34:56 -04:00
|
|
|
#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
|
|
|
|
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
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);
|
2020-03-12 04:49:05 -03:00
|
|
|
static PyStatus init_set_builtins_open(void);
|
2019-06-19 19:05:23 -03:00
|
|
|
static PyStatus init_sys_streams(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;
|
2021-06-03 20:37:29 -03:00
|
|
|
|
|
|
|
/* The following places the `_PyRuntime` structure in a location that can be
|
|
|
|
* found without any external information. This is meant to ease access to the
|
|
|
|
* interpreter state for various runtime debugging tools, but is *not* an
|
|
|
|
* officially supported feature */
|
|
|
|
|
|
|
|
#if defined(MS_WINDOWS)
|
|
|
|
|
|
|
|
#pragma section("PyRuntime", read, write)
|
|
|
|
__declspec(allocate("PyRuntime"))
|
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
|
|
|
|
__attribute__((
|
|
|
|
section(SEG_DATA ",PyRuntime")
|
|
|
|
))
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-03-30 03:35:15 -03:00
|
|
|
/* Suppress deprecation warning for PyBytesObject.ob_shash */
|
|
|
|
_Py_COMP_DIAG_PUSH
|
|
|
|
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
2021-06-03 20:37:29 -03:00
|
|
|
_PyRuntimeState _PyRuntime
|
|
|
|
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
|
|
|
|
__attribute__ ((section (".PyRuntime")))
|
|
|
|
#endif
|
|
|
|
= _PyRuntimeState_INIT;
|
2022-03-30 03:35:15 -03:00
|
|
|
_Py_COMP_DIAG_POP
|
|
|
|
|
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)
|
|
|
|
{
|
2020-03-06 19:24:23 -04:00
|
|
|
return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
|
2017-09-08 02:51:28 -03:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
|
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.)
|
|
|
|
|
|
|
|
*/
|
2020-11-12 10:14:13 -04:00
|
|
|
static int
|
2019-06-19 19:05:23 -03:00
|
|
|
init_importlib(PyThreadState *tstate, PyObject *sysmod)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2020-11-12 10:14:13 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
|
|
|
|
2019-06-19 19:05:23 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
2020-04-12 22:04:28 -03:00
|
|
|
int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2020-11-12 10:14:13 -04:00
|
|
|
// Import _importlib through its frozen version, _frozen_importlib.
|
|
|
|
if (verbose) {
|
2014-11-20 07:39:37 -04:00
|
|
|
PySys_FormatStderr("import _frozen_importlib # frozen\n");
|
|
|
|
}
|
2020-11-12 10:14:13 -04:00
|
|
|
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed
|
2014-11-20 07:39:37 -04:00
|
|
|
if (importlib == NULL) {
|
2020-11-12 10:14:13 -04:00
|
|
|
return -1;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2020-11-12 10:14:13 -04:00
|
|
|
interp->importlib = Py_NewRef(importlib);
|
2016-08-02 16:51:21 -03:00
|
|
|
|
2020-11-12 10:14:13 -04:00
|
|
|
// Import the _imp module
|
|
|
|
if (verbose) {
|
2015-09-18 04:11:57 -03:00
|
|
|
PySys_FormatStderr("import _imp # builtin\n");
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
2020-11-18 18:18:29 -04:00
|
|
|
PyObject *imp_mod = _PyImport_BootstrapImp(tstate);
|
2020-11-12 10:14:13 -04:00
|
|
|
if (imp_mod == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (_PyImport_SetModuleString("_imp", imp_mod) < 0) {
|
|
|
|
Py_DECREF(imp_mod);
|
|
|
|
return -1;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2020-11-12 10:14:13 -04:00
|
|
|
// Install importlib as the implementation of import
|
|
|
|
PyObject *value = PyObject_CallMethod(importlib, "_install",
|
|
|
|
"OO", sysmod, imp_mod);
|
|
|
|
Py_DECREF(imp_mod);
|
2014-11-20 07:39:37 -04:00
|
|
|
if (value == NULL) {
|
2020-11-12 10:14:13 -04:00
|
|
|
return -1;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(value);
|
|
|
|
|
2020-11-12 10:14:13 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
|
|
|
return 0;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2020-11-12 10:14:13 -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
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
#ifndef MS_WINDOWS
|
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
|
|
|
}
|
|
|
|
}
|
2019-11-22 12:52:42 -04:00
|
|
|
#endif /* !defined(MS_WINDOWS) */
|
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
|
|
|
|
2020-11-04 19:45:56 -04:00
|
|
|
static int
|
2020-11-10 08:21:52 -04:00
|
|
|
interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
|
2020-11-04 19:45:56 -04:00
|
|
|
{
|
2020-11-10 08:21:52 -04:00
|
|
|
const PyConfig *config = &tstate->interp->config;
|
2020-11-04 19:45:56 -04:00
|
|
|
|
2020-11-10 08:21:52 -04:00
|
|
|
if (!only_update_path_config) {
|
|
|
|
PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
_PyErr_SetFromPyStatus(status);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-11-04 19:45:56 -04:00
|
|
|
}
|
|
|
|
|
2021-02-19 08:33:31 -04:00
|
|
|
if (_Py_IsMainInterpreter(tstate->interp)) {
|
2021-12-02 20:08:42 -04:00
|
|
|
PyStatus status = _PyPathConfig_UpdateGlobal(config);
|
2020-11-04 19:45:56 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
_PyErr_SetFromPyStatus(status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the sys module for the new configuration
|
|
|
|
if (_PySys_UpdateConfig(tstate) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyInterpreterState_SetConfig(const PyConfig *src_config)
|
|
|
|
{
|
2021-10-13 09:09:13 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2020-11-04 19:45:56 -04:00
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
PyConfig config;
|
|
|
|
PyConfig_InitPythonConfig(&config);
|
|
|
|
PyStatus status = _PyConfig_Copy(&config, src_config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
_PyErr_SetFromPyStatus(status);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2021-12-02 20:08:42 -04:00
|
|
|
status = _PyConfig_Read(&config, 1);
|
2020-11-04 19:45:56 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
_PyErr_SetFromPyStatus(status);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:21:52 -04:00
|
|
|
status = _PyConfig_Copy(&tstate->interp->config, &config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
_PyErr_SetFromPyStatus(status);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = interpreter_update_config(tstate, 0);
|
2020-11-04 19:45:56 -04:00
|
|
|
|
|
|
|
done:
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-08 13:12:59 -03:00
|
|
|
status = _PyConfig_Write(config, runtime);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
2018-07-24 21:49:17 -03:00
|
|
|
|
2020-11-04 19:45:56 -04:00
|
|
|
status = _PyConfig_Copy(&interp->config, config);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-07-24 21:49:17 -03:00
|
|
|
}
|
2020-04-12 22:04:28 -03:00
|
|
|
config = _PyInterpreterState_GetConfig(interp);
|
2018-07-24 21:49:17 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config->_install_importlib) {
|
2021-12-02 20:08:42 -04:00
|
|
|
status = _PyPathConfig_UpdateGlobal(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
|
|
|
|
2020-06-08 13:12:59 -03:00
|
|
|
PyStatus status = _PyConfig_Write(config, runtime);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
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 :(
|
|
|
|
*/
|
2020-03-06 19:24:23 -04:00
|
|
|
_PyRuntimeState_SetFinalizing(runtime, NULL);
|
2017-09-08 02:51:28 -03:00
|
|
|
|
2020-06-08 13:12:59 -03:00
|
|
|
status = _Py_HashRandomization_Init(config);
|
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 = _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
|
|
|
|
2020-04-08 12:54:59 -03:00
|
|
|
static PyStatus
|
|
|
|
init_interp_create_gil(PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
PyStatus status;
|
|
|
|
|
|
|
|
/* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
|
|
|
|
only called here. */
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyEval_FiniGIL(tstate->interp);
|
2020-04-08 12:54:59 -03:00
|
|
|
|
|
|
|
/* Auto-thread-state API */
|
2021-03-10 15:00:46 -04:00
|
|
|
status = _PyGILState_SetTstate(tstate);
|
2020-04-08 12:54:59 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the GIL and take it */
|
|
|
|
status = _PyEval_InitGIL(tstate);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-03-10 15:00:46 -04:00
|
|
|
/* Auto-thread-state API */
|
|
|
|
PyStatus status = _PyGILState_Init(runtime);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-03-10 15:00:46 -04:00
|
|
|
assert(_Py_IsMainInterpreter(interp));
|
2017-12-14 20:46:02 -04:00
|
|
|
|
2021-03-10 15:00:46 -04:00
|
|
|
status = _PyConfig_Copy(&interp->config, config);
|
2019-05-27 11:39:22 -03:00
|
|
|
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);
|
|
|
|
|
2020-04-08 12:54:59 -03:00
|
|
|
status = init_interp_create_gil(tstate);
|
2020-03-09 17:24:14 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
2018-01-29 06:57:45 -04:00
|
|
|
|
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
|
2021-12-09 15:59:26 -04:00
|
|
|
pycore_init_global_objects(PyInterpreterState *interp)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
2019-11-19 20:18:11 -04:00
|
|
|
PyStatus status;
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
_PyFloat_InitState(interp);
|
2021-04-02 10:28:13 -03:00
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyUnicode_InitGlobalObjects(interp);
|
2019-11-19 20:18:11 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
_PyUnicode_InitState(interp);
|
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyStatus
|
|
|
|
pycore_init_types(PyInterpreterState *interp)
|
|
|
|
{
|
|
|
|
PyStatus status;
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyTypes_InitState(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyTypes_InitTypes(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-11-19 19:38:03 -04:00
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyBytes_InitTypes(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2020-06-25 09:07:40 -03:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyLong_InitTypes(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyUnicode_InitTypes(interp);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyFloat_InitTypes(interp);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-22 06:28:22 -03:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:59:26 -04:00
|
|
|
status = _PyTuple_InitTypes(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:48:56 -04:00
|
|
|
if (_PyExc_InitTypes(interp) < 0) {
|
|
|
|
return _PyStatus_ERR("failed to initialize an exception type");
|
2021-12-09 15:59:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
status = _PyExc_InitGlobalObjects(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = _PyExc_InitState(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = _PyErr_InitTypes(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2022-01-22 13:55:48 -04:00
|
|
|
status = _PyContext_Init(interp);
|
2021-12-09 15:59:26 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-11-19 19:38:03 -04: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
|
2021-04-02 10:28:13 -03:00
|
|
|
pycore_init_builtins(PyThreadState *tstate)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
2021-04-02 10:28:13 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
PyObject *bimod = _PyBuiltin_Init(interp);
|
2019-01-22 16:18:05 -04:00
|
|
|
if (bimod == NULL) {
|
2019-11-22 14:24:49 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
|
|
|
|
goto error;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-22 14:24:49 -04:00
|
|
|
PyObject *builtins_dict = PyModule_GetDict(bimod);
|
|
|
|
if (builtins_dict == NULL) {
|
|
|
|
goto error;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
2019-11-22 14:24:49 -04:00
|
|
|
Py_INCREF(builtins_dict);
|
|
|
|
interp->builtins = builtins_dict;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2022-03-07 15:45:00 -04:00
|
|
|
PyObject *isinstance = PyDict_GetItem(builtins_dict, &_Py_ID(isinstance));
|
|
|
|
assert(isinstance);
|
|
|
|
interp->callable_cache.isinstance = isinstance;
|
|
|
|
PyObject *len = PyDict_GetItem(builtins_dict, &_Py_ID(len));
|
|
|
|
assert(len);
|
|
|
|
interp->callable_cache.len = len;
|
|
|
|
PyObject *list_append = _PyType_Lookup(&PyList_Type, &_Py_ID(append));
|
|
|
|
assert(list_append);
|
|
|
|
interp->callable_cache.list_append = list_append;
|
|
|
|
|
2022-01-22 16:48:56 -04:00
|
|
|
if (_PyBuiltins_AddExceptions(bimod) < 0) {
|
|
|
|
return _PyStatus_ERR("failed to add exceptions to builtins");
|
2019-01-22 12:39:03 -04:00
|
|
|
}
|
2019-11-22 14:24:49 -04:00
|
|
|
|
|
|
|
interp->builtins_copy = PyDict_Copy(interp->builtins);
|
|
|
|
if (interp->builtins_copy == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
2019-12-04 07:19:59 -04:00
|
|
|
Py_DECREF(bimod);
|
2019-12-05 21:43:30 -04:00
|
|
|
|
2020-11-18 18:18:29 -04:00
|
|
|
// Get the __import__ function
|
|
|
|
PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins,
|
|
|
|
"__import__");
|
|
|
|
if (import_func == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
interp->import_func = Py_NewRef(import_func);
|
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-11-22 14:24:49 -04:00
|
|
|
|
|
|
|
error:
|
2019-12-04 07:19:59 -04:00
|
|
|
Py_XDECREF(bimod);
|
2019-11-22 14:24:49 -04:00
|
|
|
return _PyStatus_ERR("can't initialize builtins module");
|
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-12-05 22:37:07 -04:00
|
|
|
pycore_interp_init(PyThreadState *tstate)
|
2019-01-22 16:18:05 -04:00
|
|
|
{
|
2021-04-02 10:28:13 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
2019-12-05 22:37:07 -04:00
|
|
|
PyStatus status;
|
2019-12-08 16:55:58 -04:00
|
|
|
PyObject *sysmod = NULL;
|
2019-03-25 22:31:11 -03:00
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
// Create singletons before the first PyType_Ready() call, since
|
|
|
|
// PyType_Ready() uses singletons like the Unicode empty string (tp_doc)
|
|
|
|
// and the empty tuple singletons (tp_bases).
|
2021-12-09 15:59:26 -04:00
|
|
|
status = pycore_init_global_objects(interp);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2021-04-02 10:28:13 -03:00
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
// The GC must be initialized before the first GC collection.
|
|
|
|
status = _PyGC_Init(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
2022-02-24 12:54:06 -04:00
|
|
|
// Intern strings in deep-frozen modules first so that others
|
|
|
|
// can use it instead of creating a heap allocated string.
|
2022-02-26 12:35:03 -04:00
|
|
|
if (_Py_Deepfreeze_Init() < 0) {
|
|
|
|
return _PyStatus_ERR("failed to initialize deep-frozen modules");
|
|
|
|
}
|
2022-02-25 11:19:30 -04:00
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
status = pycore_init_types(interp);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-12-08 16:55:58 -04:00
|
|
|
goto done;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
if (_PyWarnings_InitState(interp) < 0) {
|
|
|
|
return _PyStatus_ERR("can't initialize warnings");
|
|
|
|
}
|
2021-02-19 10:10:45 -04:00
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
status = _PyAtExit_Init(interp);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = _PySys_Create(tstate, &sysmod);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-12-08 16:55:58 -04:00
|
|
|
goto done;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
status = pycore_init_builtins(tstate);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2021-02-19 10:10:45 -04:00
|
|
|
|
2021-04-02 10:28:13 -03:00
|
|
|
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
|
2020-11-12 10:14:13 -04:00
|
|
|
if (config->_install_importlib) {
|
|
|
|
/* This call sets up builtin and frozen import support */
|
|
|
|
if (init_importlib(tstate, sysmod) < 0) {
|
|
|
|
return _PyStatus_ERR("failed to initialize importlib");
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 16:55:58 -04:00
|
|
|
|
|
|
|
done:
|
|
|
|
/* sys.modules['sys'] contains a strong reference to the module */
|
|
|
|
Py_XDECREF(sysmod);
|
|
|
|
return status;
|
2019-12-05 22:37:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyStatus
|
|
|
|
pyinit_config(_PyRuntimeState *runtime,
|
|
|
|
PyThreadState **tstate_p,
|
|
|
|
const PyConfig *config)
|
|
|
|
{
|
|
|
|
PyStatus status = pycore_init_runtime(runtime, config);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-01-22 16:18:05 -04:00
|
|
|
}
|
|
|
|
|
2019-12-05 22:37:07 -04: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-12-05 22:37:07 -04:00
|
|
|
*tstate_p = tstate;
|
2019-01-22 16:18:05 -04:00
|
|
|
|
2019-12-05 22:37:07 -04:00
|
|
|
status = pycore_interp_init(tstate);
|
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;
|
2020-11-04 19:45:56 -04:00
|
|
|
PyConfig_InitPythonConfig(&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;
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:21:52 -04:00
|
|
|
// Read the configuration, but don't compute the path configuration
|
|
|
|
// (it is computed in the main init).
|
|
|
|
status = _PyConfig_Read(&config, 0);
|
2019-05-27 11:39:22 -03:00
|
|
|
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
|
2020-11-04 12:34:34 -04:00
|
|
|
pyinit_main_reconfigure(PyThreadState *tstate)
|
2018-07-20 12:34:23 -03:00
|
|
|
{
|
2020-11-10 08:21:52 -04:00
|
|
|
if (interpreter_update_config(tstate, 0) < 0) {
|
|
|
|
return _PyStatus_ERR("fail to reconfigure Python");
|
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
|
|
|
}
|
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-11-22 12:52:42 -04:00
|
|
|
init_interp_main(PyThreadState *tstate)
|
2017-05-24 01:46:51 -03:00
|
|
|
{
|
2019-12-05 21:43:30 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
PyStatus status;
|
2021-02-19 08:33:31 -04:00
|
|
|
int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
|
2019-06-19 19:05:23 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
2020-04-12 22:04:28 -03:00
|
|
|
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
|
2017-05-24 03:00:52 -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-11-22 12:52:42 -04:00
|
|
|
if (is_main_interp) {
|
|
|
|
interp->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
|
|
|
|
2021-09-14 20:31:45 -03:00
|
|
|
// Initialize the import-related configuration.
|
|
|
|
status = _PyConfig_InitImportConfig(&interp->config);
|
2020-11-10 08:21:52 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interpreter_update_config(tstate, 1) < 0) {
|
|
|
|
return _PyStatus_ERR("failed to update the Python config");
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2019-11-22 12:52:42 -04: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-11-22 12:52:42 -04:00
|
|
|
if (is_main_interp) {
|
|
|
|
/* initialize the faulthandler module */
|
|
|
|
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-11-22 12:52:42 -04:00
|
|
|
if (is_main_interp) {
|
2020-11-17 11:22:23 -04:00
|
|
|
if (_PySignal_Init(config->install_signal_handlers) < 0) {
|
|
|
|
return _PyStatus_ERR("can't initialize signals");
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-11-22 12:52:42 -04: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-11-22 12:52:42 -04: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
|
|
|
}
|
2017-11-15 22:11:45 -04:00
|
|
|
|
2020-03-12 04:49:05 -03:00
|
|
|
status = init_set_builtins_open();
|
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-11-22 12:52:42 -04:00
|
|
|
status = add_main_module(interp);
|
2019-11-22 11:19:14 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
if (is_main_interp) {
|
|
|
|
/* Initialize warnings. */
|
|
|
|
PyObject *warnoptions = PySys_GetObject("warnoptions");
|
|
|
|
if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
|
|
|
|
{
|
|
|
|
PyObject *warnings_module = PyImport_ImportModule("warnings");
|
|
|
|
if (warnings_module == NULL) {
|
|
|
|
fprintf(stderr, "'import warnings' failed; traceback:\n");
|
|
|
|
_PyErr_Print(tstate);
|
|
|
|
}
|
|
|
|
Py_XDECREF(warnings_module);
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
interp->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
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
if (is_main_interp) {
|
2018-08-29 17:56:06 -03:00
|
|
|
#ifndef MS_WINDOWS
|
2019-11-22 12:52:42 -04:00
|
|
|
emit_stderr_warning_for_legacy_locale(interp->runtime);
|
2018-08-29 17:56:06 -03:00
|
|
|
#endif
|
2019-11-22 12:52:42 -04:00
|
|
|
}
|
2018-08-29 17:56:06 -03:00
|
|
|
|
2019-12-05 21:43:30 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
|
|
|
|
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-11-22 12:52:42 -04: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.
|
|
|
|
*/
|
|
|
|
static PyStatus
|
|
|
|
pyinit_main(PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
if (!interp->runtime->core_initialized) {
|
|
|
|
return _PyStatus_ERR("runtime core not initialized");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interp->runtime->initialized) {
|
2020-11-04 12:34:34 -04:00
|
|
|
return pyinit_main_reconfigure(tstate);
|
2019-11-22 12:52:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyStatus status = init_interp_main(tstate);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-04-12 22:04:28 -03:00
|
|
|
config = _PyInterpreterState_GetConfig(tstate->interp);
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-04 12:34:34 -04:00
|
|
|
|
|
|
|
PyStatus
|
|
|
|
_Py_InitializeMain(void)
|
|
|
|
{
|
|
|
|
PyStatus status = _PyRuntime_Initialize();
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
|
|
|
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
|
|
|
return pyinit_main(tstate);
|
|
|
|
}
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2020-10-30 14:03:28 -03:00
|
|
|
static void
|
|
|
|
finalize_modules_delete_special(PyThreadState *tstate, int verbose)
|
|
|
|
{
|
|
|
|
// List of names to clear in sys
|
|
|
|
static const char * const sys_deletes[] = {
|
|
|
|
"path", "argv", "ps1", "ps2",
|
|
|
|
"last_type", "last_value", "last_traceback",
|
|
|
|
"path_hooks", "path_importer_cache", "meta_path",
|
|
|
|
"__interactivehook__",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const sys_files[] = {
|
|
|
|
"stdin", "__stdin__",
|
|
|
|
"stdout", "__stdout__",
|
|
|
|
"stderr", "__stderr__",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
if (verbose) {
|
|
|
|
PySys_WriteStderr("# clear builtins._\n");
|
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const *p;
|
|
|
|
for (p = sys_deletes; *p != NULL; p++) {
|
|
|
|
if (verbose) {
|
|
|
|
PySys_WriteStderr("# clear sys.%s\n", *p);
|
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (p = sys_files; *p != NULL; p+=2) {
|
|
|
|
const char *name = p[0];
|
|
|
|
const char *orig_name = p[1];
|
|
|
|
if (verbose) {
|
|
|
|
PySys_WriteStderr("# restore sys.%s\n", name);
|
|
|
|
}
|
|
|
|
PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict,
|
|
|
|
orig_name);
|
|
|
|
if (value == NULL) {
|
|
|
|
if (_PyErr_Occurred(tstate)) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
value = Py_None;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
finalize_remove_modules(PyObject *modules, int verbose)
|
|
|
|
{
|
|
|
|
PyObject *weaklist = PyList_New(0);
|
|
|
|
if (weaklist == NULL) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STORE_MODULE_WEAKREF(name, mod) \
|
|
|
|
if (weaklist != NULL) { \
|
|
|
|
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
|
|
|
|
if (wr) { \
|
|
|
|
PyObject *tup = PyTuple_Pack(2, name, wr); \
|
|
|
|
if (!tup || PyList_Append(weaklist, tup) < 0) { \
|
|
|
|
PyErr_WriteUnraisable(NULL); \
|
|
|
|
} \
|
|
|
|
Py_XDECREF(tup); \
|
|
|
|
Py_DECREF(wr); \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
PyErr_WriteUnraisable(NULL); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CLEAR_MODULE(name, mod) \
|
|
|
|
if (PyModule_Check(mod)) { \
|
|
|
|
if (verbose && PyUnicode_Check(name)) { \
|
|
|
|
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
|
|
|
|
} \
|
|
|
|
STORE_MODULE_WEAKREF(name, mod); \
|
|
|
|
if (PyObject_SetItem(modules, name, Py_None) < 0) { \
|
|
|
|
PyErr_WriteUnraisable(NULL); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyDict_CheckExact(modules)) {
|
|
|
|
Py_ssize_t pos = 0;
|
|
|
|
PyObject *key, *value;
|
|
|
|
while (PyDict_Next(modules, &pos, &key, &value)) {
|
|
|
|
CLEAR_MODULE(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyObject *iterator = PyObject_GetIter(modules);
|
|
|
|
if (iterator == NULL) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyObject *key;
|
|
|
|
while ((key = PyIter_Next(iterator))) {
|
|
|
|
PyObject *value = PyObject_GetItem(modules, key);
|
|
|
|
if (value == NULL) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CLEAR_MODULE(key, value);
|
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(key);
|
|
|
|
}
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
Py_DECREF(iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef CLEAR_MODULE
|
|
|
|
#undef STORE_MODULE_WEAKREF
|
|
|
|
|
|
|
|
return weaklist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
finalize_clear_modules_dict(PyObject *modules)
|
|
|
|
{
|
|
|
|
if (PyDict_CheckExact(modules)) {
|
|
|
|
PyDict_Clear(modules);
|
|
|
|
}
|
|
|
|
else {
|
2022-02-08 16:39:07 -04:00
|
|
|
if (PyObject_CallMethodNoArgs(modules, &_Py_ID(clear)) == NULL) {
|
2020-10-30 14:03:28 -03:00
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
finalize_restore_builtins(PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
PyObject *dict = PyDict_Copy(interp->builtins);
|
|
|
|
if (dict == NULL) {
|
|
|
|
PyErr_WriteUnraisable(NULL);
|
|
|
|
}
|
|
|
|
PyDict_Clear(interp->builtins);
|
|
|
|
if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
|
|
|
|
_PyErr_Clear(tstate);
|
|
|
|
}
|
|
|
|
Py_XDECREF(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
finalize_modules_clear_weaklist(PyInterpreterState *interp,
|
|
|
|
PyObject *weaklist, int verbose)
|
|
|
|
{
|
|
|
|
// First clear modules imported later
|
|
|
|
for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
|
|
|
|
PyObject *tup = PyList_GET_ITEM(weaklist, i);
|
|
|
|
PyObject *name = PyTuple_GET_ITEM(tup, 0);
|
|
|
|
PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
|
|
|
|
if (mod == Py_None) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert(PyModule_Check(mod));
|
|
|
|
PyObject *dict = PyModule_GetDict(mod);
|
|
|
|
if (dict == interp->builtins || dict == interp->sysdict) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Py_INCREF(mod);
|
|
|
|
if (verbose && PyUnicode_Check(name)) {
|
|
|
|
PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
|
|
|
|
}
|
|
|
|
_PyModule_Clear(mod);
|
|
|
|
Py_DECREF(mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose)
|
|
|
|
{
|
|
|
|
// Clear sys dict
|
|
|
|
if (verbose) {
|
|
|
|
PySys_FormatStderr("# cleanup[3] wiping sys\n");
|
|
|
|
}
|
|
|
|
_PyModule_ClearDict(interp->sysdict);
|
|
|
|
|
|
|
|
// Clear builtins dict
|
|
|
|
if (verbose) {
|
|
|
|
PySys_FormatStderr("# cleanup[3] wiping builtins\n");
|
|
|
|
}
|
|
|
|
_PyModule_ClearDict(interp->builtins);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear modules, as good as we can */
|
|
|
|
static void
|
|
|
|
finalize_modules(PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
PyObject *modules = interp->modules;
|
|
|
|
if (modules == NULL) {
|
|
|
|
// Already done
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
|
|
|
|
|
|
|
|
// Delete some special builtins._ and sys attributes first. These are
|
|
|
|
// common places where user values hide and people complain when their
|
|
|
|
// destructors fail. Since the modules containing them are
|
|
|
|
// deleted *last* of all, they would come too late in the normal
|
|
|
|
// destruction order. Sigh.
|
|
|
|
//
|
|
|
|
// XXX Perhaps these precautions are obsolete. Who knows?
|
|
|
|
finalize_modules_delete_special(tstate, verbose);
|
|
|
|
|
|
|
|
// Remove all modules from sys.modules, hoping that garbage collection
|
|
|
|
// can reclaim most of them: set all sys.modules values to None.
|
|
|
|
//
|
|
|
|
// We prepare a list which will receive (name, weakref) tuples of
|
|
|
|
// modules when they are removed from sys.modules. The name is used
|
|
|
|
// for diagnosis messages (in verbose mode), while the weakref helps
|
|
|
|
// detect those modules which have been held alive.
|
|
|
|
PyObject *weaklist = finalize_remove_modules(modules, verbose);
|
|
|
|
|
|
|
|
// Clear the modules dict
|
|
|
|
finalize_clear_modules_dict(modules);
|
|
|
|
|
|
|
|
// Restore the original builtins dict, to ensure that any
|
|
|
|
// user data gets cleared.
|
|
|
|
finalize_restore_builtins(tstate);
|
|
|
|
|
|
|
|
// Collect garbage
|
|
|
|
_PyGC_CollectNoFail(tstate);
|
|
|
|
|
|
|
|
// Dump GC stats before it's too late, since it uses the warnings
|
|
|
|
// machinery.
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyGC_DumpShutdownStats(interp);
|
2020-10-30 14:03:28 -03:00
|
|
|
|
|
|
|
if (weaklist != NULL) {
|
|
|
|
// Now, if there are any modules left alive, clear their globals to
|
|
|
|
// minimize potential leaks. All C extension modules actually end
|
|
|
|
// up here, since they are kept alive in the interpreter state.
|
|
|
|
//
|
|
|
|
// The special treatment of "builtins" here is because even
|
|
|
|
// when it's not referenced as a module, its dictionary is
|
|
|
|
// referenced by almost every module's __builtins__. Since
|
|
|
|
// deleting a module clears its dictionary (even if there are
|
|
|
|
// references left to it), we need to delete the "builtins"
|
|
|
|
// module last. Likewise, we don't delete sys until the very
|
|
|
|
// end because it is implicitly referenced (e.g. by print).
|
|
|
|
//
|
|
|
|
// Since dict is ordered in CPython 3.6+, modules are saved in
|
|
|
|
// importing order. First clear modules imported later.
|
|
|
|
finalize_modules_clear_weaklist(interp, weaklist, verbose);
|
|
|
|
Py_DECREF(weaklist);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear sys and builtins modules dict
|
|
|
|
finalize_clear_sys_builtins_dict(interp, verbose);
|
|
|
|
|
|
|
|
// Clear module dict copies stored in the interpreter state:
|
|
|
|
// clear PyInterpreterState.modules_by_index and
|
|
|
|
// clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase
|
|
|
|
// initialization API)
|
|
|
|
_PyInterpreterState_ClearModules(interp);
|
|
|
|
|
|
|
|
// Clear and delete the modules directory. Actual modules will
|
|
|
|
// still be there only if imported during the execution of some
|
|
|
|
// destructor.
|
|
|
|
Py_SETREF(interp->modules, NULL);
|
|
|
|
|
|
|
|
// Collect garbage once more
|
|
|
|
_PyGC_CollectNoFail(tstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2020-10-30 14:03:28 -03:00
|
|
|
|
2015-11-29 23:18:29 -04:00
|
|
|
static int
|
2014-11-20 07:39:37 -04:00
|
|
|
flush_std_files(void)
|
|
|
|
{
|
2022-02-08 16:39:07 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
PyObject *fout = _PySys_GetAttr(tstate, &_Py_ID(stdout));
|
|
|
|
PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
2014-11-20 07:39:37 -04:00
|
|
|
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)) {
|
2022-02-08 16:39:07 -04:00
|
|
|
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(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)) {
|
2022-02-08 16:39:07 -04:00
|
|
|
tmp = PyObject_CallMethodNoArgs(ferr, &_Py_ID(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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-11-20 05:38:34 -04:00
|
|
|
|
|
|
|
static void
|
2021-02-19 10:10:45 -04:00
|
|
|
finalize_interp_types(PyInterpreterState *interp)
|
2019-11-20 05:38:34 -04:00
|
|
|
{
|
2022-01-22 17:55:39 -04:00
|
|
|
_PyUnicode_FiniTypes(interp);
|
2022-01-20 20:42:25 -04:00
|
|
|
_PySys_Fini(interp);
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyExc_Fini(interp);
|
|
|
|
_PyAsyncGen_Fini(interp);
|
|
|
|
_PyContext_Fini(interp);
|
2022-01-20 20:42:25 -04:00
|
|
|
_PyFloat_FiniType(interp);
|
|
|
|
_PyLong_FiniTypes(interp);
|
|
|
|
_PyThread_FiniType(interp);
|
|
|
|
_PyErr_FiniTypes(interp);
|
2021-12-09 15:59:26 -04:00
|
|
|
_PyTypes_Fini(interp);
|
2022-01-21 08:06:34 -04:00
|
|
|
_PyTypes_FiniTypes(interp);
|
2022-01-20 20:42:25 -04:00
|
|
|
|
2020-12-25 21:58:33 -04:00
|
|
|
// Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
|
|
|
|
// a dict internally.
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyUnicode_ClearInterned(interp);
|
2020-06-07 20:22:36 -03:00
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyDict_Fini(interp);
|
|
|
|
_PyList_Fini(interp);
|
|
|
|
_PyTuple_Fini(interp);
|
2019-11-20 05:38:34 -04:00
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
_PySlice_Fini(interp);
|
2019-11-20 05:38:34 -04:00
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyUnicode_Fini(interp);
|
|
|
|
_PyFloat_Fini(interp);
|
2019-11-20 05:38:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2019-11-20 13:39:12 -04:00
|
|
|
finalize_interp_clear(PyThreadState *tstate)
|
2019-11-20 05:38:34 -04:00
|
|
|
{
|
2021-02-19 08:33:31 -04:00
|
|
|
int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
|
2019-11-20 13:39:12 -04:00
|
|
|
|
2021-10-22 20:13:46 -03:00
|
|
|
_PyExc_ClearExceptionGroupType(tstate->interp);
|
|
|
|
|
2019-11-20 05:38:34 -04:00
|
|
|
/* Clear interpreter state and all thread states */
|
2020-10-30 18:51:02 -03:00
|
|
|
_PyInterpreterState_Clear(tstate);
|
2019-12-04 07:51:03 -04:00
|
|
|
|
2022-01-22 18:22:20 -04:00
|
|
|
if (is_main_interp) {
|
|
|
|
_PyIO_Fini();
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:06:46 -03:00
|
|
|
/* Clear all loghooks */
|
|
|
|
/* Both _PySys_Audit function and users still need PyObject, such as tuple.
|
|
|
|
Call _PySys_ClearAuditHooks when PyObject available. */
|
|
|
|
if (is_main_interp) {
|
|
|
|
_PySys_ClearAuditHooks(tstate);
|
|
|
|
}
|
|
|
|
|
2019-11-20 05:38:34 -04:00
|
|
|
if (is_main_interp) {
|
2020-06-07 20:22:36 -03:00
|
|
|
_Py_HashRandomization_Fini();
|
|
|
|
_PyArg_Fini();
|
|
|
|
_Py_ClearFileSystemEncoding();
|
2022-01-27 09:03:47 -04:00
|
|
|
_Py_Deepfreeze_Fini();
|
2020-06-07 20:22:36 -03:00
|
|
|
}
|
2019-11-20 05:38:34 -04:00
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
finalize_interp_types(tstate->interp);
|
2019-11-20 05:38:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2021-02-19 10:10:45 -04:00
|
|
|
finalize_interp_delete(PyInterpreterState *interp)
|
2019-11-20 05:38:34 -04:00
|
|
|
{
|
2021-02-19 10:10:45 -04:00
|
|
|
if (_Py_IsMainInterpreter(interp)) {
|
2019-11-20 05:38:34 -04:00
|
|
|
/* Cleanup auto-thread-state */
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyGILState_Fini(interp);
|
2019-11-20 05:38:34 -04:00
|
|
|
}
|
|
|
|
|
2020-04-08 12:54:59 -03:00
|
|
|
/* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
|
|
|
|
fail when it is being awaited by another running daemon thread (see
|
|
|
|
bpo-9901). Instead pycore_create_interpreter() destroys the previously
|
|
|
|
created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
|
|
|
|
called multiple times. */
|
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
PyInterpreterState_Delete(interp);
|
2019-11-20 05:38:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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.
|
2020-01-13 13:46:59 -04:00
|
|
|
_Py_FinishPendingCalls(tstate);
|
2019-06-03 13:14:24 -03:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyAtExit_Call(tstate->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
|
2020-11-18 11:38:53 -04:00
|
|
|
int show_ref_count = tstate->interp->config.show_ref_count;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
|
|
|
#ifdef Py_TRACE_REFS
|
2020-11-18 11:38:53 -04:00
|
|
|
int dump_refs = tstate->interp->config.dump_refs;
|
2021-08-17 12:52:50 -03:00
|
|
|
wchar_t *dump_refs_file = tstate->interp->config.dump_refs_file;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_PYMALLOC
|
2020-11-18 11:38:53 -04:00
|
|
|
int malloc_stats = tstate->interp->config.malloc_stats;
|
2017-12-19 06:35:58 -04:00
|
|
|
#endif
|
2017-12-06 12:26:10 -04:00
|
|
|
|
2020-03-08 07:57:45 -03:00
|
|
|
/* Remaining daemon threads will automatically exit
|
|
|
|
when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
|
2020-03-06 19:24:23 -04:00
|
|
|
_PyRuntimeState_SetFinalizing(runtime, tstate);
|
2019-04-24 12:24:01 -03:00
|
|
|
runtime->initialized = 0;
|
|
|
|
runtime->core_initialized = 0;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2020-03-09 19:37:49 -03:00
|
|
|
/* Destroy the state of all threads of the interpreter, except of the
|
|
|
|
current thread. In practice, only daemon threads should still be alive,
|
|
|
|
except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
|
|
|
|
Clear frames of other threads to call objects destructors. Destructors
|
|
|
|
will be called in the current Python thread. Since
|
|
|
|
_PyRuntimeState_SetFinalizing() has been called, no other Python thread
|
|
|
|
can take the GIL at this point: if they try, they will exit
|
|
|
|
immediately. */
|
|
|
|
_PyThreadState_DeleteExcept(runtime, tstate);
|
|
|
|
|
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 */
|
2020-11-17 11:22:23 -04:00
|
|
|
_PySignal_Fini();
|
2014-11-20 07:39:37 -04:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2020-10-30 13:00:00 -03:00
|
|
|
PyGC_Collect();
|
2017-09-14 04:35:58 -03:00
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* Destroy all modules */
|
2020-10-30 14:03:28 -03:00
|
|
|
finalize_modules(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();
|
|
|
|
|
|
|
|
/* unload faulthandler module */
|
|
|
|
_PyFaulthandler_Fini();
|
|
|
|
|
|
|
|
/* dump hash stats */
|
|
|
|
_PyHash_Fini();
|
|
|
|
|
|
|
|
#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.
|
|
|
|
*/
|
2021-08-17 12:52:50 -03:00
|
|
|
|
|
|
|
FILE *dump_refs_fp = NULL;
|
|
|
|
if (dump_refs_file != NULL) {
|
|
|
|
dump_refs_fp = _Py_wfopen(dump_refs_file, L"w");
|
|
|
|
if (dump_refs_fp == NULL) {
|
|
|
|
fprintf(stderr, "PYTHONDUMPREFSFILE: cannot create file: %ls\n", dump_refs_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-08-17 12:52:50 -03:00
|
|
|
|
|
|
|
if (dump_refs_fp != NULL) {
|
|
|
|
_Py_PrintReferences(dump_refs_fp);
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
#endif /* Py_TRACE_REFS */
|
|
|
|
|
2019-11-20 13:39:12 -04:00
|
|
|
finalize_interp_clear(tstate);
|
2021-02-19 10:10:45 -04:00
|
|
|
finalize_interp_delete(tstate->interp);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2022-01-21 08:05:26 -04:00
|
|
|
#ifdef Py_REF_DEBUG
|
|
|
|
if (show_ref_count) {
|
|
|
|
_PyDebug_PrintTotalRefs();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
#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.
|
|
|
|
*/
|
2021-08-17 12:52:50 -03:00
|
|
|
|
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
|
|
|
}
|
2021-08-17 12:52:50 -03:00
|
|
|
|
|
|
|
if (dump_refs_fp != NULL) {
|
|
|
|
_Py_PrintReferenceAddresses(dump_refs_fp);
|
|
|
|
fclose(dump_refs_fp);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
|
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
|
2020-05-01 06:33:44 -03:00
|
|
|
new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
|
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(). */
|
2020-04-13 06:45:21 -03:00
|
|
|
runtime->gilstate.check_enabled = 0;
|
2016-03-14 18:07:55 -03:00
|
|
|
|
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 */
|
2020-04-12 22:04:28 -03:00
|
|
|
const PyConfig *config;
|
2017-05-24 01:46:51 -03:00
|
|
|
if (save_tstate != NULL) {
|
2020-04-12 22:04:28 -03:00
|
|
|
config = _PyInterpreterState_GetConfig(save_tstate->interp);
|
2020-05-05 15:27:47 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-24 01:46:51 -03:00
|
|
|
/* No current thread state, copy from the main interpreter */
|
2021-12-07 21:56:06 -04:00
|
|
|
PyInterpreterState *main_interp = _PyInterpreterState_Main();
|
2020-04-12 22:04:28 -03:00
|
|
|
config = _PyInterpreterState_GetConfig(main_interp);
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
|
|
|
|
2020-11-04 19:45:56 -04:00
|
|
|
|
|
|
|
status = _PyConfig_Copy(&interp->config, config);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-12-05 21:43:30 -04:00
|
|
|
goto error;
|
2017-12-14 20:46:02 -04:00
|
|
|
}
|
2020-05-01 06:33:44 -03:00
|
|
|
interp->config._isolated_interpreter = isolated_subinterpreter;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2020-05-05 15:16:37 -03:00
|
|
|
status = init_interp_create_gil(tstate);
|
2019-12-05 21:43:30 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto error;
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 15:16:37 -03:00
|
|
|
status = pycore_interp_init(tstate);
|
2019-12-05 21:43:30 -04:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto error;
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2020-05-05 15:16:37 -03:00
|
|
|
status = init_interp_main(tstate);
|
2020-03-18 22:41:21 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2020-05-05 15:16:37 -03:00
|
|
|
goto error;
|
2020-03-18 22:41:21 -03: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
|
|
|
|
2019-12-05 21:43:30 -04:00
|
|
|
error:
|
2019-11-22 12:52:42 -04:00
|
|
|
*tstate_p = NULL;
|
|
|
|
|
|
|
|
/* Oops, it didn't work. Undo it all. */
|
2014-11-20 07:39:37 -04:00
|
|
|
PyErr_PrintEx(0);
|
|
|
|
PyThreadState_Clear(tstate);
|
|
|
|
PyThreadState_Delete(tstate);
|
|
|
|
PyInterpreterState_Delete(interp);
|
2019-11-20 06:17:17 -04:00
|
|
|
PyThreadState_Swap(save_tstate);
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2019-11-22 12:52:42 -04:00
|
|
|
return status;
|
2017-11-15 22:11:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadState *
|
2020-05-01 06:33:44 -03:00
|
|
|
_Py_NewInterpreter(int isolated_subinterpreter)
|
2017-11-15 22:11:45 -04:00
|
|
|
{
|
2019-03-18 13:10:29 -03:00
|
|
|
PyThreadState *tstate = NULL;
|
2020-05-01 06:33:44 -03:00
|
|
|
PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
|
2019-05-27 11:39:22 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-01 06:33:44 -03:00
|
|
|
PyThreadState *
|
|
|
|
Py_NewInterpreter(void)
|
|
|
|
{
|
|
|
|
return _Py_NewInterpreter(0);
|
|
|
|
}
|
|
|
|
|
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()) {
|
2020-03-06 19:54:20 -04:00
|
|
|
Py_FatalError("thread is not current");
|
2019-06-19 19:05:23 -03:00
|
|
|
}
|
2021-10-28 09:59:11 -03:00
|
|
|
if (tstate->cframe->current_frame != NULL) {
|
2020-03-06 19:54:20 -04:00
|
|
|
Py_FatalError("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
|
|
|
|
2021-02-19 10:10:45 -04:00
|
|
|
_PyAtExit_Call(tstate->interp);
|
2017-12-20 06:17:58 -04:00
|
|
|
|
2021-12-07 17:03:47 -04:00
|
|
|
if (tstate != interp->threads.head || tstate->next != NULL) {
|
2020-03-06 19:54:20 -04:00
|
|
|
Py_FatalError("not the last thread");
|
2019-06-19 19:05:23 -03:00
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2020-10-30 14:03:28 -03:00
|
|
|
finalize_modules(tstate);
|
|
|
|
|
2019-11-20 13:39:12 -04:00
|
|
|
finalize_interp_clear(tstate);
|
2021-02-19 10:10:45 -04:00
|
|
|
finalize_interp_delete(tstate->interp);
|
2014-11-20 07:39:37 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-10-26 03:43:39 -03:00
|
|
|
if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
return _PyStatus_ERR("Failed to test __main__.__builtins__");
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
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.
|
|
|
|
*/
|
2020-10-26 03:43:39 -03:00
|
|
|
loader = _PyDict_GetItemStringWithError(d, "__loader__");
|
2014-11-20 07:39:37 -04:00
|
|
|
if (loader == NULL || loader == Py_None) {
|
2020-10-26 03:43:39 -03:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
return _PyStatus_ERR("Failed to test __main__.__loader__");
|
|
|
|
}
|
2014-11-20 07:39:37 -04:00
|
|
|
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.
|
|
|
|
|
2021-11-28 14:40:27 -04:00
|
|
|
fcntl(fd, F_GETFD) is even faster, because it only checks the process table.
|
2021-12-13 21:31:41 -04:00
|
|
|
It is preferred over dup() when available, since it cannot fail with the
|
|
|
|
"too many open files" error (EMFILE).
|
2021-11-28 14:40:27 -04:00
|
|
|
|
2019-04-17 13:09:12 -03:00
|
|
|
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).
|
|
|
|
|
2021-12-13 21:31:41 -04:00
|
|
|
Only use dup() on Linux where dup() is enough to detect invalid FD
|
|
|
|
(bpo-32849).
|
2021-11-28 14:40:27 -04:00
|
|
|
*/
|
2019-04-17 13:09:12 -03:00
|
|
|
if (fd < 0) {
|
2015-09-04 12:29:57 -03:00
|
|
|
return 0;
|
2019-04-17 13:09:12 -03:00
|
|
|
}
|
2021-12-02 07:19:30 -04:00
|
|
|
#if defined(F_GETFD) && ( \
|
|
|
|
defined(__linux__) || \
|
|
|
|
defined(__APPLE__) || \
|
|
|
|
defined(__wasm__))
|
2021-12-13 08:57:59 -04:00
|
|
|
return fcntl(fd, F_GETFD) >= 0;
|
|
|
|
#elif defined(__linux__)
|
|
|
|
int 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
|
|
|
}
|
|
|
|
return (fd2 >= 0);
|
2021-12-13 08:57:59 -04:00
|
|
|
#elif defined(MS_WINDOWS)
|
|
|
|
HANDLE hfile;
|
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
|
|
|
hfile = (HANDLE)_get_osfhandle(fd);
|
|
|
|
_Py_END_SUPPRESS_IPH
|
|
|
|
return (hfile != INVALID_HANDLE_VALUE
|
|
|
|
&& GetFileType(hfile) != FILE_TYPE_UNKNOWN);
|
2019-04-17 13:09:12 -03:00
|
|
|
#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;
|
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";
|
2022-02-08 16:39:07 -04:00
|
|
|
buf = _PyObject_CallMethod(io, &_Py_ID(open), "isiOOOO",
|
|
|
|
fd, mode, buffering,
|
|
|
|
Py_None, Py_None, /* encoding, errors */
|
|
|
|
Py_None, Py_False); /* newline, closefd */
|
2014-11-20 07:39:37 -04:00
|
|
|
if (buf == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (buffering) {
|
2022-02-08 16:39:07 -04:00
|
|
|
raw = PyObject_GetAttr(buf, &_Py_ID(raw));
|
2014-11-20 07:39:37 -04:00
|
|
|
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);
|
2022-02-08 16:39:07 -04:00
|
|
|
if (text == NULL || PyObject_SetAttr(raw, &_Py_ID(name), text) < 0)
|
2014-11-20 07:39:37 -04:00
|
|
|
goto error;
|
2022-02-08 16:39:07 -04:00
|
|
|
res = PyObject_CallMethodNoArgs(raw, &_Py_ID(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;
|
2020-01-01 18:21:43 -04:00
|
|
|
if (buffered_stdio && (isatty || fd == fileno(stderr)))
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
stream = _PyObject_CallMethod(io, &_Py_ID(TextIOWrapper), "OOOsOO",
|
|
|
|
buf, encoding_str, errors_str,
|
|
|
|
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);
|
2022-02-08 16:39:07 -04:00
|
|
|
if (!text || PyObject_SetAttr(stream, &_Py_ID(mode), text) < 0)
|
2014-11-20 07:39:37 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-12 05:44:53 -03:00
|
|
|
/* Set builtins.open to io.open */
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2020-03-12 04:49:05 -03:00
|
|
|
init_set_builtins_open(void)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
2022-06-14 01:15:26 -03:00
|
|
|
PyObject *wrapper;
|
2014-11-20 07:39:37 -04:00
|
|
|
PyObject *bimod = NULL;
|
2019-11-22 11:19:14 -04:00
|
|
|
PyStatus res = _PyStatus_OK();
|
|
|
|
|
|
|
|
if (!(bimod = PyImport_ImportModule("builtins"))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:15:26 -03:00
|
|
|
if (!(wrapper = _PyImport_GetModuleAttrString("io", "open"))) {
|
2019-11-22 11:19:14 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set builtins.open */
|
|
|
|
if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
|
|
|
|
Py_DECREF(wrapper);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(wrapper);
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
error:
|
|
|
|
res = _PyStatus_ERR("can't initialize io.open");
|
|
|
|
|
|
|
|
done:
|
|
|
|
Py_XDECREF(bimod);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-12 05:44:53 -03:00
|
|
|
/* Create sys.stdin, sys.stdout and sys.stderr */
|
2019-11-22 11:19:14 -04:00
|
|
|
static PyStatus
|
|
|
|
init_sys_streams(PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
PyObject *iomod = NULL;
|
2014-11-20 07:39:37 -04:00
|
|
|
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();
|
2020-04-12 22:04:28 -03:00
|
|
|
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
|
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
|
|
|
if (!(iomod = PyImport_ImportModule("io"))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2022-02-08 16:39:07 -04:00
|
|
|
_PySys_SetAttr(&_Py_ID(stdin), std);
|
2014-11-20 07:39:37 -04:00
|
|
|
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);
|
2022-02-08 16:39:07 -04:00
|
|
|
_PySys_SetAttr(&_Py_ID(stdout), std);
|
2014-11-20 07:39:37 -04:00
|
|
|
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;
|
|
|
|
}
|
2022-02-08 16:39:07 -04:00
|
|
|
if (_PySys_SetAttr(&_Py_ID(stderr), std) < 0) {
|
2014-11-20 07:39:37 -04:00
|
|
|
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();
|
2014-11-20 07:39:37 -04:00
|
|
|
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
|
|
|
{
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "\n");
|
2016-03-14 12:53:12 -03:00
|
|
|
|
|
|
|
/* 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
|
2020-03-12 04:49:05 -03:00
|
|
|
_Py_FatalError_PrintExc(PyThreadState *tstate)
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
2015-03-24 09:46:18 -03:00
|
|
|
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() */
|
2022-02-08 16:39:07 -04:00
|
|
|
res = PyObject_CallMethodNoArgs(ferr, &_Py_ID(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
|
2021-01-18 13:34:56 -04:00
|
|
|
fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime)
|
2019-09-17 20:35:33 -03:00
|
|
|
{
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "Python runtime state: ");
|
2020-03-06 19:24:23 -04:00
|
|
|
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
|
|
|
|
if (finalizing) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "finalizing (tstate=0x");
|
|
|
|
_Py_DumpHexadecimal(fd, (uintptr_t)finalizing, sizeof(finalizing) * 2);
|
|
|
|
PUTS(fd, ")");
|
2019-09-17 20:35:33 -03:00
|
|
|
}
|
|
|
|
else if (runtime->initialized) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "initialized");
|
2019-09-17 20:35:33 -03:00
|
|
|
}
|
|
|
|
else if (runtime->core_initialized) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "core initialized");
|
2019-09-17 20:35:33 -03:00
|
|
|
}
|
|
|
|
else if (runtime->preinitialized) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "preinitialized");
|
2019-09-17 20:35:33 -03:00
|
|
|
}
|
|
|
|
else if (runtime->preinitializing) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "preinitializing");
|
2019-09-17 20:35:33 -03:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "unknown");
|
2019-09-17 20:35:33 -03:00
|
|
|
}
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "\n");
|
2019-09-17 20:35:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-25 15:27:36 -03:00
|
|
|
static inline void _Py_NO_RETURN
|
|
|
|
fatal_error_exit(int status)
|
|
|
|
{
|
|
|
|
if (status < 0) {
|
|
|
|
#if defined(MS_WINDOWS) && defined(_DEBUG)
|
|
|
|
DebugBreak();
|
|
|
|
#endif
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-19 18:35:27 -04:00
|
|
|
// Dump the list of extension modules of sys.modules, excluding stdlib modules
|
2021-01-25 18:12:50 -04:00
|
|
|
// (sys.stdlib_module_names), into fd file descriptor.
|
2021-01-19 18:35:27 -04:00
|
|
|
//
|
2021-01-18 15:47:13 -04:00
|
|
|
// This function is called by a signal handler in faulthandler: avoid memory
|
2021-01-19 18:35:27 -04:00
|
|
|
// allocations and keep the implementation simple. For example, the list is not
|
|
|
|
// sorted on purpose.
|
2021-01-18 15:47:13 -04:00
|
|
|
void
|
|
|
|
_Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
|
|
|
|
{
|
|
|
|
if (interp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PyObject *modules = interp->modules;
|
2021-01-19 18:35:27 -04:00
|
|
|
if (modules == NULL || !PyDict_Check(modules)) {
|
2021-01-18 15:47:13 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-25 08:24:42 -04:00
|
|
|
Py_ssize_t pos;
|
|
|
|
PyObject *key, *value;
|
|
|
|
|
|
|
|
// Avoid PyDict_GetItemString() which calls PyUnicode_FromString(),
|
|
|
|
// memory cannot be allocated on the heap in a signal handler.
|
|
|
|
// Iterate on the dict instead.
|
2021-01-25 18:12:50 -04:00
|
|
|
PyObject *stdlib_module_names = NULL;
|
2021-04-07 18:12:45 -03:00
|
|
|
if (interp->sysdict != NULL) {
|
|
|
|
pos = 0;
|
|
|
|
while (PyDict_Next(interp->sysdict, &pos, &key, &value)) {
|
|
|
|
if (PyUnicode_Check(key)
|
|
|
|
&& PyUnicode_CompareWithASCIIString(key, "stdlib_module_names") == 0) {
|
|
|
|
stdlib_module_names = value;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-25 08:24:42 -04:00
|
|
|
}
|
|
|
|
}
|
2021-01-25 18:12:50 -04:00
|
|
|
// If we failed to get sys.stdlib_module_names or it's not a frozenset,
|
2021-01-25 08:24:42 -04:00
|
|
|
// don't exclude stdlib modules.
|
2021-01-25 18:12:50 -04:00
|
|
|
if (stdlib_module_names != NULL && !PyFrozenSet_Check(stdlib_module_names)) {
|
|
|
|
stdlib_module_names = NULL;
|
2021-01-25 08:24:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// List extensions
|
2021-01-19 18:35:27 -04:00
|
|
|
int header = 1;
|
|
|
|
Py_ssize_t count = 0;
|
2021-01-25 08:24:42 -04:00
|
|
|
pos = 0;
|
2021-01-18 15:47:13 -04:00
|
|
|
while (PyDict_Next(modules, &pos, &key, &value)) {
|
|
|
|
if (!PyUnicode_Check(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!_PyModule_IsExtension(value)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-19 18:35:27 -04:00
|
|
|
// Use the module name from the sys.modules key,
|
|
|
|
// don't attempt to get the module object name.
|
2021-01-25 18:12:50 -04:00
|
|
|
if (stdlib_module_names != NULL) {
|
2021-01-25 08:24:42 -04:00
|
|
|
int is_stdlib_ext = 0;
|
|
|
|
|
2021-01-25 18:12:50 -04:00
|
|
|
Py_ssize_t i = 0;
|
2021-01-25 08:24:42 -04:00
|
|
|
PyObject *item;
|
|
|
|
Py_hash_t hash;
|
2021-01-25 18:12:50 -04:00
|
|
|
while (_PySet_NextEntry(stdlib_module_names, &i, &item, &hash)) {
|
2021-01-25 08:24:42 -04:00
|
|
|
if (PyUnicode_Check(item)
|
|
|
|
&& PyUnicode_Compare(key, item) == 0)
|
|
|
|
{
|
|
|
|
is_stdlib_ext = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_stdlib_ext) {
|
|
|
|
// Ignore stdlib extension
|
|
|
|
continue;
|
2021-01-19 18:35:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header) {
|
|
|
|
PUTS(fd, "\nExtension modules: ");
|
|
|
|
header = 0;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-18 15:47:13 -04:00
|
|
|
PUTS(fd, ", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
_Py_DumpASCII(fd, key);
|
2021-01-19 18:35:27 -04:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
PUTS(fd, " (total: ");
|
|
|
|
_Py_DumpDecimal(fd, count);
|
|
|
|
PUTS(fd, ")");
|
|
|
|
PUTS(fd, "\n");
|
2021-01-18 15:47:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-25 17:02:55 -04:00
|
|
|
static void _Py_NO_RETURN
|
2021-01-18 13:34:56 -04:00
|
|
|
fatal_error(int fd, int header, const char *prefix, const char *msg,
|
2020-03-25 15:27:36 -03:00
|
|
|
int status)
|
2014-11-20 07:39:37 -04:00
|
|
|
{
|
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. */
|
2020-03-25 15:27:36 -03:00
|
|
|
fatal_error_exit(status);
|
2015-03-24 21:55:14 -03:00
|
|
|
}
|
|
|
|
reentrant = 1;
|
2014-11-20 07:39:37 -04:00
|
|
|
|
2020-03-25 15:27:36 -03:00
|
|
|
if (header) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "Fatal Python error: ");
|
2020-03-25 15:27:36 -03:00
|
|
|
if (prefix) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, prefix);
|
|
|
|
PUTS(fd, ": ");
|
2020-03-25 15:27:36 -03:00
|
|
|
}
|
|
|
|
if (msg) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, msg);
|
2020-03-25 15:27:36 -03:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "<message not set>");
|
2020-03-25 15:27:36 -03:00
|
|
|
}
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, "\n");
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2015-03-24 08:01:30 -03:00
|
|
|
|
2019-09-17 20:35:33 -03:00
|
|
|
_PyRuntimeState *runtime = &_PyRuntime;
|
2021-01-18 13:34:56 -04:00
|
|
|
fatal_error_dump_runtime(fd, runtime);
|
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.
|
|
|
|
*/
|
2021-01-18 13:34:56 -04:00
|
|
|
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
|
|
|
|
PyInterpreterState *interp = NULL;
|
2019-09-17 20:35:33 -03:00
|
|
|
PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
|
2021-01-18 13:34:56 -04:00
|
|
|
if (tstate != NULL) {
|
|
|
|
interp = tstate->interp;
|
|
|
|
}
|
|
|
|
else if (tss_tstate != NULL) {
|
|
|
|
interp = tss_tstate->interp;
|
|
|
|
}
|
2019-09-17 20:35:33 -03:00
|
|
|
int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
|
2021-01-18 13:34:56 -04:00
|
|
|
|
2018-10-31 20:26:41 -03:00
|
|
|
if (has_tstate_and_gil) {
|
|
|
|
/* If an exception is set, print the exception with its traceback */
|
2020-03-12 04:49:05 -03:00
|
|
|
if (!_Py_FatalError_PrintExc(tss_tstate)) {
|
2018-10-31 20:26:41 -03:00
|
|
|
/* 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
|
|
|
|
2021-01-18 15:47:13 -04:00
|
|
|
_Py_DumpExtensionModules(fd, interp);
|
|
|
|
|
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 */
|
|
|
|
|
2020-03-25 15:27:36 -03:00
|
|
|
fatal_error_exit(status);
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:27:36 -03:00
|
|
|
|
2020-03-06 19:54:20 -04:00
|
|
|
#undef Py_FatalError
|
|
|
|
|
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)
|
|
|
|
{
|
2021-01-18 13:34:56 -04:00
|
|
|
fatal_error(fileno(stderr), 1, NULL, msg, -1);
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:27:36 -03:00
|
|
|
|
2020-03-06 19:54:20 -04:00
|
|
|
void _Py_NO_RETURN
|
|
|
|
_Py_FatalErrorFunc(const char *func, const char *msg)
|
|
|
|
{
|
2021-01-18 13:34:56 -04:00
|
|
|
fatal_error(fileno(stderr), 1, func, msg, -1);
|
2020-03-25 15:27:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _Py_NO_RETURN
|
|
|
|
_Py_FatalErrorFormat(const char *func, const char *format, ...)
|
|
|
|
{
|
|
|
|
static int reentrant = 0;
|
|
|
|
if (reentrant) {
|
|
|
|
/* _Py_FatalErrorFormat() caused a second fatal error */
|
|
|
|
fatal_error_exit(-1);
|
|
|
|
}
|
|
|
|
reentrant = 1;
|
|
|
|
|
|
|
|
FILE *stream = stderr;
|
2021-01-18 13:34:56 -04:00
|
|
|
const int fd = fileno(stream);
|
|
|
|
PUTS(fd, "Fatal Python error: ");
|
2020-03-25 15:27:36 -03:00
|
|
|
if (func) {
|
2021-01-18 13:34:56 -04:00
|
|
|
PUTS(fd, func);
|
|
|
|
PUTS(fd, ": ");
|
2020-03-25 15:27:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
va_list vargs;
|
|
|
|
va_start(vargs, format);
|
|
|
|
vfprintf(stream, format, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
fputs("\n", stream);
|
|
|
|
fflush(stream);
|
|
|
|
|
2021-01-18 13:34:56 -04:00
|
|
|
fatal_error(fd, 0, NULL, NULL, -1);
|
2020-03-06 19:54:20 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:27:36 -03:00
|
|
|
|
2021-09-21 18:04:34 -03:00
|
|
|
void _Py_NO_RETURN
|
|
|
|
_Py_FatalRefcountErrorFunc(const char *func, const char *msg)
|
|
|
|
{
|
|
|
|
_Py_FatalErrorFormat(func,
|
|
|
|
"%s: bug likely caused by a refcount error "
|
|
|
|
"in a C extension",
|
|
|
|
msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)) {
|
2021-01-18 13:34:56 -04:00
|
|
|
fatal_error(fileno(stderr), 1, 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
|
|
|
}
|
|
|
|
|
2020-12-14 18:07:54 -04:00
|
|
|
|
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
|
|
|
{
|
|
|
|
PyObject *result;
|
2022-02-08 16:39:07 -04:00
|
|
|
PyObject *threading = PyImport_GetModule(&_Py_ID(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;
|
|
|
|
}
|
2022-02-08 16:39:07 -04:00
|
|
|
result = PyObject_CallMethodNoArgs(threading, &_Py_ID(_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-09 17:37:27 -04:00
|
|
|
int
|
|
|
|
_Py_FdIsInteractive(FILE *fp, PyObject *filename)
|
|
|
|
{
|
|
|
|
if (isatty((int)fileno(fp))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!Py_InteractiveFlag) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (filename == NULL) ||
|
|
|
|
(PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0) ||
|
|
|
|
(PyUnicode_CompareWithASCIIString(filename, "???") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-20 07:39:37 -04:00
|
|
|
/* 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);
|
2021-03-05 01:49:30 -04:00
|
|
|
/* Using SA_ONSTACK is friendlier to other C/C++/Golang-VM code that
|
|
|
|
* extension module or embedding code may use where tiny thread stacks
|
|
|
|
* are used. https://bugs.python.org/issue43390 */
|
|
|
|
context.sa_flags = SA_ONSTACK;
|
2014-11-20 07:39:37 -04:00
|
|
|
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
|