| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1 | /* Python interpreter top-level routines, including init/exit */ | 
 | 2 |  | 
 | 3 | #include "Python.h" | 
 | 4 |  | 
 | 5 | #include "Python-ast.h" | 
 | 6 | #undef Yield /* undefine macro conflicting with winbase.h */ | 
| Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 7 | #include "internal/context.h" | 
 | 8 | #include "internal/hamt.h" | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 9 | #include "internal/pystate.h" | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 10 | #include "grammar.h" | 
 | 11 | #include "node.h" | 
 | 12 | #include "token.h" | 
 | 13 | #include "parsetok.h" | 
 | 14 | #include "errcode.h" | 
 | 15 | #include "code.h" | 
 | 16 | #include "symtable.h" | 
 | 17 | #include "ast.h" | 
 | 18 | #include "marshal.h" | 
 | 19 | #include "osdefs.h" | 
 | 20 | #include <locale.h> | 
 | 21 |  | 
 | 22 | #ifdef HAVE_SIGNAL_H | 
 | 23 | #include <signal.h> | 
 | 24 | #endif | 
 | 25 |  | 
 | 26 | #ifdef MS_WINDOWS | 
 | 27 | #include "malloc.h" /* for alloca */ | 
 | 28 | #endif | 
 | 29 |  | 
 | 30 | #ifdef HAVE_LANGINFO_H | 
 | 31 | #include <langinfo.h> | 
 | 32 | #endif | 
 | 33 |  | 
 | 34 | #ifdef MS_WINDOWS | 
 | 35 | #undef BYTE | 
 | 36 | #include "windows.h" | 
| Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 37 |  | 
 | 38 | extern PyTypeObject PyWindowsConsoleIO_Type; | 
 | 39 | #define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type)) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 40 | #endif | 
 | 41 |  | 
 | 42 | _Py_IDENTIFIER(flush); | 
 | 43 | _Py_IDENTIFIER(name); | 
 | 44 | _Py_IDENTIFIER(stdin); | 
 | 45 | _Py_IDENTIFIER(stdout); | 
 | 46 | _Py_IDENTIFIER(stderr); | 
| Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 47 | _Py_IDENTIFIER(threading); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 48 |  | 
 | 49 | #ifdef __cplusplus | 
 | 50 | extern "C" { | 
 | 51 | #endif | 
 | 52 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 53 | extern grammar _PyParser_Grammar; /* From graminit.c */ | 
 | 54 |  | 
 | 55 | /* Forward */ | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 56 | static _PyInitError add_main_module(PyInterpreterState *interp); | 
 | 57 | static _PyInitError initfsencoding(PyInterpreterState *interp); | 
 | 58 | static _PyInitError initsite(void); | 
| Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 59 | static _PyInitError init_sys_streams(PyInterpreterState *interp); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 60 | static _PyInitError initsigs(void); | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 61 | static void call_py_exitfuncs(PyInterpreterState *); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 62 | static void wait_for_thread_shutdown(void); | 
 | 63 | static void call_ll_exitfuncs(void); | 
 | 64 | extern int _PyUnicode_Init(void); | 
 | 65 | extern int _PyStructSequence_Init(void); | 
 | 66 | extern void _PyUnicode_Fini(void); | 
 | 67 | extern int _PyLong_Init(void); | 
 | 68 | extern void PyLong_Fini(void); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 69 | extern _PyInitError _PyFaulthandler_Init(int enable); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 70 | extern void _PyFaulthandler_Fini(void); | 
 | 71 | extern void _PyHash_Fini(void); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 72 | extern int _PyTraceMalloc_Init(int enable); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 73 | extern int _PyTraceMalloc_Fini(void); | 
| Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 74 | extern void _Py_ReadyTypes(void); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 75 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 76 | extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); | 
 | 77 | extern void _PyGILState_Fini(void); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 78 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 79 | _PyRuntimeState _PyRuntime = _PyRuntimeState_INIT; | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 80 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 81 | _PyInitError | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 82 | _PyRuntime_Initialize(void) | 
 | 83 | { | 
 | 84 |     /* XXX We only initialize once in the process, which aligns with | 
 | 85 |        the static initialization of the former globals now found in | 
 | 86 |        _PyRuntime.  However, _PyRuntime *should* be initialized with | 
 | 87 |        every Py_Initialize() call, but doing so breaks the runtime. | 
 | 88 |        This is because the runtime state is not properly finalized | 
 | 89 |        currently. */ | 
 | 90 |     static int initialized = 0; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 91 |     if (initialized) { | 
 | 92 |         return _Py_INIT_OK(); | 
 | 93 |     } | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 94 |     initialized = 1; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 95 |  | 
 | 96 |     return _PyRuntimeState_Init(&_PyRuntime); | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 97 | } | 
 | 98 |  | 
 | 99 | void | 
 | 100 | _PyRuntime_Finalize(void) | 
 | 101 | { | 
 | 102 |     _PyRuntimeState_Fini(&_PyRuntime); | 
 | 103 | } | 
 | 104 |  | 
 | 105 | int | 
 | 106 | _Py_IsFinalizing(void) | 
 | 107 | { | 
 | 108 |     return _PyRuntime.finalizing != NULL; | 
 | 109 | } | 
 | 110 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 111 | /* Hack to force loading of object files */ | 
 | 112 | int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ | 
 | 113 |     PyOS_mystrnicmp; /* Python/pystrcmp.o */ | 
 | 114 |  | 
 | 115 | /* PyModule_GetWarningsModule is no longer necessary as of 2.6 | 
 | 116 | since _warnings is builtin.  This API should not be used. */ | 
 | 117 | PyObject * | 
 | 118 | PyModule_GetWarningsModule(void) | 
 | 119 | { | 
 | 120 |     return PyImport_ImportModule("warnings"); | 
 | 121 | } | 
 | 122 |  | 
| Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 123 |  | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 124 | /* APIs to access the initialization flags | 
 | 125 |  * | 
 | 126 |  * Can be called prior to Py_Initialize. | 
 | 127 |  */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 128 |  | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 129 | int | 
 | 130 | _Py_IsCoreInitialized(void) | 
 | 131 | { | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 132 |     return _PyRuntime.core_initialized; | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 133 | } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 134 |  | 
 | 135 | int | 
 | 136 | Py_IsInitialized(void) | 
 | 137 | { | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 138 |     return _PyRuntime.initialized; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 139 | } | 
 | 140 |  | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 141 |  | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 142 | /* Global initializations.  Can be undone by Py_FinalizeEx().  Don't | 
 | 143 |    call this twice without an intervening Py_FinalizeEx() call.  When | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 144 |    initializations fail, a fatal error is issued and the function does | 
 | 145 |    not return.  On return, the first thread and interpreter state have | 
 | 146 |    been created. | 
 | 147 |  | 
 | 148 |    Locking: you must hold the interpreter lock while calling this. | 
 | 149 |    (If the lock has not yet been initialized, that's equivalent to | 
 | 150 |    having the lock, but you cannot use multiple threads.) | 
 | 151 |  | 
 | 152 | */ | 
 | 153 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 154 | static char* | 
 | 155 | get_codec_name(const char *encoding) | 
 | 156 | { | 
| Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 157 |     const char *name_utf8; | 
 | 158 |     char *name_str; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 159 |     PyObject *codec, *name = NULL; | 
 | 160 |  | 
 | 161 |     codec = _PyCodec_Lookup(encoding); | 
 | 162 |     if (!codec) | 
 | 163 |         goto error; | 
 | 164 |  | 
 | 165 |     name = _PyObject_GetAttrId(codec, &PyId_name); | 
 | 166 |     Py_CLEAR(codec); | 
 | 167 |     if (!name) | 
 | 168 |         goto error; | 
 | 169 |  | 
| Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 170 |     name_utf8 = PyUnicode_AsUTF8(name); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 171 |     if (name_utf8 == NULL) | 
 | 172 |         goto error; | 
 | 173 |     name_str = _PyMem_RawStrdup(name_utf8); | 
 | 174 |     Py_DECREF(name); | 
 | 175 |     if (name_str == NULL) { | 
 | 176 |         PyErr_NoMemory(); | 
 | 177 |         return NULL; | 
 | 178 |     } | 
 | 179 |     return name_str; | 
 | 180 |  | 
 | 181 | error: | 
 | 182 |     Py_XDECREF(codec); | 
 | 183 |     Py_XDECREF(name); | 
 | 184 |     return NULL; | 
 | 185 | } | 
 | 186 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 187 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 188 | static _PyInitError | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 189 | initimport(PyInterpreterState *interp, PyObject *sysmod) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 190 | { | 
 | 191 |     PyObject *importlib; | 
 | 192 |     PyObject *impmod; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 193 |     PyObject *value; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 194 |     _PyInitError err; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 195 |  | 
 | 196 |     /* Import _importlib through its frozen version, _frozen_importlib. */ | 
 | 197 |     if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 198 |         return _Py_INIT_ERR("can't import _frozen_importlib"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 199 |     } | 
 | 200 |     else if (Py_VerboseFlag) { | 
 | 201 |         PySys_FormatStderr("import _frozen_importlib # frozen\n"); | 
 | 202 |     } | 
 | 203 |     importlib = PyImport_AddModule("_frozen_importlib"); | 
 | 204 |     if (importlib == NULL) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 205 |         return _Py_INIT_ERR("couldn't get _frozen_importlib from sys.modules"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 206 |     } | 
 | 207 |     interp->importlib = importlib; | 
 | 208 |     Py_INCREF(interp->importlib); | 
 | 209 |  | 
| Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 210 |     interp->import_func = PyDict_GetItemString(interp->builtins, "__import__"); | 
 | 211 |     if (interp->import_func == NULL) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 212 |         return _Py_INIT_ERR("__import__ not found"); | 
| Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 213 |     Py_INCREF(interp->import_func); | 
 | 214 |  | 
| Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 215 |     /* Import the _imp module */ | 
| Benjamin Peterson | c65ef77 | 2018-01-29 11:33:57 -0800 | [diff] [blame] | 216 |     impmod = PyInit__imp(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 217 |     if (impmod == NULL) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 218 |         return _Py_INIT_ERR("can't import _imp"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 219 |     } | 
 | 220 |     else if (Py_VerboseFlag) { | 
| Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 221 |         PySys_FormatStderr("import _imp # builtin\n"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 222 |     } | 
| Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 223 |     if (_PyImport_SetModuleString("_imp", impmod) < 0) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 224 |         return _Py_INIT_ERR("can't save _imp to sys.modules"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 225 |     } | 
 | 226 |  | 
| Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 227 |     /* Install importlib as the implementation of import */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 228 |     value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); | 
 | 229 |     if (value == NULL) { | 
 | 230 |         PyErr_Print(); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 231 |         return _Py_INIT_ERR("importlib install failed"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 232 |     } | 
 | 233 |     Py_DECREF(value); | 
 | 234 |     Py_DECREF(impmod); | 
 | 235 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 236 |     err = _PyImportZip_Init(); | 
 | 237 |     if (_Py_INIT_FAILED(err)) { | 
 | 238 |         return err; | 
 | 239 |     } | 
 | 240 |  | 
 | 241 |     return _Py_INIT_OK(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 242 | } | 
 | 243 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 244 | static _PyInitError | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 245 | initexternalimport(PyInterpreterState *interp) | 
 | 246 | { | 
 | 247 |     PyObject *value; | 
 | 248 |     value = PyObject_CallMethod(interp->importlib, | 
 | 249 |                                 "_install_external_importers", ""); | 
 | 250 |     if (value == NULL) { | 
 | 251 |         PyErr_Print(); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 252 |         return _Py_INIT_ERR("external importer setup failed"); | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 253 |     } | 
| Stéphane Wirtel | ab1cb80 | 2017-06-08 13:13:20 +0200 | [diff] [blame] | 254 |     Py_DECREF(value); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 255 |     return _Py_INIT_OK(); | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 256 | } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 257 |  | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 258 | /* Helper functions to better handle the legacy C locale | 
 | 259 |  * | 
 | 260 |  * The legacy C locale assumes ASCII as the default text encoding, which | 
 | 261 |  * causes problems not only for the CPython runtime, but also other | 
 | 262 |  * components like GNU readline. | 
 | 263 |  * | 
 | 264 |  * Accordingly, when the CLI detects it, it attempts to coerce it to a | 
 | 265 |  * more capable UTF-8 based alternative as follows: | 
 | 266 |  * | 
 | 267 |  *     if (_Py_LegacyLocaleDetected()) { | 
 | 268 |  *         _Py_CoerceLegacyLocale(); | 
 | 269 |  *     } | 
 | 270 |  * | 
 | 271 |  * See the documentation of the PYTHONCOERCECLOCALE setting for more details. | 
 | 272 |  * | 
 | 273 |  * Locale coercion also impacts the default error handler for the standard | 
 | 274 |  * streams: while the usual default is "strict", the default for the legacy | 
 | 275 |  * C locale and for any of the coercion target locales is "surrogateescape". | 
 | 276 |  */ | 
 | 277 |  | 
 | 278 | int | 
 | 279 | _Py_LegacyLocaleDetected(void) | 
 | 280 | { | 
 | 281 | #ifndef MS_WINDOWS | 
 | 282 |     /* On non-Windows systems, the C locale is considered a legacy locale */ | 
| Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 283 |     /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat | 
 | 284 |      *                 the POSIX locale as a simple alias for the C locale, so | 
 | 285 |      *                 we may also want to check for that explicitly. | 
 | 286 |      */ | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 287 |     const char *ctype_loc = setlocale(LC_CTYPE, NULL); | 
 | 288 |     return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0; | 
 | 289 | #else | 
 | 290 |     /* Windows uses code pages instead of locales, so no locale is legacy */ | 
 | 291 |     return 0; | 
 | 292 | #endif | 
 | 293 | } | 
 | 294 |  | 
| Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 295 | static const char *_C_LOCALE_WARNING = | 
 | 296 |     "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII " | 
 | 297 |     "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, " | 
 | 298 |     "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible " | 
 | 299 |     "locales is recommended.\n"; | 
 | 300 |  | 
| Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 301 | static void | 
| Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 302 | _emit_stderr_warning_for_legacy_locale(const _PyCoreConfig *core_config) | 
| Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 303 | { | 
| Victor Stinner | cf21504 | 2018-08-29 22:56:06 +0200 | [diff] [blame] | 304 |     if (core_config->coerce_c_locale_warn && _Py_LegacyLocaleDetected()) { | 
 | 305 |         PySys_FormatStderr("%s", _C_LOCALE_WARNING); | 
| Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 306 |     } | 
 | 307 | } | 
 | 308 |  | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 309 | typedef struct _CandidateLocale { | 
 | 310 |     const char *locale_name; /* The locale to try as a coercion target */ | 
 | 311 | } _LocaleCoercionTarget; | 
 | 312 |  | 
 | 313 | static _LocaleCoercionTarget _TARGET_LOCALES[] = { | 
 | 314 |     {"C.UTF-8"}, | 
 | 315 |     {"C.utf8"}, | 
| Nick Coghlan | 18974c3 | 2017-06-30 00:48:14 +1000 | [diff] [blame] | 316 |     {"UTF-8"}, | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 317 |     {NULL} | 
 | 318 | }; | 
 | 319 |  | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 320 |  | 
 | 321 | int | 
 | 322 | _Py_IsLocaleCoercionTarget(const char *ctype_loc) | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 323 | { | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 324 |     const _LocaleCoercionTarget *target = NULL; | 
 | 325 |     for (target = _TARGET_LOCALES; target->locale_name; target++) { | 
 | 326 |         if (strcmp(ctype_loc, target->locale_name) == 0) { | 
 | 327 |             return 1; | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 328 |         } | 
| Victor Stinner | 124b9eb | 2018-08-29 01:29:06 +0200 | [diff] [blame] | 329 |     } | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 330 |     return 0; | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 331 | } | 
 | 332 |  | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 333 |  | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 334 | #ifdef PY_COERCE_C_LOCALE | 
| Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 335 | static const char C_LOCALE_COERCION_WARNING[] = | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 336 |     "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale " | 
 | 337 |     "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n"; | 
 | 338 |  | 
 | 339 | static void | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 340 | _coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target) | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 341 | { | 
 | 342 |     const char *newloc = target->locale_name; | 
 | 343 |  | 
 | 344 |     /* Reset locale back to currently configured defaults */ | 
| xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 345 |     _Py_SetLocaleFromEnv(LC_ALL); | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 346 |  | 
 | 347 |     /* Set the relevant locale environment variable */ | 
 | 348 |     if (setenv("LC_CTYPE", newloc, 1)) { | 
 | 349 |         fprintf(stderr, | 
 | 350 |                 "Error setting LC_CTYPE, skipping C locale coercion\n"); | 
 | 351 |         return; | 
 | 352 |     } | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 353 |     if (warn) { | 
| Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 354 |         fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc); | 
| Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 355 |     } | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 356 |  | 
 | 357 |     /* Reconfigure with the overridden environment variables */ | 
| xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 358 |     _Py_SetLocaleFromEnv(LC_ALL); | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 359 | } | 
 | 360 | #endif | 
 | 361 |  | 
 | 362 | void | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 363 | _Py_CoerceLegacyLocale(int warn) | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 364 | { | 
 | 365 | #ifdef PY_COERCE_C_LOCALE | 
| Victor Stinner | 8ea0911 | 2018-09-03 17:05:18 +0200 | [diff] [blame] | 366 |     char *oldloc = NULL; | 
 | 367 |  | 
 | 368 |     oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL)); | 
 | 369 |     if (oldloc == NULL) { | 
 | 370 |         return; | 
 | 371 |     } | 
 | 372 |  | 
| Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 373 |     const char *locale_override = getenv("LC_ALL"); | 
 | 374 |     if (locale_override == NULL || *locale_override == '\0') { | 
 | 375 |         /* LC_ALL is also not set (or is set to an empty string) */ | 
 | 376 |         const _LocaleCoercionTarget *target = NULL; | 
 | 377 |         for (target = _TARGET_LOCALES; target->locale_name; target++) { | 
 | 378 |             const char *new_locale = setlocale(LC_CTYPE, | 
 | 379 |                                                target->locale_name); | 
 | 380 |             if (new_locale != NULL) { | 
| xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 381 | #if !defined(__APPLE__) && !defined(__ANDROID__) && \ | 
| Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 382 | defined(HAVE_LANGINFO_H) && defined(CODESET) | 
 | 383 |                 /* Also ensure that nl_langinfo works in this locale */ | 
 | 384 |                 char *codeset = nl_langinfo(CODESET); | 
 | 385 |                 if (!codeset || *codeset == '\0') { | 
 | 386 |                     /* CODESET is not set or empty, so skip coercion */ | 
 | 387 |                     new_locale = NULL; | 
 | 388 |                     _Py_SetLocaleFromEnv(LC_CTYPE); | 
 | 389 |                     continue; | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 390 |                 } | 
| Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 391 | #endif | 
 | 392 |                 /* Successfully configured locale, so make it the default */ | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 393 |                 _coerce_default_locale_settings(warn, target); | 
| Victor Stinner | 8ea0911 | 2018-09-03 17:05:18 +0200 | [diff] [blame] | 394 |                 goto done; | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 395 |             } | 
 | 396 |         } | 
 | 397 |     } | 
 | 398 |     /* No C locale warning here, as Py_Initialize will emit one later */ | 
| Victor Stinner | 8ea0911 | 2018-09-03 17:05:18 +0200 | [diff] [blame] | 399 |  | 
 | 400 |     setlocale(LC_CTYPE, oldloc); | 
 | 401 |  | 
 | 402 | done: | 
 | 403 |     PyMem_RawFree(oldloc); | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 404 | #endif | 
 | 405 | } | 
 | 406 |  | 
| xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 407 | /* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to | 
 | 408 |  * isolate the idiosyncrasies of different libc implementations. It reads the | 
 | 409 |  * appropriate environment variable and uses its value to select the locale for | 
 | 410 |  * 'category'. */ | 
 | 411 | char * | 
 | 412 | _Py_SetLocaleFromEnv(int category) | 
 | 413 | { | 
 | 414 | #ifdef __ANDROID__ | 
 | 415 |     const char *locale; | 
 | 416 |     const char **pvar; | 
 | 417 | #ifdef PY_COERCE_C_LOCALE | 
 | 418 |     const char *coerce_c_locale; | 
 | 419 | #endif | 
 | 420 |     const char *utf8_locale = "C.UTF-8"; | 
 | 421 |     const char *env_var_set[] = { | 
 | 422 |         "LC_ALL", | 
 | 423 |         "LC_CTYPE", | 
 | 424 |         "LANG", | 
 | 425 |         NULL, | 
 | 426 |     }; | 
 | 427 |  | 
 | 428 |     /* Android setlocale(category, "") doesn't check the environment variables | 
 | 429 |      * and incorrectly sets the "C" locale at API 24 and older APIs. We only | 
 | 430 |      * check the environment variables listed in env_var_set. */ | 
 | 431 |     for (pvar=env_var_set; *pvar; pvar++) { | 
 | 432 |         locale = getenv(*pvar); | 
 | 433 |         if (locale != NULL && *locale != '\0') { | 
 | 434 |             if (strcmp(locale, utf8_locale) == 0 || | 
 | 435 |                     strcmp(locale, "en_US.UTF-8") == 0) { | 
 | 436 |                 return setlocale(category, utf8_locale); | 
 | 437 |             } | 
 | 438 |             return setlocale(category, "C"); | 
 | 439 |         } | 
 | 440 |     } | 
 | 441 |  | 
 | 442 |     /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of | 
 | 443 |      * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string. | 
 | 444 |      * Quote from POSIX section "8.2 Internationalization Variables": | 
 | 445 |      * "4. If the LANG environment variable is not set or is set to the empty | 
 | 446 |      * string, the implementation-defined default locale shall be used." */ | 
 | 447 |  | 
 | 448 | #ifdef PY_COERCE_C_LOCALE | 
 | 449 |     coerce_c_locale = getenv("PYTHONCOERCECLOCALE"); | 
 | 450 |     if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) { | 
 | 451 |         /* Some other ported code may check the environment variables (e.g. in | 
 | 452 |          * extension modules), so we make sure that they match the locale | 
 | 453 |          * configuration */ | 
 | 454 |         if (setenv("LC_CTYPE", utf8_locale, 1)) { | 
 | 455 |             fprintf(stderr, "Warning: failed setting the LC_CTYPE " | 
 | 456 |                             "environment variable to %s\n", utf8_locale); | 
 | 457 |         } | 
 | 458 |     } | 
 | 459 | #endif | 
 | 460 |     return setlocale(category, utf8_locale); | 
 | 461 | #else /* __ANDROID__ */ | 
 | 462 |     return setlocale(category, ""); | 
 | 463 | #endif /* __ANDROID__ */ | 
 | 464 | } | 
 | 465 |  | 
| Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 466 |  | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 467 | /* Global initializations.  Can be undone by Py_Finalize().  Don't | 
 | 468 |    call this twice without an intervening Py_Finalize() call. | 
 | 469 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 470 |    Every call to _Py_InitializeCore, Py_Initialize or Py_InitializeEx | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 471 |    must have a corresponding call to Py_Finalize. | 
 | 472 |  | 
 | 473 |    Locking: you must hold the interpreter lock while calling these APIs. | 
 | 474 |    (If the lock has not yet been initialized, that's equivalent to | 
 | 475 |    having the lock, but you cannot use multiple threads.) | 
 | 476 |  | 
 | 477 | */ | 
 | 478 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 479 | static _PyInitError | 
 | 480 | _Py_Initialize_ReconfigureCore(PyInterpreterState *interp, | 
 | 481 |                                const _PyCoreConfig *core_config) | 
 | 482 | { | 
 | 483 |     if (core_config->allocator != NULL) { | 
 | 484 |         const char *allocator = _PyMem_GetAllocatorsName(); | 
 | 485 |         if (allocator == NULL || strcmp(core_config->allocator, allocator) != 0) { | 
 | 486 |             return _Py_INIT_USER_ERR("cannot modify memory allocator " | 
 | 487 |                                      "after first Py_Initialize()"); | 
 | 488 |         } | 
 | 489 |     } | 
 | 490 |  | 
 | 491 |     _PyCoreConfig_SetGlobalConfig(core_config); | 
 | 492 |  | 
 | 493 |     if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) { | 
 | 494 |         return _Py_INIT_ERR("failed to copy core config"); | 
 | 495 |     } | 
 | 496 |     core_config = &interp->core_config; | 
 | 497 |  | 
 | 498 |     if (core_config->_install_importlib) { | 
 | 499 |         _PyInitError err = _PyCoreConfig_SetPathConfig(core_config); | 
 | 500 |         if (_Py_INIT_FAILED(err)) { | 
 | 501 |             return err; | 
 | 502 |         } | 
 | 503 |     } | 
 | 504 |     return _Py_INIT_OK(); | 
 | 505 | } | 
 | 506 |  | 
 | 507 |  | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 508 | /* Begin interpreter initialization | 
 | 509 |  * | 
 | 510 |  * On return, the first thread and interpreter state have been created, | 
 | 511 |  * but the compiler, signal handling, multithreading and | 
 | 512 |  * multiple interpreter support, and codec infrastructure are not yet | 
 | 513 |  * available. | 
 | 514 |  * | 
 | 515 |  * The import system will support builtin and frozen modules only. | 
 | 516 |  * The only supported io is writing to sys.stderr | 
 | 517 |  * | 
 | 518 |  * If any operation invoked by this function fails, a fatal error is | 
 | 519 |  * issued and the function does not return. | 
 | 520 |  * | 
 | 521 |  * Any code invoked from this function should *not* assume it has access | 
 | 522 |  * to the Python C API (unless the API is explicitly listed as being | 
 | 523 |  * safe to call without calling Py_Initialize first) | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 524 |  * | 
 | 525 |  * The caller is responsible to call _PyCoreConfig_Read(). | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 526 |  */ | 
 | 527 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 528 | static _PyInitError | 
 | 529 | _Py_InitializeCore_impl(PyInterpreterState **interp_p, | 
 | 530 |                         const _PyCoreConfig *core_config) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 531 | { | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 532 |     PyInterpreterState *interp; | 
 | 533 |     _PyInitError err; | 
 | 534 |  | 
 | 535 |     /* bpo-34008: For backward compatibility reasons, calling Py_Main() after | 
 | 536 |        Py_Initialize() ignores the new configuration. */ | 
 | 537 |     if (_PyRuntime.core_initialized) { | 
 | 538 |         PyThreadState *tstate = PyThreadState_GET(); | 
 | 539 |         if (!tstate) { | 
 | 540 |             return _Py_INIT_ERR("failed to read thread state"); | 
 | 541 |         } | 
 | 542 |  | 
 | 543 |         interp = tstate->interp; | 
 | 544 |         if (interp == NULL) { | 
 | 545 |             return _Py_INIT_ERR("can't make main interpreter"); | 
 | 546 |         } | 
 | 547 |         *interp_p = interp; | 
 | 548 |  | 
 | 549 |         return _Py_Initialize_ReconfigureCore(interp, core_config); | 
 | 550 |     } | 
 | 551 |  | 
 | 552 |     if (_PyRuntime.initialized) { | 
 | 553 |         return _Py_INIT_ERR("main interpreter already initialized"); | 
 | 554 |     } | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 555 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 556 |     _PyCoreConfig_SetGlobalConfig(core_config); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 557 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 558 |     err = _PyRuntime_Initialize(); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 559 |     if (_Py_INIT_FAILED(err)) { | 
 | 560 |         return err; | 
 | 561 |     } | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 562 |  | 
| Victor Stinner | 31e9908 | 2017-12-20 23:41:38 +0100 | [diff] [blame] | 563 |     if (core_config->allocator != NULL) { | 
 | 564 |         if (_PyMem_SetupAllocators(core_config->allocator) < 0) { | 
 | 565 |             return _Py_INIT_USER_ERR("Unknown PYTHONMALLOC allocator"); | 
 | 566 |         } | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 567 |     } | 
 | 568 |  | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 569 |     /* Py_Finalize leaves _Py_Finalizing set in order to help daemon | 
 | 570 |      * threads behave a little more gracefully at interpreter shutdown. | 
 | 571 |      * We clobber it here so the new interpreter can start with a clean | 
 | 572 |      * slate. | 
 | 573 |      * | 
 | 574 |      * However, this may still lead to misbehaviour if there are daemon | 
 | 575 |      * threads still hanging around from a previous Py_Initialize/Finalize | 
 | 576 |      * pair :( | 
 | 577 |      */ | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 578 |     _PyRuntime.finalizing = NULL; | 
 | 579 |  | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 580 |     err = _Py_HashRandomization_Init(core_config); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 581 |     if (_Py_INIT_FAILED(err)) { | 
 | 582 |         return err; | 
 | 583 |     } | 
 | 584 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 585 |     err = _PyInterpreterState_Enable(&_PyRuntime); | 
 | 586 |     if (_Py_INIT_FAILED(err)) { | 
 | 587 |         return err; | 
 | 588 |     } | 
 | 589 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 590 |     interp = PyInterpreterState_New(); | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 591 |     if (interp == NULL) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 592 |         return _Py_INIT_ERR("can't make main interpreter"); | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 593 |     } | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 594 |     *interp_p = interp; | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 595 |  | 
 | 596 |     if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) { | 
 | 597 |         return _Py_INIT_ERR("failed to copy core config"); | 
 | 598 |     } | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 599 |     core_config = &interp->core_config; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 600 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 601 |     PyThreadState *tstate = PyThreadState_New(interp); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 602 |     if (tstate == NULL) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 603 |         return _Py_INIT_ERR("can't make first thread"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 604 |     (void) PyThreadState_Swap(tstate); | 
 | 605 |  | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 606 |     /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 607 |        destroying the GIL might fail when it is being referenced from | 
 | 608 |        another running thread (see issue #9901). | 
 | 609 |        Instead we destroy the previously created GIL here, which ensures | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 610 |        that we can call Py_Initialize / Py_FinalizeEx multiple times. */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 611 |     _PyEval_FiniThreads(); | 
| Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 612 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 613 |     /* Auto-thread-state API */ | 
 | 614 |     _PyGILState_Init(interp, tstate); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 615 |  | 
| Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 616 |     /* Create the GIL */ | 
 | 617 |     PyEval_InitThreads(); | 
 | 618 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 619 |     _Py_ReadyTypes(); | 
 | 620 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 621 |     if (!_PyLong_Init()) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 622 |         return _Py_INIT_ERR("can't init longs"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 623 |  | 
 | 624 |     if (!PyByteArray_Init()) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 625 |         return _Py_INIT_ERR("can't init bytearray"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 626 |  | 
 | 627 |     if (!_PyFloat_Init()) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 628 |         return _Py_INIT_ERR("can't init float"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 629 |  | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 630 |     PyObject *modules = PyDict_New(); | 
 | 631 |     if (modules == NULL) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 632 |         return _Py_INIT_ERR("can't make modules dictionary"); | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 633 |     interp->modules = modules; | 
 | 634 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 635 |     PyObject *sysmod; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 636 |     err = _PySys_BeginInit(&sysmod); | 
 | 637 |     if (_Py_INIT_FAILED(err)) { | 
 | 638 |         return err; | 
 | 639 |     } | 
 | 640 |  | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 641 |     interp->sysdict = PyModule_GetDict(sysmod); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 642 |     if (interp->sysdict == NULL) { | 
 | 643 |         return _Py_INIT_ERR("can't initialize sys dict"); | 
 | 644 |     } | 
 | 645 |  | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 646 |     Py_INCREF(interp->sysdict); | 
 | 647 |     PyDict_SetItemString(interp->sysdict, "modules", modules); | 
 | 648 |     _PyImport_FixupBuiltin(sysmod, "sys", modules); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 649 |  | 
 | 650 |     /* Init Unicode implementation; relies on the codec registry */ | 
 | 651 |     if (_PyUnicode_Init() < 0) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 652 |         return _Py_INIT_ERR("can't initialize unicode"); | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 653 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 654 |     if (_PyStructSequence_Init() < 0) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 655 |         return _Py_INIT_ERR("can't initialize structseq"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 656 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 657 |     PyObject *bimod = _PyBuiltin_Init(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 658 |     if (bimod == NULL) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 659 |         return _Py_INIT_ERR("can't initialize builtins modules"); | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 660 |     _PyImport_FixupBuiltin(bimod, "builtins", modules); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 661 |     interp->builtins = PyModule_GetDict(bimod); | 
 | 662 |     if (interp->builtins == NULL) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 663 |         return _Py_INIT_ERR("can't initialize builtins dict"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 664 |     Py_INCREF(interp->builtins); | 
 | 665 |  | 
 | 666 |     /* initialize builtin exceptions */ | 
 | 667 |     _PyExc_Init(bimod); | 
 | 668 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 669 |     /* Set up a preliminary stderr printer until we have enough | 
 | 670 |        infrastructure for the io module in place. */ | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 671 |     PyObject *pstderr = PyFile_NewStdPrinter(fileno(stderr)); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 672 |     if (pstderr == NULL) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 673 |         return _Py_INIT_ERR("can't set preliminary stderr"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 674 |     _PySys_SetObjectId(&PyId_stderr, pstderr); | 
 | 675 |     PySys_SetObject("__stderr__", pstderr); | 
 | 676 |     Py_DECREF(pstderr); | 
 | 677 |  | 
| Victor Stinner | 672b6ba | 2017-12-06 17:25:50 +0100 | [diff] [blame] | 678 |     err = _PyImport_Init(interp); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 679 |     if (_Py_INIT_FAILED(err)) { | 
 | 680 |         return err; | 
 | 681 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 682 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 683 |     err = _PyImportHooks_Init(); | 
 | 684 |     if (_Py_INIT_FAILED(err)) { | 
 | 685 |         return err; | 
 | 686 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 687 |  | 
 | 688 |     /* Initialize _warnings. */ | 
| Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 689 |     if (_PyWarnings_Init() == NULL) { | 
| Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 690 |         return _Py_INIT_ERR("can't initialize warnings"); | 
 | 691 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 692 |  | 
| Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 693 |     if (!_PyContext_Init()) | 
 | 694 |         return _Py_INIT_ERR("can't init context"); | 
 | 695 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 696 |     if (core_config->_install_importlib) { | 
| Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 697 |         err = _PyCoreConfig_SetPathConfig(core_config); | 
 | 698 |         if (_Py_INIT_FAILED(err)) { | 
 | 699 |             return err; | 
 | 700 |         } | 
 | 701 |     } | 
 | 702 |  | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 703 |     /* This call sets up builtin and frozen import support */ | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 704 |     if (core_config->_install_importlib) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 705 |         err = initimport(interp, sysmod); | 
 | 706 |         if (_Py_INIT_FAILED(err)) { | 
 | 707 |             return err; | 
 | 708 |         } | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 709 |     } | 
 | 710 |  | 
 | 711 |     /* Only when we get here is the runtime core fully initialized */ | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 712 |     _PyRuntime.core_initialized = 1; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 713 |     return _Py_INIT_OK(); | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 714 | } | 
 | 715 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 716 | _PyInitError | 
 | 717 | _Py_InitializeCore(PyInterpreterState **interp_p, | 
 | 718 |                    const _PyCoreConfig *src_config) | 
 | 719 | { | 
 | 720 |     assert(src_config != NULL); | 
 | 721 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 722 |     PyMemAllocatorEx old_alloc; | 
 | 723 |     _PyInitError err; | 
 | 724 |  | 
 | 725 |     /* Copy the configuration, since _PyCoreConfig_Read() modifies it | 
 | 726 |        (and the input configuration is read only). */ | 
 | 727 |     _PyCoreConfig config = _PyCoreConfig_INIT; | 
 | 728 |  | 
| Victor Stinner | 177d921 | 2018-08-29 11:25:15 +0200 | [diff] [blame] | 729 |     /* Set LC_CTYPE to the user preferred locale */ | 
| Victor Stinner | 2c8ddcf | 2018-08-29 00:16:53 +0200 | [diff] [blame] | 730 |     _Py_SetLocaleFromEnv(LC_CTYPE); | 
| Victor Stinner | 2c8ddcf | 2018-08-29 00:16:53 +0200 | [diff] [blame] | 731 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 732 |     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); | 
 | 733 |     if (_PyCoreConfig_Copy(&config, src_config) >= 0) { | 
 | 734 |         err = _PyCoreConfig_Read(&config); | 
 | 735 |     } | 
 | 736 |     else { | 
 | 737 |         err = _Py_INIT_ERR("failed to copy core config"); | 
 | 738 |     } | 
 | 739 |     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); | 
 | 740 |  | 
 | 741 |     if (_Py_INIT_FAILED(err)) { | 
 | 742 |         goto done; | 
 | 743 |     } | 
 | 744 |  | 
 | 745 |     err = _Py_InitializeCore_impl(interp_p, &config); | 
 | 746 |  | 
 | 747 | done: | 
 | 748 |     _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); | 
 | 749 |     _PyCoreConfig_Clear(&config); | 
 | 750 |     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); | 
 | 751 |  | 
 | 752 |     return err; | 
 | 753 | } | 
 | 754 |  | 
| Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 755 | /* Py_Initialize() has already been called: update the main interpreter | 
 | 756 |    configuration. Example of bpo-34008: Py_Main() called after | 
 | 757 |    Py_Initialize(). */ | 
 | 758 | static _PyInitError | 
 | 759 | _Py_ReconfigureMainInterpreter(PyInterpreterState *interp, | 
 | 760 |                                const _PyMainInterpreterConfig *config) | 
 | 761 | { | 
 | 762 |     if (config->argv != NULL) { | 
 | 763 |         int res = PyDict_SetItemString(interp->sysdict, "argv", config->argv); | 
 | 764 |         if (res < 0) { | 
 | 765 |             return _Py_INIT_ERR("fail to set sys.argv"); | 
 | 766 |         } | 
 | 767 |     } | 
 | 768 |     return _Py_INIT_OK(); | 
 | 769 | } | 
 | 770 |  | 
| Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 771 | /* Update interpreter state based on supplied configuration settings | 
 | 772 |  * | 
 | 773 |  * After calling this function, most of the restrictions on the interpreter | 
 | 774 |  * are lifted. The only remaining incomplete settings are those related | 
 | 775 |  * to the main module (sys.argv[0], __main__ metadata) | 
 | 776 |  * | 
 | 777 |  * Calling this when the interpreter is not initializing, is already | 
 | 778 |  * initialized or without a valid current thread state is a fatal error. | 
 | 779 |  * Other errors should be reported as normal Python exceptions with a | 
 | 780 |  * non-zero return code. | 
 | 781 |  */ | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 782 | _PyInitError | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 783 | _Py_InitializeMainInterpreter(PyInterpreterState *interp, | 
 | 784 |                               const _PyMainInterpreterConfig *config) | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 785 | { | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 786 |     if (!_PyRuntime.core_initialized) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 787 |         return _Py_INIT_ERR("runtime core not initialized"); | 
| Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 788 |     } | 
| Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 789 |  | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 790 |     /* Configure the main interpreter */ | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 791 |     if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) { | 
 | 792 |         return _Py_INIT_ERR("failed to copy main interpreter config"); | 
 | 793 |     } | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 794 |     config = &interp->config; | 
 | 795 |     _PyCoreConfig *core_config = &interp->core_config; | 
| Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 796 |  | 
| Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 797 |     if (_PyRuntime.initialized) { | 
 | 798 |         return _Py_ReconfigureMainInterpreter(interp, config); | 
 | 799 |     } | 
 | 800 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 801 |     if (!core_config->_install_importlib) { | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 802 |         /* Special mode for freeze_importlib: run with no import system | 
 | 803 |          * | 
 | 804 |          * This means anything which needs support from extension modules | 
 | 805 |          * or pure Python code in the standard library won't work. | 
 | 806 |          */ | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 807 |         _PyRuntime.initialized = 1; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 808 |         return _Py_INIT_OK(); | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 809 |     } | 
| Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 810 |  | 
| Victor Stinner | 33c377e | 2017-12-05 15:12:41 +0100 | [diff] [blame] | 811 |     if (_PyTime_Init() < 0) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 812 |         return _Py_INIT_ERR("can't initialize time"); | 
| Victor Stinner | 33c377e | 2017-12-05 15:12:41 +0100 | [diff] [blame] | 813 |     } | 
| Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 814 |  | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 815 |     if (_PySys_EndInit(interp->sysdict, interp) < 0) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 816 |         return _Py_INIT_ERR("can't finish initializing sys"); | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 817 |     } | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 818 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 819 |     _PyInitError err = initexternalimport(interp); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 820 |     if (_Py_INIT_FAILED(err)) { | 
 | 821 |         return err; | 
 | 822 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 823 |  | 
 | 824 |     /* initialize the faulthandler module */ | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 825 |     err = _PyFaulthandler_Init(core_config->faulthandler); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 826 |     if (_Py_INIT_FAILED(err)) { | 
 | 827 |         return err; | 
 | 828 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 829 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 830 |     err = initfsencoding(interp); | 
 | 831 |     if (_Py_INIT_FAILED(err)) { | 
 | 832 |         return err; | 
 | 833 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 834 |  | 
| Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 835 |     if (interp->config.install_signal_handlers) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 836 |         err = initsigs(); /* Signal handling stuff, including initintr() */ | 
 | 837 |         if (_Py_INIT_FAILED(err)) { | 
 | 838 |             return err; | 
 | 839 |         } | 
 | 840 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 841 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 842 |     if (_PyTraceMalloc_Init(core_config->tracemalloc) < 0) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 843 |         return _Py_INIT_ERR("can't initialize tracemalloc"); | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 844 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 845 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 846 |     err = add_main_module(interp); | 
 | 847 |     if (_Py_INIT_FAILED(err)) { | 
 | 848 |         return err; | 
 | 849 |     } | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 850 |  | 
| Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 851 |     err = init_sys_streams(interp); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 852 |     if (_Py_INIT_FAILED(err)) { | 
 | 853 |         return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 854 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 855 |  | 
 | 856 |     /* Initialize warnings. */ | 
| Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 857 |     if (interp->config.warnoptions != NULL && | 
 | 858 |         PyList_Size(interp->config.warnoptions) > 0) | 
 | 859 |     { | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 860 |         PyObject *warnings_module = PyImport_ImportModule("warnings"); | 
 | 861 |         if (warnings_module == NULL) { | 
 | 862 |             fprintf(stderr, "'import warnings' failed; traceback:\n"); | 
 | 863 |             PyErr_Print(); | 
 | 864 |         } | 
 | 865 |         Py_XDECREF(warnings_module); | 
 | 866 |     } | 
 | 867 |  | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 868 |     _PyRuntime.initialized = 1; | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 869 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 870 |     if (core_config->site_import) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 871 |         err = initsite(); /* Module site */ | 
 | 872 |         if (_Py_INIT_FAILED(err)) { | 
 | 873 |             return err; | 
 | 874 |         } | 
 | 875 |     } | 
| Victor Stinner | cf21504 | 2018-08-29 22:56:06 +0200 | [diff] [blame] | 876 |  | 
 | 877 | #ifndef MS_WINDOWS | 
 | 878 |     _emit_stderr_warning_for_legacy_locale(core_config); | 
 | 879 | #endif | 
 | 880 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 881 |     return _Py_INIT_OK(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 882 | } | 
 | 883 |  | 
| Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 884 | #undef _INIT_DEBUG_PRINT | 
 | 885 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 886 | _PyInitError | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 887 | _Py_InitializeFromConfig(const _PyCoreConfig *config) | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 888 | { | 
| Benjamin Peterson | acd282f | 2018-09-11 15:11:06 -0700 | [diff] [blame^] | 889 |     PyInterpreterState *interp = NULL; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 890 |     _PyInitError err; | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 891 |     err = _Py_InitializeCore(&interp, config); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 892 |     if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 893 |         return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 894 |     } | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 895 |     config = &interp->core_config; | 
| Victor Stinner | bc8ac6b | 2017-11-30 18:03:55 +0100 | [diff] [blame] | 896 |  | 
| Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 897 |     _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT; | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 898 |     err = _PyMainInterpreterConfig_Read(&main_config, config); | 
| Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 899 |     if (!_Py_INIT_FAILED(err)) { | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 900 |         err = _Py_InitializeMainInterpreter(interp, &main_config); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 901 |     } | 
| Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 902 |     _PyMainInterpreterConfig_Clear(&main_config); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 903 |     if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 904 |         return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 905 |     } | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 906 |     return _Py_INIT_OK(); | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 907 | } | 
 | 908 |  | 
 | 909 |  | 
 | 910 | void | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 911 | Py_InitializeEx(int install_sigs) | 
 | 912 | { | 
| Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 913 |     if (_PyRuntime.initialized) { | 
 | 914 |         /* bpo-33932: Calling Py_Initialize() twice does nothing. */ | 
 | 915 |         return; | 
 | 916 |     } | 
 | 917 |  | 
 | 918 |     _PyInitError err; | 
 | 919 |     _PyCoreConfig config = _PyCoreConfig_INIT; | 
 | 920 |     config.install_signal_handlers = install_sigs; | 
 | 921 |  | 
 | 922 |     err = _Py_InitializeFromConfig(&config); | 
 | 923 |     _PyCoreConfig_Clear(&config); | 
 | 924 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 925 |     if (_Py_INIT_FAILED(err)) { | 
 | 926 |         _Py_FatalInitError(err); | 
 | 927 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 928 | } | 
 | 929 |  | 
 | 930 | void | 
 | 931 | Py_Initialize(void) | 
 | 932 | { | 
 | 933 |     Py_InitializeEx(1); | 
 | 934 | } | 
 | 935 |  | 
 | 936 |  | 
 | 937 | #ifdef COUNT_ALLOCS | 
 | 938 | extern void dump_counts(FILE*); | 
 | 939 | #endif | 
 | 940 |  | 
 | 941 | /* Flush stdout and stderr */ | 
 | 942 |  | 
 | 943 | static int | 
 | 944 | file_is_closed(PyObject *fobj) | 
 | 945 | { | 
 | 946 |     int r; | 
 | 947 |     PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); | 
 | 948 |     if (tmp == NULL) { | 
 | 949 |         PyErr_Clear(); | 
 | 950 |         return 0; | 
 | 951 |     } | 
 | 952 |     r = PyObject_IsTrue(tmp); | 
 | 953 |     Py_DECREF(tmp); | 
 | 954 |     if (r < 0) | 
 | 955 |         PyErr_Clear(); | 
 | 956 |     return r > 0; | 
 | 957 | } | 
 | 958 |  | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 959 | static int | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 960 | flush_std_files(void) | 
 | 961 | { | 
 | 962 |     PyObject *fout = _PySys_GetObjectId(&PyId_stdout); | 
 | 963 |     PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); | 
 | 964 |     PyObject *tmp; | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 965 |     int status = 0; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 966 |  | 
 | 967 |     if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { | 
| Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 968 |         tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL); | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 969 |         if (tmp == NULL) { | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 970 |             PyErr_WriteUnraisable(fout); | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 971 |             status = -1; | 
 | 972 |         } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 973 |         else | 
 | 974 |             Py_DECREF(tmp); | 
 | 975 |     } | 
 | 976 |  | 
 | 977 |     if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { | 
| Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 978 |         tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 979 |         if (tmp == NULL) { | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 980 |             PyErr_Clear(); | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 981 |             status = -1; | 
 | 982 |         } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 983 |         else | 
 | 984 |             Py_DECREF(tmp); | 
 | 985 |     } | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 986 |  | 
 | 987 |     return status; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 988 | } | 
 | 989 |  | 
 | 990 | /* Undo the effect of Py_Initialize(). | 
 | 991 |  | 
 | 992 |    Beware: if multiple interpreter and/or thread states exist, these | 
 | 993 |    are not wiped out; only the current thread and interpreter state | 
 | 994 |    are deleted.  But since everything else is deleted, those other | 
 | 995 |    interpreter and thread states should no longer be used. | 
 | 996 |  | 
 | 997 |    (XXX We should do better, e.g. wipe out all interpreters and | 
 | 998 |    threads.) | 
 | 999 |  | 
 | 1000 |    Locking: as above. | 
 | 1001 |  | 
 | 1002 | */ | 
 | 1003 |  | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1004 | int | 
 | 1005 | Py_FinalizeEx(void) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1006 | { | 
 | 1007 |     PyInterpreterState *interp; | 
 | 1008 |     PyThreadState *tstate; | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1009 |     int status = 0; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1010 |  | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1011 |     if (!_PyRuntime.initialized) | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1012 |         return status; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1013 |  | 
 | 1014 |     wait_for_thread_shutdown(); | 
 | 1015 |  | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 1016 |     /* Get current thread state and interpreter pointer */ | 
 | 1017 |     tstate = PyThreadState_GET(); | 
 | 1018 |     interp = tstate->interp; | 
 | 1019 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1020 |     /* The interpreter is still entirely intact at this point, and the | 
 | 1021 |      * exit funcs may be relying on that.  In particular, if some thread | 
 | 1022 |      * or exit func is still waiting to do an import, the import machinery | 
 | 1023 |      * expects Py_IsInitialized() to return true.  So don't say the | 
 | 1024 |      * interpreter is uninitialized until after the exit funcs have run. | 
 | 1025 |      * Note that Threading.py uses an exit func to do a join on all the | 
 | 1026 |      * threads created thru it, so this also protects pending imports in | 
 | 1027 |      * the threads created via Threading. | 
 | 1028 |      */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1029 |  | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 1030 |     call_py_exitfuncs(interp); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1031 |  | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1032 |     /* Copy the core config, PyInterpreterState_Delete() free | 
 | 1033 |        the core config memory */ | 
| Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1034 | #ifdef Py_REF_DEBUG | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1035 |     int show_ref_count = interp->core_config.show_ref_count; | 
| Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1036 | #endif | 
 | 1037 | #ifdef Py_TRACE_REFS | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1038 |     int dump_refs = interp->core_config.dump_refs; | 
| Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1039 | #endif | 
 | 1040 | #ifdef WITH_PYMALLOC | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1041 |     int malloc_stats = interp->core_config.malloc_stats; | 
| Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1042 | #endif | 
| Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1043 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1044 |     /* Remaining threads (e.g. daemon threads) will automatically exit | 
 | 1045 |        after taking the GIL (in PyEval_RestoreThread()). */ | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1046 |     _PyRuntime.finalizing = tstate; | 
 | 1047 |     _PyRuntime.initialized = 0; | 
 | 1048 |     _PyRuntime.core_initialized = 0; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1049 |  | 
| Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1050 |     /* Flush sys.stdout and sys.stderr */ | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1051 |     if (flush_std_files() < 0) { | 
 | 1052 |         status = -1; | 
 | 1053 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1054 |  | 
 | 1055 |     /* Disable signal handling */ | 
 | 1056 |     PyOS_FiniInterrupts(); | 
 | 1057 |  | 
 | 1058 |     /* Collect garbage.  This may call finalizers; it's nice to call these | 
 | 1059 |      * before all modules are destroyed. | 
 | 1060 |      * XXX If a __del__ or weakref callback is triggered here, and tries to | 
 | 1061 |      * XXX import a module, bad things can happen, because Python no | 
 | 1062 |      * XXX longer believes it's initialized. | 
 | 1063 |      * XXX     Fatal Python error: Interpreter not initialized (version mismatch?) | 
 | 1064 |      * XXX is easy to provoke that way.  I've also seen, e.g., | 
 | 1065 |      * XXX     Exception exceptions.ImportError: 'No module named sha' | 
 | 1066 |      * XXX         in <function callback at 0x008F5718> ignored | 
 | 1067 |      * XXX but I'm unclear on exactly how that one happens.  In any case, | 
 | 1068 |      * XXX I haven't seen a real-life report of either of these. | 
 | 1069 |      */ | 
| Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1070 |     _PyGC_CollectIfEnabled(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1071 | #ifdef COUNT_ALLOCS | 
 | 1072 |     /* With COUNT_ALLOCS, it helps to run GC multiple times: | 
 | 1073 |        each collection might release some types from the type | 
 | 1074 |        list, so they become garbage. */ | 
| Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1075 |     while (_PyGC_CollectIfEnabled() > 0) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1076 |         /* nothing */; | 
 | 1077 | #endif | 
| Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1078 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1079 |     /* Destroy all modules */ | 
 | 1080 |     PyImport_Cleanup(); | 
 | 1081 |  | 
| Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1082 |     /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1083 |     if (flush_std_files() < 0) { | 
 | 1084 |         status = -1; | 
 | 1085 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1086 |  | 
 | 1087 |     /* Collect final garbage.  This disposes of cycles created by | 
 | 1088 |      * class definitions, for example. | 
 | 1089 |      * XXX This is disabled because it caused too many problems.  If | 
 | 1090 |      * XXX a __del__ or weakref callback triggers here, Python code has | 
 | 1091 |      * XXX a hard time running, because even the sys module has been | 
 | 1092 |      * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). | 
 | 1093 |      * XXX One symptom is a sequence of information-free messages | 
 | 1094 |      * XXX coming from threads (if a __del__ or callback is invoked, | 
 | 1095 |      * XXX other threads can execute too, and any exception they encounter | 
 | 1096 |      * XXX triggers a comedy of errors as subsystem after subsystem | 
 | 1097 |      * XXX fails to find what it *expects* to find in sys to help report | 
 | 1098 |      * XXX the exception and consequent unexpected failures).  I've also | 
 | 1099 |      * XXX seen segfaults then, after adding print statements to the | 
 | 1100 |      * XXX Python code getting called. | 
 | 1101 |      */ | 
 | 1102 | #if 0 | 
| Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1103 |     _PyGC_CollectIfEnabled(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1104 | #endif | 
 | 1105 |  | 
 | 1106 |     /* Disable tracemalloc after all Python objects have been destroyed, | 
 | 1107 |        so it is possible to use tracemalloc in objects destructor. */ | 
 | 1108 |     _PyTraceMalloc_Fini(); | 
 | 1109 |  | 
 | 1110 |     /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ | 
 | 1111 |     _PyImport_Fini(); | 
 | 1112 |  | 
 | 1113 |     /* Cleanup typeobject.c's internal caches. */ | 
 | 1114 |     _PyType_Fini(); | 
 | 1115 |  | 
 | 1116 |     /* unload faulthandler module */ | 
 | 1117 |     _PyFaulthandler_Fini(); | 
 | 1118 |  | 
 | 1119 |     /* Debugging stuff */ | 
 | 1120 | #ifdef COUNT_ALLOCS | 
| Serhiy Storchaka | 7e160ce | 2016-07-03 21:03:53 +0300 | [diff] [blame] | 1121 |     dump_counts(stderr); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1122 | #endif | 
 | 1123 |     /* dump hash stats */ | 
 | 1124 |     _PyHash_Fini(); | 
 | 1125 |  | 
| Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1126 | #ifdef Py_REF_DEBUG | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1127 |     if (show_ref_count) { | 
| Victor Stinner | 25420fe | 2017-11-20 18:12:22 -0800 | [diff] [blame] | 1128 |         _PyDebug_PrintTotalRefs(); | 
 | 1129 |     } | 
| Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1130 | #endif | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1131 |  | 
 | 1132 | #ifdef Py_TRACE_REFS | 
 | 1133 |     /* Display all objects still alive -- this can invoke arbitrary | 
 | 1134 |      * __repr__ overrides, so requires a mostly-intact interpreter. | 
 | 1135 |      * Alas, a lot of stuff may still be alive now that will be cleaned | 
 | 1136 |      * up later. | 
 | 1137 |      */ | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1138 |     if (dump_refs) { | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1139 |         _Py_PrintReferences(stderr); | 
| Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1140 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1141 | #endif /* Py_TRACE_REFS */ | 
 | 1142 |  | 
 | 1143 |     /* Clear interpreter state and all thread states. */ | 
 | 1144 |     PyInterpreterState_Clear(interp); | 
 | 1145 |  | 
 | 1146 |     /* Now we decref the exception classes.  After this point nothing | 
 | 1147 |        can raise an exception.  That's okay, because each Fini() method | 
 | 1148 |        below has been checked to make sure no exceptions are ever | 
 | 1149 |        raised. | 
 | 1150 |     */ | 
 | 1151 |  | 
 | 1152 |     _PyExc_Fini(); | 
 | 1153 |  | 
 | 1154 |     /* Sundry finalizers */ | 
 | 1155 |     PyMethod_Fini(); | 
 | 1156 |     PyFrame_Fini(); | 
 | 1157 |     PyCFunction_Fini(); | 
 | 1158 |     PyTuple_Fini(); | 
 | 1159 |     PyList_Fini(); | 
 | 1160 |     PySet_Fini(); | 
 | 1161 |     PyBytes_Fini(); | 
 | 1162 |     PyByteArray_Fini(); | 
 | 1163 |     PyLong_Fini(); | 
 | 1164 |     PyFloat_Fini(); | 
 | 1165 |     PyDict_Fini(); | 
 | 1166 |     PySlice_Fini(); | 
 | 1167 |     _PyGC_Fini(); | 
| Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1168 |     _Py_HashRandomization_Fini(); | 
| Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1169 |     _PyArg_Fini(); | 
| Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1170 |     PyAsyncGen_Fini(); | 
| Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 1171 |     _PyContext_Fini(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1172 |  | 
 | 1173 |     /* Cleanup Unicode implementation */ | 
 | 1174 |     _PyUnicode_Fini(); | 
 | 1175 |  | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 1176 |     _Py_ClearFileSystemEncoding(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1177 |  | 
 | 1178 |     /* XXX Still allocated: | 
 | 1179 |        - various static ad-hoc pointers to interned strings | 
 | 1180 |        - int and float free list blocks | 
 | 1181 |        - whatever various modules and libraries allocate | 
 | 1182 |     */ | 
 | 1183 |  | 
 | 1184 |     PyGrammar_RemoveAccelerators(&_PyParser_Grammar); | 
 | 1185 |  | 
 | 1186 |     /* Cleanup auto-thread-state */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1187 |     _PyGILState_Fini(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1188 |  | 
 | 1189 |     /* Delete current thread. After this, many C API calls become crashy. */ | 
 | 1190 |     PyThreadState_Swap(NULL); | 
| Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1191 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1192 |     PyInterpreterState_Delete(interp); | 
 | 1193 |  | 
 | 1194 | #ifdef Py_TRACE_REFS | 
 | 1195 |     /* Display addresses (& refcnts) of all objects still alive. | 
 | 1196 |      * An address can be used to find the repr of the object, printed | 
 | 1197 |      * above by _Py_PrintReferences. | 
 | 1198 |      */ | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1199 |     if (dump_refs) { | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1200 |         _Py_PrintReferenceAddresses(stderr); | 
| Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1201 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1202 | #endif /* Py_TRACE_REFS */ | 
| Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1203 | #ifdef WITH_PYMALLOC | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1204 |     if (malloc_stats) { | 
| Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1205 |         _PyObject_DebugMallocStats(stderr); | 
| Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1206 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1207 | #endif | 
 | 1208 |  | 
 | 1209 |     call_ll_exitfuncs(); | 
| Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1210 |  | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1211 |     _PyRuntime_Finalize(); | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1212 |     return status; | 
 | 1213 | } | 
 | 1214 |  | 
 | 1215 | void | 
 | 1216 | Py_Finalize(void) | 
 | 1217 | { | 
 | 1218 |     Py_FinalizeEx(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1219 | } | 
 | 1220 |  | 
 | 1221 | /* Create and initialize a new interpreter and thread, and return the | 
 | 1222 |    new thread.  This requires that Py_Initialize() has been called | 
 | 1223 |    first. | 
 | 1224 |  | 
 | 1225 |    Unsuccessful initialization yields a NULL pointer.  Note that *no* | 
 | 1226 |    exception information is available even in this case -- the | 
 | 1227 |    exception information is held in the thread, and there is no | 
 | 1228 |    thread. | 
 | 1229 |  | 
 | 1230 |    Locking: as above. | 
 | 1231 |  | 
 | 1232 | */ | 
 | 1233 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1234 | static _PyInitError | 
 | 1235 | new_interpreter(PyThreadState **tstate_p) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1236 | { | 
 | 1237 |     PyInterpreterState *interp; | 
 | 1238 |     PyThreadState *tstate, *save_tstate; | 
 | 1239 |     PyObject *bimod, *sysmod; | 
| Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1240 |     _PyInitError err; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1241 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1242 |     if (!_PyRuntime.initialized) { | 
 | 1243 |         return _Py_INIT_ERR("Py_Initialize must be called first"); | 
 | 1244 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1245 |  | 
| Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1246 |     /* Issue #10915, #15751: The GIL API doesn't work with multiple | 
 | 1247 |        interpreters: disable PyGILState_Check(). */ | 
 | 1248 |     _PyGILState_check_enabled = 0; | 
 | 1249 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1250 |     interp = PyInterpreterState_New(); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1251 |     if (interp == NULL) { | 
 | 1252 |         *tstate_p = NULL; | 
 | 1253 |         return _Py_INIT_OK(); | 
 | 1254 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1255 |  | 
 | 1256 |     tstate = PyThreadState_New(interp); | 
 | 1257 |     if (tstate == NULL) { | 
 | 1258 |         PyInterpreterState_Delete(interp); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1259 |         *tstate_p = NULL; | 
 | 1260 |         return _Py_INIT_OK(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1261 |     } | 
 | 1262 |  | 
 | 1263 |     save_tstate = PyThreadState_Swap(tstate); | 
 | 1264 |  | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1265 |     /* Copy the current interpreter config into the new interpreter */ | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1266 |     _PyCoreConfig *core_config; | 
 | 1267 |     _PyMainInterpreterConfig *config; | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1268 |     if (save_tstate != NULL) { | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1269 |         core_config = &save_tstate->interp->core_config; | 
 | 1270 |         config = &save_tstate->interp->config; | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1271 |     } else { | 
 | 1272 |         /* No current thread state, copy from the main interpreter */ | 
 | 1273 |         PyInterpreterState *main_interp = PyInterpreterState_Main(); | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1274 |         core_config = &main_interp->core_config; | 
 | 1275 |         config = &main_interp->config; | 
 | 1276 |     } | 
 | 1277 |  | 
 | 1278 |     if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) { | 
 | 1279 |         return _Py_INIT_ERR("failed to copy core config"); | 
 | 1280 |     } | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1281 |     core_config = &interp->core_config; | 
| Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1282 |     if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) { | 
 | 1283 |         return _Py_INIT_ERR("failed to copy main interpreter config"); | 
| Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1284 |     } | 
 | 1285 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1286 |     /* XXX The following is lax in error checking */ | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1287 |     PyObject *modules = PyDict_New(); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1288 |     if (modules == NULL) { | 
 | 1289 |         return _Py_INIT_ERR("can't make modules dictionary"); | 
 | 1290 |     } | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1291 |     interp->modules = modules; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1292 |  | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1293 |     sysmod = _PyImport_FindBuiltin("sys", modules); | 
 | 1294 |     if (sysmod != NULL) { | 
 | 1295 |         interp->sysdict = PyModule_GetDict(sysmod); | 
 | 1296 |         if (interp->sysdict == NULL) | 
 | 1297 |             goto handle_error; | 
 | 1298 |         Py_INCREF(interp->sysdict); | 
 | 1299 |         PyDict_SetItemString(interp->sysdict, "modules", modules); | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1300 |         _PySys_EndInit(interp->sysdict, interp); | 
| Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1301 |     } | 
 | 1302 |  | 
 | 1303 |     bimod = _PyImport_FindBuiltin("builtins", modules); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1304 |     if (bimod != NULL) { | 
 | 1305 |         interp->builtins = PyModule_GetDict(bimod); | 
 | 1306 |         if (interp->builtins == NULL) | 
 | 1307 |             goto handle_error; | 
 | 1308 |         Py_INCREF(interp->builtins); | 
 | 1309 |     } | 
 | 1310 |  | 
 | 1311 |     /* initialize builtin exceptions */ | 
 | 1312 |     _PyExc_Init(bimod); | 
 | 1313 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1314 |     if (bimod != NULL && sysmod != NULL) { | 
 | 1315 |         PyObject *pstderr; | 
 | 1316 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1317 |         /* Set up a preliminary stderr printer until we have enough | 
 | 1318 |            infrastructure for the io module in place. */ | 
 | 1319 |         pstderr = PyFile_NewStdPrinter(fileno(stderr)); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1320 |         if (pstderr == NULL) { | 
 | 1321 |             return _Py_INIT_ERR("can't set preliminary stderr"); | 
 | 1322 |         } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1323 |         _PySys_SetObjectId(&PyId_stderr, pstderr); | 
 | 1324 |         PySys_SetObject("__stderr__", pstderr); | 
 | 1325 |         Py_DECREF(pstderr); | 
 | 1326 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1327 |         err = _PyImportHooks_Init(); | 
 | 1328 |         if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1329 |             return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1330 |         } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1331 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1332 |         err = initimport(interp, sysmod); | 
 | 1333 |         if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1334 |             return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1335 |         } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1336 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1337 |         err = initexternalimport(interp); | 
 | 1338 |         if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1339 |             return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1340 |         } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1341 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1342 |         err = initfsencoding(interp); | 
 | 1343 |         if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1344 |             return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1345 |         } | 
 | 1346 |  | 
| Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1347 |         err = init_sys_streams(interp); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1348 |         if (_Py_INIT_FAILED(err)) { | 
 | 1349 |             return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1350 |         } | 
 | 1351 |  | 
 | 1352 |         err = add_main_module(interp); | 
 | 1353 |         if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1354 |             return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1355 |         } | 
 | 1356 |  | 
| Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1357 |         if (core_config->site_import) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1358 |             err = initsite(); | 
 | 1359 |             if (_Py_INIT_FAILED(err)) { | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1360 |                 return err; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1361 |             } | 
 | 1362 |         } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1363 |     } | 
 | 1364 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1365 |     if (PyErr_Occurred()) { | 
 | 1366 |         goto handle_error; | 
 | 1367 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1368 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1369 |     *tstate_p = tstate; | 
 | 1370 |     return _Py_INIT_OK(); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1371 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1372 | handle_error: | 
 | 1373 |     /* Oops, it didn't work.  Undo it all. */ | 
 | 1374 |  | 
 | 1375 |     PyErr_PrintEx(0); | 
 | 1376 |     PyThreadState_Clear(tstate); | 
 | 1377 |     PyThreadState_Swap(save_tstate); | 
 | 1378 |     PyThreadState_Delete(tstate); | 
 | 1379 |     PyInterpreterState_Delete(interp); | 
 | 1380 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1381 |     *tstate_p = NULL; | 
 | 1382 |     return _Py_INIT_OK(); | 
 | 1383 | } | 
 | 1384 |  | 
 | 1385 | PyThreadState * | 
 | 1386 | Py_NewInterpreter(void) | 
 | 1387 | { | 
 | 1388 |     PyThreadState *tstate; | 
 | 1389 |     _PyInitError err = new_interpreter(&tstate); | 
 | 1390 |     if (_Py_INIT_FAILED(err)) { | 
 | 1391 |         _Py_FatalInitError(err); | 
 | 1392 |     } | 
 | 1393 |     return tstate; | 
 | 1394 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1395 | } | 
 | 1396 |  | 
 | 1397 | /* Delete an interpreter and its last thread.  This requires that the | 
 | 1398 |    given thread state is current, that the thread has no remaining | 
 | 1399 |    frames, and that it is its interpreter's only remaining thread. | 
 | 1400 |    It is a fatal error to violate these constraints. | 
 | 1401 |  | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1402 |    (Py_FinalizeEx() doesn't have these constraints -- it zaps | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1403 |    everything, regardless.) | 
 | 1404 |  | 
 | 1405 |    Locking: as above. | 
 | 1406 |  | 
 | 1407 | */ | 
 | 1408 |  | 
 | 1409 | void | 
 | 1410 | Py_EndInterpreter(PyThreadState *tstate) | 
 | 1411 | { | 
 | 1412 |     PyInterpreterState *interp = tstate->interp; | 
 | 1413 |  | 
 | 1414 |     if (tstate != PyThreadState_GET()) | 
 | 1415 |         Py_FatalError("Py_EndInterpreter: thread is not current"); | 
 | 1416 |     if (tstate->frame != NULL) | 
 | 1417 |         Py_FatalError("Py_EndInterpreter: thread still has a frame"); | 
 | 1418 |  | 
 | 1419 |     wait_for_thread_shutdown(); | 
 | 1420 |  | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 1421 |     call_py_exitfuncs(interp); | 
 | 1422 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1423 |     if (tstate != interp->tstate_head || tstate->next != NULL) | 
 | 1424 |         Py_FatalError("Py_EndInterpreter: not the last thread"); | 
 | 1425 |  | 
 | 1426 |     PyImport_Cleanup(); | 
 | 1427 |     PyInterpreterState_Clear(interp); | 
 | 1428 |     PyThreadState_Swap(NULL); | 
 | 1429 |     PyInterpreterState_Delete(interp); | 
 | 1430 | } | 
 | 1431 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1432 | /* Add the __main__ module */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1433 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1434 | static _PyInitError | 
 | 1435 | add_main_module(PyInterpreterState *interp) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1436 | { | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1437 |     PyObject *m, *d, *loader, *ann_dict; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1438 |     m = PyImport_AddModule("__main__"); | 
 | 1439 |     if (m == NULL) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1440 |         return _Py_INIT_ERR("can't create __main__ module"); | 
 | 1441 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1442 |     d = PyModule_GetDict(m); | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1443 |     ann_dict = PyDict_New(); | 
 | 1444 |     if ((ann_dict == NULL) || | 
 | 1445 |         (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1446 |         return _Py_INIT_ERR("Failed to initialize __main__.__annotations__"); | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1447 |     } | 
 | 1448 |     Py_DECREF(ann_dict); | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1449 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1450 |     if (PyDict_GetItemString(d, "__builtins__") == NULL) { | 
 | 1451 |         PyObject *bimod = PyImport_ImportModule("builtins"); | 
 | 1452 |         if (bimod == NULL) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1453 |             return _Py_INIT_ERR("Failed to retrieve builtins module"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1454 |         } | 
 | 1455 |         if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1456 |             return _Py_INIT_ERR("Failed to initialize __main__.__builtins__"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1457 |         } | 
 | 1458 |         Py_DECREF(bimod); | 
 | 1459 |     } | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1460 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1461 |     /* Main is a little special - imp.is_builtin("__main__") will return | 
 | 1462 |      * False, but BuiltinImporter is still the most appropriate initial | 
 | 1463 |      * setting for its __loader__ attribute. A more suitable value will | 
 | 1464 |      * be set if __main__ gets further initialized later in the startup | 
 | 1465 |      * process. | 
 | 1466 |      */ | 
 | 1467 |     loader = PyDict_GetItemString(d, "__loader__"); | 
 | 1468 |     if (loader == NULL || loader == Py_None) { | 
 | 1469 |         PyObject *loader = PyObject_GetAttrString(interp->importlib, | 
 | 1470 |                                                   "BuiltinImporter"); | 
 | 1471 |         if (loader == NULL) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1472 |             return _Py_INIT_ERR("Failed to retrieve BuiltinImporter"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1473 |         } | 
 | 1474 |         if (PyDict_SetItemString(d, "__loader__", loader) < 0) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1475 |             return _Py_INIT_ERR("Failed to initialize __main__.__loader__"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1476 |         } | 
 | 1477 |         Py_DECREF(loader); | 
 | 1478 |     } | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1479 |     return _Py_INIT_OK(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1480 | } | 
 | 1481 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1482 | static _PyInitError | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1483 | initfsencoding(PyInterpreterState *interp) | 
 | 1484 | { | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 1485 |     _PyCoreConfig *config = &interp->core_config; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1486 |  | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 1487 |     char *encoding = get_codec_name(config->filesystem_encoding); | 
 | 1488 |     if (encoding == NULL) { | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1489 |         /* Such error can only occurs in critical situations: no more | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 1490 |            memory, import a module of the standard library failed, etc. */ | 
 | 1491 |         return _Py_INIT_ERR("failed to get the Python codec " | 
 | 1492 |                             "of the filesystem encoding"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1493 |     } | 
| Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 1494 |  | 
 | 1495 |     /* Update the filesystem encoding to the normalized Python codec name. | 
 | 1496 |        For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii" | 
 | 1497 |        (Python codec name). */ | 
 | 1498 |     PyMem_RawFree(config->filesystem_encoding); | 
 | 1499 |     config->filesystem_encoding = encoding; | 
 | 1500 |  | 
 | 1501 |     /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors | 
 | 1502 |        global configuration variables. */ | 
 | 1503 |     if (_Py_SetFileSystemEncoding(config->filesystem_encoding, | 
 | 1504 |                                   config->filesystem_errors) < 0) { | 
 | 1505 |         return _Py_INIT_NO_MEMORY(); | 
 | 1506 |     } | 
 | 1507 |  | 
 | 1508 |     /* PyUnicode can now use the Python codec rather than C implementation | 
 | 1509 |        for the filesystem encoding */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1510 |     interp->fscodec_initialized = 1; | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1511 |     return _Py_INIT_OK(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1512 | } | 
 | 1513 |  | 
 | 1514 | /* Import the site module (not into __main__ though) */ | 
 | 1515 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1516 | static _PyInitError | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1517 | initsite(void) | 
 | 1518 | { | 
 | 1519 |     PyObject *m; | 
 | 1520 |     m = PyImport_ImportModule("site"); | 
 | 1521 |     if (m == NULL) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1522 |         return _Py_INIT_USER_ERR("Failed to import the site module"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1523 |     } | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1524 |     Py_DECREF(m); | 
 | 1525 |     return _Py_INIT_OK(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1526 | } | 
 | 1527 |  | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1528 | /* Check if a file descriptor is valid or not. | 
 | 1529 |    Return 0 if the file descriptor is invalid, return non-zero otherwise. */ | 
 | 1530 | static int | 
 | 1531 | is_valid_fd(int fd) | 
 | 1532 | { | 
| Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1533 | #ifdef __APPLE__ | 
 | 1534 |     /* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe | 
 | 1535 |        and the other side of the pipe is closed, dup(1) succeed, whereas | 
 | 1536 |        fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect | 
 | 1537 |        such error. */ | 
 | 1538 |     struct stat st; | 
 | 1539 |     return (fstat(fd, &st) == 0); | 
 | 1540 | #else | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1541 |     int fd2; | 
| Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 1542 |     if (fd < 0) | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1543 |         return 0; | 
 | 1544 |     _Py_BEGIN_SUPPRESS_IPH | 
| Victor Stinner | 449b271 | 2015-09-29 13:59:50 +0200 | [diff] [blame] | 1545 |     /* Prefer dup() over fstat(). fstat() can require input/output whereas | 
 | 1546 |        dup() doesn't, there is a low risk of EMFILE/ENFILE at Python | 
 | 1547 |        startup. */ | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1548 |     fd2 = dup(fd); | 
 | 1549 |     if (fd2 >= 0) | 
 | 1550 |         close(fd2); | 
 | 1551 |     _Py_END_SUPPRESS_IPH | 
 | 1552 |     return fd2 >= 0; | 
| Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1553 | #endif | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1554 | } | 
 | 1555 |  | 
 | 1556 | /* returns Py_None if the fd is not valid */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1557 | static PyObject* | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1558 | create_stdio(const _PyCoreConfig *config, PyObject* io, | 
| Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1559 |     int fd, int write_mode, const char* name, | 
 | 1560 |     const char* encoding, const char* errors) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1561 | { | 
 | 1562 |     PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; | 
 | 1563 |     const char* mode; | 
 | 1564 |     const char* newline; | 
| Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1565 |     PyObject *line_buffering, *write_through; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1566 |     int buffering, isatty; | 
 | 1567 |     _Py_IDENTIFIER(open); | 
 | 1568 |     _Py_IDENTIFIER(isatty); | 
 | 1569 |     _Py_IDENTIFIER(TextIOWrapper); | 
 | 1570 |     _Py_IDENTIFIER(mode); | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1571 |     const int buffered_stdio = config->buffered_stdio; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1572 |  | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1573 |     if (!is_valid_fd(fd)) | 
 | 1574 |         Py_RETURN_NONE; | 
 | 1575 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1576 |     /* stdin is always opened in buffered mode, first because it shouldn't | 
 | 1577 |        make a difference in common use cases, second because TextIOWrapper | 
 | 1578 |        depends on the presence of a read1() method which only exists on | 
 | 1579 |        buffered streams. | 
 | 1580 |     */ | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1581 |     if (!buffered_stdio && write_mode) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1582 |         buffering = 0; | 
 | 1583 |     else | 
 | 1584 |         buffering = -1; | 
 | 1585 |     if (write_mode) | 
 | 1586 |         mode = "wb"; | 
 | 1587 |     else | 
 | 1588 |         mode = "rb"; | 
 | 1589 |     buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi", | 
 | 1590 |                                  fd, mode, buffering, | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1591 |                                  Py_None, Py_None, /* encoding, errors */ | 
 | 1592 |                                  Py_None, 0); /* newline, closefd */ | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1593 |     if (buf == NULL) | 
 | 1594 |         goto error; | 
 | 1595 |  | 
 | 1596 |     if (buffering) { | 
 | 1597 |         _Py_IDENTIFIER(raw); | 
 | 1598 |         raw = _PyObject_GetAttrId(buf, &PyId_raw); | 
 | 1599 |         if (raw == NULL) | 
 | 1600 |             goto error; | 
 | 1601 |     } | 
 | 1602 |     else { | 
 | 1603 |         raw = buf; | 
 | 1604 |         Py_INCREF(raw); | 
 | 1605 |     } | 
 | 1606 |  | 
| Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 1607 | #ifdef MS_WINDOWS | 
 | 1608 |     /* Windows console IO is always UTF-8 encoded */ | 
 | 1609 |     if (PyWindowsConsoleIO_Check(raw)) | 
 | 1610 |         encoding = "utf-8"; | 
 | 1611 | #endif | 
 | 1612 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1613 |     text = PyUnicode_FromString(name); | 
 | 1614 |     if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) | 
 | 1615 |         goto error; | 
| Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1616 |     res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1617 |     if (res == NULL) | 
 | 1618 |         goto error; | 
 | 1619 |     isatty = PyObject_IsTrue(res); | 
 | 1620 |     Py_DECREF(res); | 
 | 1621 |     if (isatty == -1) | 
 | 1622 |         goto error; | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1623 |     if (!buffered_stdio) | 
| Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1624 |         write_through = Py_True; | 
 | 1625 |     else | 
 | 1626 |         write_through = Py_False; | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1627 |     if (isatty && buffered_stdio) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1628 |         line_buffering = Py_True; | 
 | 1629 |     else | 
 | 1630 |         line_buffering = Py_False; | 
 | 1631 |  | 
 | 1632 |     Py_CLEAR(raw); | 
 | 1633 |     Py_CLEAR(text); | 
 | 1634 |  | 
 | 1635 | #ifdef MS_WINDOWS | 
 | 1636 |     /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" | 
 | 1637 |        newlines to "\n". | 
 | 1638 |        sys.stdout and sys.stderr: translate "\n" to "\r\n". */ | 
 | 1639 |     newline = NULL; | 
 | 1640 | #else | 
 | 1641 |     /* sys.stdin: split lines at "\n". | 
 | 1642 |        sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ | 
 | 1643 |     newline = "\n"; | 
 | 1644 | #endif | 
 | 1645 |  | 
| Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1646 |     stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssOO", | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1647 |                                     buf, encoding, errors, | 
| Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1648 |                                     newline, line_buffering, write_through); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1649 |     Py_CLEAR(buf); | 
 | 1650 |     if (stream == NULL) | 
 | 1651 |         goto error; | 
 | 1652 |  | 
 | 1653 |     if (write_mode) | 
 | 1654 |         mode = "w"; | 
 | 1655 |     else | 
 | 1656 |         mode = "r"; | 
 | 1657 |     text = PyUnicode_FromString(mode); | 
 | 1658 |     if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) | 
 | 1659 |         goto error; | 
 | 1660 |     Py_CLEAR(text); | 
 | 1661 |     return stream; | 
 | 1662 |  | 
 | 1663 | error: | 
 | 1664 |     Py_XDECREF(buf); | 
 | 1665 |     Py_XDECREF(stream); | 
 | 1666 |     Py_XDECREF(text); | 
 | 1667 |     Py_XDECREF(raw); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1668 |  | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1669 |     if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { | 
 | 1670 |         /* Issue #24891: the file descriptor was closed after the first | 
 | 1671 |            is_valid_fd() check was called. Ignore the OSError and set the | 
 | 1672 |            stream to None. */ | 
 | 1673 |         PyErr_Clear(); | 
 | 1674 |         Py_RETURN_NONE; | 
 | 1675 |     } | 
 | 1676 |     return NULL; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1677 | } | 
 | 1678 |  | 
 | 1679 | /* Initialize sys.stdin, stdout, stderr and builtins.open */ | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1680 | static _PyInitError | 
| Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1681 | init_sys_streams(PyInterpreterState *interp) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1682 | { | 
 | 1683 |     PyObject *iomod = NULL, *wrapper; | 
 | 1684 |     PyObject *bimod = NULL; | 
 | 1685 |     PyObject *m; | 
 | 1686 |     PyObject *std = NULL; | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1687 |     int fd; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1688 |     PyObject * encoding_attr; | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1689 |     _PyInitError res = _Py_INIT_OK(); | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1690 |     _PyCoreConfig *config = &interp->core_config; | 
 | 1691 |  | 
 | 1692 |     char *codec_name = get_codec_name(config->stdio_encoding); | 
 | 1693 |     if (codec_name == NULL) { | 
 | 1694 |         return _Py_INIT_ERR("failed to get the Python codec name " | 
 | 1695 |                             "of the stdio encoding"); | 
 | 1696 |     } | 
 | 1697 |     PyMem_RawFree(config->stdio_encoding); | 
 | 1698 |     config->stdio_encoding = codec_name; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1699 |  | 
 | 1700 |     /* Hack to avoid a nasty recursion issue when Python is invoked | 
 | 1701 |        in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ | 
 | 1702 |     if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { | 
 | 1703 |         goto error; | 
 | 1704 |     } | 
 | 1705 |     Py_DECREF(m); | 
 | 1706 |  | 
 | 1707 |     if (!(m = PyImport_ImportModule("encodings.latin_1"))) { | 
 | 1708 |         goto error; | 
 | 1709 |     } | 
 | 1710 |     Py_DECREF(m); | 
 | 1711 |  | 
 | 1712 |     if (!(bimod = PyImport_ImportModule("builtins"))) { | 
 | 1713 |         goto error; | 
 | 1714 |     } | 
 | 1715 |  | 
 | 1716 |     if (!(iomod = PyImport_ImportModule("io"))) { | 
 | 1717 |         goto error; | 
 | 1718 |     } | 
 | 1719 |     if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { | 
 | 1720 |         goto error; | 
 | 1721 |     } | 
 | 1722 |  | 
 | 1723 |     /* Set builtins.open */ | 
 | 1724 |     if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { | 
 | 1725 |         Py_DECREF(wrapper); | 
 | 1726 |         goto error; | 
 | 1727 |     } | 
 | 1728 |     Py_DECREF(wrapper); | 
 | 1729 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1730 |     /* Set sys.stdin */ | 
 | 1731 |     fd = fileno(stdin); | 
 | 1732 |     /* Under some conditions stdin, stdout and stderr may not be connected | 
 | 1733 |      * and fileno() may point to an invalid file descriptor. For example | 
 | 1734 |      * GUI apps don't have valid standard streams by default. | 
 | 1735 |      */ | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1736 |     std = create_stdio(config, iomod, fd, 0, "<stdin>", | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1737 |                        config->stdio_encoding, | 
 | 1738 |                        config->stdio_errors); | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1739 |     if (std == NULL) | 
 | 1740 |         goto error; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1741 |     PySys_SetObject("__stdin__", std); | 
 | 1742 |     _PySys_SetObjectId(&PyId_stdin, std); | 
 | 1743 |     Py_DECREF(std); | 
 | 1744 |  | 
 | 1745 |     /* Set sys.stdout */ | 
 | 1746 |     fd = fileno(stdout); | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1747 |     std = create_stdio(config, iomod, fd, 1, "<stdout>", | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1748 |                        config->stdio_encoding, | 
 | 1749 |                        config->stdio_errors); | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1750 |     if (std == NULL) | 
 | 1751 |         goto error; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1752 |     PySys_SetObject("__stdout__", std); | 
 | 1753 |     _PySys_SetObjectId(&PyId_stdout, std); | 
 | 1754 |     Py_DECREF(std); | 
 | 1755 |  | 
 | 1756 | #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ | 
 | 1757 |     /* Set sys.stderr, replaces the preliminary stderr */ | 
 | 1758 |     fd = fileno(stderr); | 
| Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1759 |     std = create_stdio(config, iomod, fd, 1, "<stderr>", | 
| Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1760 |                        config->stdio_encoding, | 
 | 1761 |                        "backslashreplace"); | 
| Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1762 |     if (std == NULL) | 
 | 1763 |         goto error; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1764 |  | 
 | 1765 |     /* Same as hack above, pre-import stderr's codec to avoid recursion | 
 | 1766 |        when import.c tries to write to stderr in verbose mode. */ | 
 | 1767 |     encoding_attr = PyObject_GetAttrString(std, "encoding"); | 
 | 1768 |     if (encoding_attr != NULL) { | 
| Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1769 |         const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1770 |         if (std_encoding != NULL) { | 
 | 1771 |             PyObject *codec_info = _PyCodec_Lookup(std_encoding); | 
 | 1772 |             Py_XDECREF(codec_info); | 
 | 1773 |         } | 
 | 1774 |         Py_DECREF(encoding_attr); | 
 | 1775 |     } | 
 | 1776 |     PyErr_Clear();  /* Not a fatal error if codec isn't available */ | 
 | 1777 |  | 
 | 1778 |     if (PySys_SetObject("__stderr__", std) < 0) { | 
 | 1779 |         Py_DECREF(std); | 
 | 1780 |         goto error; | 
 | 1781 |     } | 
 | 1782 |     if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { | 
 | 1783 |         Py_DECREF(std); | 
 | 1784 |         goto error; | 
 | 1785 |     } | 
 | 1786 |     Py_DECREF(std); | 
 | 1787 | #endif | 
 | 1788 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1789 |     goto done; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1790 |  | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1791 | error: | 
 | 1792 |     res = _Py_INIT_ERR("can't initialize sys standard streams"); | 
 | 1793 |  | 
 | 1794 | done: | 
| Victor Stinner | 124b9eb | 2018-08-29 01:29:06 +0200 | [diff] [blame] | 1795 |     _Py_ClearStandardStreamEncoding(); | 
| Victor Stinner | 31e9908 | 2017-12-20 23:41:38 +0100 | [diff] [blame] | 1796 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1797 |     Py_XDECREF(bimod); | 
 | 1798 |     Py_XDECREF(iomod); | 
| Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1799 |     return res; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1800 | } | 
 | 1801 |  | 
 | 1802 |  | 
| Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1803 | static void | 
| Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1804 | _Py_FatalError_DumpTracebacks(int fd) | 
| Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1805 | { | 
| Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1806 |     fputc('\n', stderr); | 
 | 1807 |     fflush(stderr); | 
 | 1808 |  | 
 | 1809 |     /* display the current Python stack */ | 
| Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 1810 |     _Py_DumpTracebackThreads(fd, NULL, NULL); | 
| Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1811 | } | 
| Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1812 |  | 
 | 1813 | /* Print the current exception (if an exception is set) with its traceback, | 
 | 1814 |    or display the current Python stack. | 
 | 1815 |  | 
 | 1816 |    Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is | 
 | 1817 |    called on catastrophic cases. | 
 | 1818 |  | 
 | 1819 |    Return 1 if the traceback was displayed, 0 otherwise. */ | 
 | 1820 |  | 
 | 1821 | static int | 
 | 1822 | _Py_FatalError_PrintExc(int fd) | 
 | 1823 | { | 
 | 1824 |     PyObject *ferr, *res; | 
 | 1825 |     PyObject *exception, *v, *tb; | 
 | 1826 |     int has_tb; | 
 | 1827 |  | 
 | 1828 |     if (PyThreadState_GET() == NULL) { | 
 | 1829 |         /* The GIL is released: trying to acquire it is likely to deadlock, | 
 | 1830 |            just give up. */ | 
 | 1831 |         return 0; | 
 | 1832 |     } | 
 | 1833 |  | 
 | 1834 |     PyErr_Fetch(&exception, &v, &tb); | 
 | 1835 |     if (exception == NULL) { | 
 | 1836 |         /* No current exception */ | 
 | 1837 |         return 0; | 
 | 1838 |     } | 
 | 1839 |  | 
 | 1840 |     ferr = _PySys_GetObjectId(&PyId_stderr); | 
 | 1841 |     if (ferr == NULL || ferr == Py_None) { | 
 | 1842 |         /* sys.stderr is not set yet or set to None, | 
 | 1843 |            no need to try to display the exception */ | 
 | 1844 |         return 0; | 
 | 1845 |     } | 
 | 1846 |  | 
 | 1847 |     PyErr_NormalizeException(&exception, &v, &tb); | 
 | 1848 |     if (tb == NULL) { | 
 | 1849 |         tb = Py_None; | 
 | 1850 |         Py_INCREF(tb); | 
 | 1851 |     } | 
 | 1852 |     PyException_SetTraceback(v, tb); | 
 | 1853 |     if (exception == NULL) { | 
 | 1854 |         /* PyErr_NormalizeException() failed */ | 
 | 1855 |         return 0; | 
 | 1856 |     } | 
 | 1857 |  | 
 | 1858 |     has_tb = (tb != Py_None); | 
 | 1859 |     PyErr_Display(exception, v, tb); | 
 | 1860 |     Py_XDECREF(exception); | 
 | 1861 |     Py_XDECREF(v); | 
 | 1862 |     Py_XDECREF(tb); | 
 | 1863 |  | 
 | 1864 |     /* sys.stderr may be buffered: call sys.stderr.flush() */ | 
| Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1865 |     res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); | 
| Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1866 |     if (res == NULL) | 
 | 1867 |         PyErr_Clear(); | 
 | 1868 |     else | 
 | 1869 |         Py_DECREF(res); | 
 | 1870 |  | 
 | 1871 |     return has_tb; | 
 | 1872 | } | 
 | 1873 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1874 | /* Print fatal error message and abort */ | 
 | 1875 |  | 
| Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 1876 | #ifdef MS_WINDOWS | 
 | 1877 | static void | 
 | 1878 | fatal_output_debug(const char *msg) | 
 | 1879 | { | 
 | 1880 |     /* buffer of 256 bytes allocated on the stack */ | 
 | 1881 |     WCHAR buffer[256 / sizeof(WCHAR)]; | 
 | 1882 |     size_t buflen = Py_ARRAY_LENGTH(buffer) - 1; | 
 | 1883 |     size_t msglen; | 
 | 1884 |  | 
 | 1885 |     OutputDebugStringW(L"Fatal Python error: "); | 
 | 1886 |  | 
 | 1887 |     msglen = strlen(msg); | 
 | 1888 |     while (msglen) { | 
 | 1889 |         size_t i; | 
 | 1890 |  | 
 | 1891 |         if (buflen > msglen) { | 
 | 1892 |             buflen = msglen; | 
 | 1893 |         } | 
 | 1894 |  | 
 | 1895 |         /* Convert the message to wchar_t. This uses a simple one-to-one | 
 | 1896 |            conversion, assuming that the this error message actually uses | 
 | 1897 |            ASCII only. If this ceases to be true, we will have to convert. */ | 
 | 1898 |         for (i=0; i < buflen; ++i) { | 
 | 1899 |             buffer[i] = msg[i]; | 
 | 1900 |         } | 
 | 1901 |         buffer[i] = L'\0'; | 
 | 1902 |         OutputDebugStringW(buffer); | 
 | 1903 |  | 
 | 1904 |         msg += buflen; | 
 | 1905 |         msglen -= buflen; | 
 | 1906 |     } | 
 | 1907 |     OutputDebugStringW(L"\n"); | 
 | 1908 | } | 
 | 1909 | #endif | 
 | 1910 |  | 
| Benjamin Peterson | cef88b9 | 2017-11-25 13:02:55 -0800 | [diff] [blame] | 1911 | static void _Py_NO_RETURN | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1912 | fatal_error(const char *prefix, const char *msg, int status) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1913 | { | 
 | 1914 |     const int fd = fileno(stderr); | 
| Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 1915 |     static int reentrant = 0; | 
| Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 1916 |  | 
 | 1917 |     if (reentrant) { | 
 | 1918 |         /* Py_FatalError() caused a second fatal error. | 
 | 1919 |            Example: flush_std_files() raises a recursion error. */ | 
 | 1920 |         goto exit; | 
 | 1921 |     } | 
 | 1922 |     reentrant = 1; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1923 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1924 |     fprintf(stderr, "Fatal Python error: "); | 
 | 1925 |     if (prefix) { | 
 | 1926 |         fputs(prefix, stderr); | 
 | 1927 |         fputs(": ", stderr); | 
 | 1928 |     } | 
 | 1929 |     if (msg) { | 
 | 1930 |         fputs(msg, stderr); | 
 | 1931 |     } | 
 | 1932 |     else { | 
 | 1933 |         fprintf(stderr, "<message not set>"); | 
 | 1934 |     } | 
 | 1935 |     fputs("\n", stderr); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1936 |     fflush(stderr); /* it helps in Windows debug build */ | 
| Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1937 |  | 
| Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1938 |     /* Print the exception (if an exception is set) with its traceback, | 
 | 1939 |      * or display the current Python stack. */ | 
| Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 1940 |     if (!_Py_FatalError_PrintExc(fd)) { | 
| Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1941 |         _Py_FatalError_DumpTracebacks(fd); | 
| Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 1942 |     } | 
| Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1943 |  | 
| Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 1944 |     /* The main purpose of faulthandler is to display the traceback. | 
 | 1945 |        This function already did its best to display a traceback. | 
 | 1946 |        Disable faulthandler to prevent writing a second traceback | 
 | 1947 |        on abort(). */ | 
| Victor Stinner | 2025d78 | 2016-03-16 23:19:15 +0100 | [diff] [blame] | 1948 |     _PyFaulthandler_Fini(); | 
 | 1949 |  | 
| Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1950 |     /* Check if the current Python thread hold the GIL */ | 
 | 1951 |     if (PyThreadState_GET() != NULL) { | 
 | 1952 |         /* Flush sys.stdout and sys.stderr */ | 
 | 1953 |         flush_std_files(); | 
 | 1954 |     } | 
| Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1955 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1956 | #ifdef MS_WINDOWS | 
| Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 1957 |     fatal_output_debug(msg); | 
| Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 1958 | #endif /* MS_WINDOWS */ | 
 | 1959 |  | 
 | 1960 | exit: | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1961 |     if (status < 0) { | 
| Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 1962 | #if defined(MS_WINDOWS) && defined(_DEBUG) | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1963 |         DebugBreak(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1964 | #endif | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1965 |         abort(); | 
 | 1966 |     } | 
 | 1967 |     else { | 
 | 1968 |         exit(status); | 
 | 1969 |     } | 
 | 1970 | } | 
 | 1971 |  | 
| Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1972 | void _Py_NO_RETURN | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1973 | Py_FatalError(const char *msg) | 
 | 1974 | { | 
 | 1975 |     fatal_error(NULL, msg, -1); | 
 | 1976 | } | 
 | 1977 |  | 
| Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1978 | void _Py_NO_RETURN | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1979 | _Py_FatalInitError(_PyInitError err) | 
 | 1980 | { | 
 | 1981 |     /* On "user" error: exit with status 1. | 
 | 1982 |        For all other errors, call abort(). */ | 
 | 1983 |     int status = err.user_err ? 1 : -1; | 
 | 1984 |     fatal_error(err.prefix, err.msg, status); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1985 | } | 
 | 1986 |  | 
 | 1987 | /* Clean up and exit */ | 
 | 1988 |  | 
| Victor Stinner | d7292b5 | 2016-06-17 12:29:00 +0200 | [diff] [blame] | 1989 | #  include "pythread.h" | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1990 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1991 | /* For the atexit module. */ | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 1992 | void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1993 | { | 
| Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1994 |     PyInterpreterState *is = _PyInterpreterState_Get(); | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 1995 |  | 
| Antoine Pitrou | fc5db95 | 2017-12-13 02:29:07 +0100 | [diff] [blame] | 1996 |     /* Guard against API misuse (see bpo-17852) */ | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 1997 |     assert(is->pyexitfunc == NULL || is->pyexitfunc == func); | 
 | 1998 |  | 
 | 1999 |     is->pyexitfunc = func; | 
 | 2000 |     is->pyexitmodule = module; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2001 | } | 
 | 2002 |  | 
 | 2003 | static void | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 2004 | call_py_exitfuncs(PyInterpreterState *istate) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2005 | { | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 2006 |     if (istate->pyexitfunc == NULL) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2007 |         return; | 
 | 2008 |  | 
| Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 2009 |     (*istate->pyexitfunc)(istate->pyexitmodule); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2010 |     PyErr_Clear(); | 
 | 2011 | } | 
 | 2012 |  | 
 | 2013 | /* Wait until threading._shutdown completes, provided | 
 | 2014 |    the threading module was imported in the first place. | 
 | 2015 |    The shutdown routine will wait until all non-daemon | 
 | 2016 |    "threading" threads have completed. */ | 
 | 2017 | static void | 
 | 2018 | wait_for_thread_shutdown(void) | 
 | 2019 | { | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2020 |     _Py_IDENTIFIER(_shutdown); | 
 | 2021 |     PyObject *result; | 
| Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 2022 |     PyObject *threading = _PyImport_GetModuleId(&PyId_threading); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2023 |     if (threading == NULL) { | 
 | 2024 |         /* threading not imported */ | 
 | 2025 |         PyErr_Clear(); | 
 | 2026 |         return; | 
 | 2027 |     } | 
| Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2028 |     result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2029 |     if (result == NULL) { | 
 | 2030 |         PyErr_WriteUnraisable(threading); | 
 | 2031 |     } | 
 | 2032 |     else { | 
 | 2033 |         Py_DECREF(result); | 
 | 2034 |     } | 
 | 2035 |     Py_DECREF(threading); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2036 | } | 
 | 2037 |  | 
 | 2038 | #define NEXITFUNCS 32 | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2039 | int Py_AtExit(void (*func)(void)) | 
 | 2040 | { | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2041 |     if (_PyRuntime.nexitfuncs >= NEXITFUNCS) | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2042 |         return -1; | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2043 |     _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func; | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2044 |     return 0; | 
 | 2045 | } | 
 | 2046 |  | 
 | 2047 | static void | 
 | 2048 | call_ll_exitfuncs(void) | 
 | 2049 | { | 
| Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2050 |     while (_PyRuntime.nexitfuncs > 0) | 
 | 2051 |         (*_PyRuntime.exitfuncs[--_PyRuntime.nexitfuncs])(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2052 |  | 
 | 2053 |     fflush(stdout); | 
 | 2054 |     fflush(stderr); | 
 | 2055 | } | 
 | 2056 |  | 
| Victor Stinner | cfc8831 | 2018-08-01 16:41:25 +0200 | [diff] [blame] | 2057 | void _Py_NO_RETURN | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2058 | Py_Exit(int sts) | 
 | 2059 | { | 
| Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 2060 |     if (Py_FinalizeEx() < 0) { | 
 | 2061 |         sts = 120; | 
 | 2062 |     } | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2063 |  | 
 | 2064 |     exit(sts); | 
 | 2065 | } | 
 | 2066 |  | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2067 | static _PyInitError | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2068 | initsigs(void) | 
 | 2069 | { | 
 | 2070 | #ifdef SIGPIPE | 
 | 2071 |     PyOS_setsig(SIGPIPE, SIG_IGN); | 
 | 2072 | #endif | 
 | 2073 | #ifdef SIGXFZ | 
 | 2074 |     PyOS_setsig(SIGXFZ, SIG_IGN); | 
 | 2075 | #endif | 
 | 2076 | #ifdef SIGXFSZ | 
 | 2077 |     PyOS_setsig(SIGXFSZ, SIG_IGN); | 
 | 2078 | #endif | 
 | 2079 |     PyOS_InitInterrupts(); /* May imply initsignal() */ | 
 | 2080 |     if (PyErr_Occurred()) { | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2081 |         return _Py_INIT_ERR("can't import signal"); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2082 |     } | 
| Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2083 |     return _Py_INIT_OK(); | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2084 | } | 
 | 2085 |  | 
 | 2086 |  | 
 | 2087 | /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. | 
 | 2088 |  * | 
 | 2089 |  * All of the code in this function must only use async-signal-safe functions, | 
 | 2090 |  * listed at `man 7 signal` or | 
 | 2091 |  * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. | 
 | 2092 |  */ | 
 | 2093 | void | 
 | 2094 | _Py_RestoreSignals(void) | 
 | 2095 | { | 
 | 2096 | #ifdef SIGPIPE | 
 | 2097 |     PyOS_setsig(SIGPIPE, SIG_DFL); | 
 | 2098 | #endif | 
 | 2099 | #ifdef SIGXFZ | 
 | 2100 |     PyOS_setsig(SIGXFZ, SIG_DFL); | 
 | 2101 | #endif | 
 | 2102 | #ifdef SIGXFSZ | 
 | 2103 |     PyOS_setsig(SIGXFSZ, SIG_DFL); | 
 | 2104 | #endif | 
 | 2105 | } | 
 | 2106 |  | 
 | 2107 |  | 
 | 2108 | /* | 
 | 2109 |  * The file descriptor fd is considered ``interactive'' if either | 
 | 2110 |  *   a) isatty(fd) is TRUE, or | 
 | 2111 |  *   b) the -i flag was given, and the filename associated with | 
 | 2112 |  *      the descriptor is NULL or "<stdin>" or "???". | 
 | 2113 |  */ | 
 | 2114 | int | 
 | 2115 | Py_FdIsInteractive(FILE *fp, const char *filename) | 
 | 2116 | { | 
 | 2117 |     if (isatty((int)fileno(fp))) | 
 | 2118 |         return 1; | 
 | 2119 |     if (!Py_InteractiveFlag) | 
 | 2120 |         return 0; | 
 | 2121 |     return (filename == NULL) || | 
 | 2122 |            (strcmp(filename, "<stdin>") == 0) || | 
 | 2123 |            (strcmp(filename, "???") == 0); | 
 | 2124 | } | 
 | 2125 |  | 
 | 2126 |  | 
| Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2127 | /* Wrappers around sigaction() or signal(). */ | 
 | 2128 |  | 
 | 2129 | PyOS_sighandler_t | 
 | 2130 | PyOS_getsig(int sig) | 
 | 2131 | { | 
 | 2132 | #ifdef HAVE_SIGACTION | 
 | 2133 |     struct sigaction context; | 
 | 2134 |     if (sigaction(sig, NULL, &context) == -1) | 
 | 2135 |         return SIG_ERR; | 
 | 2136 |     return context.sa_handler; | 
 | 2137 | #else | 
 | 2138 |     PyOS_sighandler_t handler; | 
 | 2139 | /* Special signal handling for the secure CRT in Visual Studio 2005 */ | 
 | 2140 | #if defined(_MSC_VER) && _MSC_VER >= 1400 | 
 | 2141 |     switch (sig) { | 
 | 2142 |     /* Only these signals are valid */ | 
 | 2143 |     case SIGINT: | 
 | 2144 |     case SIGILL: | 
 | 2145 |     case SIGFPE: | 
 | 2146 |     case SIGSEGV: | 
 | 2147 |     case SIGTERM: | 
 | 2148 |     case SIGBREAK: | 
 | 2149 |     case SIGABRT: | 
 | 2150 |         break; | 
 | 2151 |     /* Don't call signal() with other values or it will assert */ | 
 | 2152 |     default: | 
 | 2153 |         return SIG_ERR; | 
 | 2154 |     } | 
 | 2155 | #endif /* _MSC_VER && _MSC_VER >= 1400 */ | 
 | 2156 |     handler = signal(sig, SIG_IGN); | 
 | 2157 |     if (handler != SIG_ERR) | 
 | 2158 |         signal(sig, handler); | 
 | 2159 |     return handler; | 
 | 2160 | #endif | 
 | 2161 | } | 
 | 2162 |  | 
 | 2163 | /* | 
 | 2164 |  * All of the code in this function must only use async-signal-safe functions, | 
 | 2165 |  * listed at `man 7 signal` or | 
 | 2166 |  * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. | 
 | 2167 |  */ | 
 | 2168 | PyOS_sighandler_t | 
 | 2169 | PyOS_setsig(int sig, PyOS_sighandler_t handler) | 
 | 2170 | { | 
 | 2171 | #ifdef HAVE_SIGACTION | 
 | 2172 |     /* Some code in Modules/signalmodule.c depends on sigaction() being | 
 | 2173 |      * used here if HAVE_SIGACTION is defined.  Fix that if this code | 
 | 2174 |      * changes to invalidate that assumption. | 
 | 2175 |      */ | 
 | 2176 |     struct sigaction context, ocontext; | 
 | 2177 |     context.sa_handler = handler; | 
 | 2178 |     sigemptyset(&context.sa_mask); | 
 | 2179 |     context.sa_flags = 0; | 
 | 2180 |     if (sigaction(sig, &context, &ocontext) == -1) | 
 | 2181 |         return SIG_ERR; | 
 | 2182 |     return ocontext.sa_handler; | 
 | 2183 | #else | 
 | 2184 |     PyOS_sighandler_t oldhandler; | 
 | 2185 |     oldhandler = signal(sig, handler); | 
 | 2186 | #ifdef HAVE_SIGINTERRUPT | 
 | 2187 |     siginterrupt(sig, 1); | 
 | 2188 | #endif | 
 | 2189 |     return oldhandler; | 
 | 2190 | #endif | 
 | 2191 | } | 
 | 2192 |  | 
 | 2193 | #ifdef __cplusplus | 
 | 2194 | } | 
 | 2195 | #endif |