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