| 1 | n/a | /* Python interpreter top-level routines, including init/exit */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "Python.h" |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | #include "Python-ast.h" |
|---|
| 6 | n/a | #undef Yield /* undefine macro conflicting with winbase.h */ |
|---|
| 7 | n/a | #include "grammar.h" |
|---|
| 8 | n/a | #include "node.h" |
|---|
| 9 | n/a | #include "token.h" |
|---|
| 10 | n/a | #include "parsetok.h" |
|---|
| 11 | n/a | #include "errcode.h" |
|---|
| 12 | n/a | #include "code.h" |
|---|
| 13 | n/a | #include "symtable.h" |
|---|
| 14 | n/a | #include "ast.h" |
|---|
| 15 | n/a | #include "marshal.h" |
|---|
| 16 | n/a | #include "osdefs.h" |
|---|
| 17 | n/a | #include <locale.h> |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | #ifdef HAVE_SIGNAL_H |
|---|
| 20 | n/a | #include <signal.h> |
|---|
| 21 | n/a | #endif |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | #ifdef MS_WINDOWS |
|---|
| 24 | n/a | #include "malloc.h" /* for alloca */ |
|---|
| 25 | n/a | #endif |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | #ifdef HAVE_LANGINFO_H |
|---|
| 28 | n/a | #include <langinfo.h> |
|---|
| 29 | n/a | #endif |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | #ifdef MS_WINDOWS |
|---|
| 32 | n/a | #undef BYTE |
|---|
| 33 | n/a | #include "windows.h" |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | extern PyTypeObject PyWindowsConsoleIO_Type; |
|---|
| 36 | n/a | #define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type)) |
|---|
| 37 | n/a | #endif |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | _Py_IDENTIFIER(flush); |
|---|
| 40 | n/a | _Py_IDENTIFIER(name); |
|---|
| 41 | n/a | _Py_IDENTIFIER(stdin); |
|---|
| 42 | n/a | _Py_IDENTIFIER(stdout); |
|---|
| 43 | n/a | _Py_IDENTIFIER(stderr); |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | #ifdef __cplusplus |
|---|
| 46 | n/a | extern "C" { |
|---|
| 47 | n/a | #endif |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | extern wchar_t *Py_GetPath(void); |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | extern grammar _PyParser_Grammar; /* From graminit.c */ |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | /* Forward */ |
|---|
| 54 | n/a | static void initmain(PyInterpreterState *interp); |
|---|
| 55 | n/a | static int initfsencoding(PyInterpreterState *interp); |
|---|
| 56 | n/a | static void initsite(void); |
|---|
| 57 | n/a | static int initstdio(void); |
|---|
| 58 | n/a | static void initsigs(void); |
|---|
| 59 | n/a | static void call_py_exitfuncs(void); |
|---|
| 60 | n/a | static void wait_for_thread_shutdown(void); |
|---|
| 61 | n/a | static void call_ll_exitfuncs(void); |
|---|
| 62 | n/a | extern int _PyUnicode_Init(void); |
|---|
| 63 | n/a | extern int _PyStructSequence_Init(void); |
|---|
| 64 | n/a | extern void _PyUnicode_Fini(void); |
|---|
| 65 | n/a | extern int _PyLong_Init(void); |
|---|
| 66 | n/a | extern void PyLong_Fini(void); |
|---|
| 67 | n/a | extern int _PyFaulthandler_Init(void); |
|---|
| 68 | n/a | extern void _PyFaulthandler_Fini(void); |
|---|
| 69 | n/a | extern void _PyHash_Fini(void); |
|---|
| 70 | n/a | extern int _PyTraceMalloc_Init(void); |
|---|
| 71 | n/a | extern int _PyTraceMalloc_Fini(void); |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | #ifdef WITH_THREAD |
|---|
| 74 | n/a | extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); |
|---|
| 75 | n/a | extern void _PyGILState_Fini(void); |
|---|
| 76 | n/a | #endif /* WITH_THREAD */ |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | /* Global configuration variable declarations are in pydebug.h */ |
|---|
| 79 | n/a | /* XXX (ncoghlan): move those declarations to pylifecycle.h? */ |
|---|
| 80 | n/a | int Py_DebugFlag; /* Needed by parser.c */ |
|---|
| 81 | n/a | int Py_VerboseFlag; /* Needed by import.c */ |
|---|
| 82 | n/a | int Py_QuietFlag; /* Needed by sysmodule.c */ |
|---|
| 83 | n/a | int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ |
|---|
| 84 | n/a | int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */ |
|---|
| 85 | n/a | int Py_OptimizeFlag = 0; /* Needed by compile.c */ |
|---|
| 86 | n/a | int Py_NoSiteFlag; /* Suppress 'import site' */ |
|---|
| 87 | n/a | int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */ |
|---|
| 88 | n/a | int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */ |
|---|
| 89 | n/a | int Py_FrozenFlag; /* Needed by getpath.c */ |
|---|
| 90 | n/a | int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ |
|---|
| 91 | n/a | int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */ |
|---|
| 92 | n/a | int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ |
|---|
| 93 | n/a | int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ |
|---|
| 94 | n/a | int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */ |
|---|
| 95 | n/a | int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */ |
|---|
| 96 | n/a | #ifdef MS_WINDOWS |
|---|
| 97 | n/a | int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */ |
|---|
| 98 | n/a | int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */ |
|---|
| 99 | n/a | #endif |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | PyThreadState *_Py_Finalizing = NULL; |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | /* Hack to force loading of object files */ |
|---|
| 104 | n/a | int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ |
|---|
| 105 | n/a | PyOS_mystrnicmp; /* Python/pystrcmp.o */ |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | /* PyModule_GetWarningsModule is no longer necessary as of 2.6 |
|---|
| 108 | n/a | since _warnings is builtin. This API should not be used. */ |
|---|
| 109 | n/a | PyObject * |
|---|
| 110 | n/a | PyModule_GetWarningsModule(void) |
|---|
| 111 | n/a | { |
|---|
| 112 | n/a | return PyImport_ImportModule("warnings"); |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | static int initialized = 0; |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | /* API to access the initialized flag -- useful for esoteric use */ |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | int |
|---|
| 120 | n/a | Py_IsInitialized(void) |
|---|
| 121 | n/a | { |
|---|
| 122 | n/a | return initialized; |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | /* Helper to allow an embedding application to override the normal |
|---|
| 126 | n/a | * mechanism that attempts to figure out an appropriate IO encoding |
|---|
| 127 | n/a | */ |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | static char *_Py_StandardStreamEncoding = NULL; |
|---|
| 130 | n/a | static char *_Py_StandardStreamErrors = NULL; |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | int |
|---|
| 133 | n/a | Py_SetStandardStreamEncoding(const char *encoding, const char *errors) |
|---|
| 134 | n/a | { |
|---|
| 135 | n/a | if (Py_IsInitialized()) { |
|---|
| 136 | n/a | /* This is too late to have any effect */ |
|---|
| 137 | n/a | return -1; |
|---|
| 138 | n/a | } |
|---|
| 139 | n/a | /* Can't call PyErr_NoMemory() on errors, as Python hasn't been |
|---|
| 140 | n/a | * initialised yet. |
|---|
| 141 | n/a | * |
|---|
| 142 | n/a | * However, the raw memory allocators are initialised appropriately |
|---|
| 143 | n/a | * as C static variables, so _PyMem_RawStrdup is OK even though |
|---|
| 144 | n/a | * Py_Initialize hasn't been called yet. |
|---|
| 145 | n/a | */ |
|---|
| 146 | n/a | if (encoding) { |
|---|
| 147 | n/a | _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding); |
|---|
| 148 | n/a | if (!_Py_StandardStreamEncoding) { |
|---|
| 149 | n/a | return -2; |
|---|
| 150 | n/a | } |
|---|
| 151 | n/a | } |
|---|
| 152 | n/a | if (errors) { |
|---|
| 153 | n/a | _Py_StandardStreamErrors = _PyMem_RawStrdup(errors); |
|---|
| 154 | n/a | if (!_Py_StandardStreamErrors) { |
|---|
| 155 | n/a | if (_Py_StandardStreamEncoding) { |
|---|
| 156 | n/a | PyMem_RawFree(_Py_StandardStreamEncoding); |
|---|
| 157 | n/a | } |
|---|
| 158 | n/a | return -3; |
|---|
| 159 | n/a | } |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | #ifdef MS_WINDOWS |
|---|
| 162 | n/a | if (_Py_StandardStreamEncoding) { |
|---|
| 163 | n/a | /* Overriding the stream encoding implies legacy streams */ |
|---|
| 164 | n/a | Py_LegacyWindowsStdioFlag = 1; |
|---|
| 165 | n/a | } |
|---|
| 166 | n/a | #endif |
|---|
| 167 | n/a | return 0; |
|---|
| 168 | n/a | } |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | /* Global initializations. Can be undone by Py_FinalizeEx(). Don't |
|---|
| 171 | n/a | call this twice without an intervening Py_FinalizeEx() call. When |
|---|
| 172 | n/a | initializations fail, a fatal error is issued and the function does |
|---|
| 173 | n/a | not return. On return, the first thread and interpreter state have |
|---|
| 174 | n/a | been created. |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | Locking: you must hold the interpreter lock while calling this. |
|---|
| 177 | n/a | (If the lock has not yet been initialized, that's equivalent to |
|---|
| 178 | n/a | having the lock, but you cannot use multiple threads.) |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | */ |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | static int |
|---|
| 183 | n/a | add_flag(int flag, const char *envs) |
|---|
| 184 | n/a | { |
|---|
| 185 | n/a | int env = atoi(envs); |
|---|
| 186 | n/a | if (flag < env) |
|---|
| 187 | n/a | flag = env; |
|---|
| 188 | n/a | if (flag < 1) |
|---|
| 189 | n/a | flag = 1; |
|---|
| 190 | n/a | return flag; |
|---|
| 191 | n/a | } |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | static char* |
|---|
| 194 | n/a | get_codec_name(const char *encoding) |
|---|
| 195 | n/a | { |
|---|
| 196 | n/a | const char *name_utf8; |
|---|
| 197 | n/a | char *name_str; |
|---|
| 198 | n/a | PyObject *codec, *name = NULL; |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | codec = _PyCodec_Lookup(encoding); |
|---|
| 201 | n/a | if (!codec) |
|---|
| 202 | n/a | goto error; |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | name = _PyObject_GetAttrId(codec, &PyId_name); |
|---|
| 205 | n/a | Py_CLEAR(codec); |
|---|
| 206 | n/a | if (!name) |
|---|
| 207 | n/a | goto error; |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | name_utf8 = PyUnicode_AsUTF8(name); |
|---|
| 210 | n/a | if (name_utf8 == NULL) |
|---|
| 211 | n/a | goto error; |
|---|
| 212 | n/a | name_str = _PyMem_RawStrdup(name_utf8); |
|---|
| 213 | n/a | Py_DECREF(name); |
|---|
| 214 | n/a | if (name_str == NULL) { |
|---|
| 215 | n/a | PyErr_NoMemory(); |
|---|
| 216 | n/a | return NULL; |
|---|
| 217 | n/a | } |
|---|
| 218 | n/a | return name_str; |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | error: |
|---|
| 221 | n/a | Py_XDECREF(codec); |
|---|
| 222 | n/a | Py_XDECREF(name); |
|---|
| 223 | n/a | return NULL; |
|---|
| 224 | n/a | } |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | static char* |
|---|
| 227 | n/a | get_locale_encoding(void) |
|---|
| 228 | n/a | { |
|---|
| 229 | n/a | #ifdef MS_WINDOWS |
|---|
| 230 | n/a | char codepage[100]; |
|---|
| 231 | n/a | PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP()); |
|---|
| 232 | n/a | return get_codec_name(codepage); |
|---|
| 233 | n/a | #elif defined(HAVE_LANGINFO_H) && defined(CODESET) |
|---|
| 234 | n/a | char* codeset = nl_langinfo(CODESET); |
|---|
| 235 | n/a | if (!codeset || codeset[0] == '\0') { |
|---|
| 236 | n/a | PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); |
|---|
| 237 | n/a | return NULL; |
|---|
| 238 | n/a | } |
|---|
| 239 | n/a | return get_codec_name(codeset); |
|---|
| 240 | n/a | #elif defined(__ANDROID__) |
|---|
| 241 | n/a | return get_codec_name("UTF-8"); |
|---|
| 242 | n/a | #else |
|---|
| 243 | n/a | PyErr_SetNone(PyExc_NotImplementedError); |
|---|
| 244 | n/a | return NULL; |
|---|
| 245 | n/a | #endif |
|---|
| 246 | n/a | } |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | static void |
|---|
| 249 | n/a | import_init(PyInterpreterState *interp, PyObject *sysmod) |
|---|
| 250 | n/a | { |
|---|
| 251 | n/a | PyObject *importlib; |
|---|
| 252 | n/a | PyObject *impmod; |
|---|
| 253 | n/a | PyObject *sys_modules; |
|---|
| 254 | n/a | PyObject *value; |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | /* Import _importlib through its frozen version, _frozen_importlib. */ |
|---|
| 257 | n/a | if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { |
|---|
| 258 | n/a | Py_FatalError("Py_Initialize: can't import _frozen_importlib"); |
|---|
| 259 | n/a | } |
|---|
| 260 | n/a | else if (Py_VerboseFlag) { |
|---|
| 261 | n/a | PySys_FormatStderr("import _frozen_importlib # frozen\n"); |
|---|
| 262 | n/a | } |
|---|
| 263 | n/a | importlib = PyImport_AddModule("_frozen_importlib"); |
|---|
| 264 | n/a | if (importlib == NULL) { |
|---|
| 265 | n/a | Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from " |
|---|
| 266 | n/a | "sys.modules"); |
|---|
| 267 | n/a | } |
|---|
| 268 | n/a | interp->importlib = importlib; |
|---|
| 269 | n/a | Py_INCREF(interp->importlib); |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | interp->import_func = PyDict_GetItemString(interp->builtins, "__import__"); |
|---|
| 272 | n/a | if (interp->import_func == NULL) |
|---|
| 273 | n/a | Py_FatalError("Py_Initialize: __import__ not found"); |
|---|
| 274 | n/a | Py_INCREF(interp->import_func); |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | /* Import the _imp module */ |
|---|
| 277 | n/a | impmod = PyInit_imp(); |
|---|
| 278 | n/a | if (impmod == NULL) { |
|---|
| 279 | n/a | Py_FatalError("Py_Initialize: can't import _imp"); |
|---|
| 280 | n/a | } |
|---|
| 281 | n/a | else if (Py_VerboseFlag) { |
|---|
| 282 | n/a | PySys_FormatStderr("import _imp # builtin\n"); |
|---|
| 283 | n/a | } |
|---|
| 284 | n/a | sys_modules = PyImport_GetModuleDict(); |
|---|
| 285 | n/a | if (Py_VerboseFlag) { |
|---|
| 286 | n/a | PySys_FormatStderr("import sys # builtin\n"); |
|---|
| 287 | n/a | } |
|---|
| 288 | n/a | if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) { |
|---|
| 289 | n/a | Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); |
|---|
| 290 | n/a | } |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | /* Install importlib as the implementation of import */ |
|---|
| 293 | n/a | value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); |
|---|
| 294 | n/a | if (value == NULL) { |
|---|
| 295 | n/a | PyErr_Print(); |
|---|
| 296 | n/a | Py_FatalError("Py_Initialize: importlib install failed"); |
|---|
| 297 | n/a | } |
|---|
| 298 | n/a | Py_DECREF(value); |
|---|
| 299 | n/a | Py_DECREF(impmod); |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | _PyImportZip_Init(); |
|---|
| 302 | n/a | } |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | void |
|---|
| 306 | n/a | _Py_InitializeEx_Private(int install_sigs, int install_importlib) |
|---|
| 307 | n/a | { |
|---|
| 308 | n/a | PyInterpreterState *interp; |
|---|
| 309 | n/a | PyThreadState *tstate; |
|---|
| 310 | n/a | PyObject *bimod, *sysmod, *pstderr; |
|---|
| 311 | n/a | char *p; |
|---|
| 312 | n/a | extern void _Py_ReadyTypes(void); |
|---|
| 313 | n/a | |
|---|
| 314 | n/a | if (initialized) |
|---|
| 315 | n/a | return; |
|---|
| 316 | n/a | initialized = 1; |
|---|
| 317 | n/a | _Py_Finalizing = NULL; |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | #ifdef HAVE_SETLOCALE |
|---|
| 320 | n/a | /* Set up the LC_CTYPE locale, so we can obtain |
|---|
| 321 | n/a | the locale's charset without having to switch |
|---|
| 322 | n/a | locales. */ |
|---|
| 323 | n/a | setlocale(LC_CTYPE, ""); |
|---|
| 324 | n/a | #endif |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') |
|---|
| 327 | n/a | Py_DebugFlag = add_flag(Py_DebugFlag, p); |
|---|
| 328 | n/a | if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') |
|---|
| 329 | n/a | Py_VerboseFlag = add_flag(Py_VerboseFlag, p); |
|---|
| 330 | n/a | if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') |
|---|
| 331 | n/a | Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); |
|---|
| 332 | n/a | if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') |
|---|
| 333 | n/a | Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); |
|---|
| 334 | n/a | /* The variable is only tested for existence here; _PyRandom_Init will |
|---|
| 335 | n/a | check its value further. */ |
|---|
| 336 | n/a | if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0') |
|---|
| 337 | n/a | Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p); |
|---|
| 338 | n/a | #ifdef MS_WINDOWS |
|---|
| 339 | n/a | if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0') |
|---|
| 340 | n/a | Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p); |
|---|
| 341 | n/a | if ((p = Py_GETENV("PYTHONLEGACYWINDOWSSTDIO")) && *p != '\0') |
|---|
| 342 | n/a | Py_LegacyWindowsStdioFlag = add_flag(Py_LegacyWindowsStdioFlag, p); |
|---|
| 343 | n/a | #endif |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | _PyRandom_Init(); |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | interp = PyInterpreterState_New(); |
|---|
| 348 | n/a | if (interp == NULL) |
|---|
| 349 | n/a | Py_FatalError("Py_Initialize: can't make first interpreter"); |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | tstate = PyThreadState_New(interp); |
|---|
| 352 | n/a | if (tstate == NULL) |
|---|
| 353 | n/a | Py_FatalError("Py_Initialize: can't make first thread"); |
|---|
| 354 | n/a | (void) PyThreadState_Swap(tstate); |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | #ifdef WITH_THREAD |
|---|
| 357 | n/a | /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because |
|---|
| 358 | n/a | destroying the GIL might fail when it is being referenced from |
|---|
| 359 | n/a | another running thread (see issue #9901). |
|---|
| 360 | n/a | Instead we destroy the previously created GIL here, which ensures |
|---|
| 361 | n/a | that we can call Py_Initialize / Py_FinalizeEx multiple times. */ |
|---|
| 362 | n/a | _PyEval_FiniThreads(); |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | /* Auto-thread-state API */ |
|---|
| 365 | n/a | _PyGILState_Init(interp, tstate); |
|---|
| 366 | n/a | #endif /* WITH_THREAD */ |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | _Py_ReadyTypes(); |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | if (!_PyFrame_Init()) |
|---|
| 371 | n/a | Py_FatalError("Py_Initialize: can't init frames"); |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | if (!_PyLong_Init()) |
|---|
| 374 | n/a | Py_FatalError("Py_Initialize: can't init longs"); |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | if (!PyByteArray_Init()) |
|---|
| 377 | n/a | Py_FatalError("Py_Initialize: can't init bytearray"); |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | if (!_PyFloat_Init()) |
|---|
| 380 | n/a | Py_FatalError("Py_Initialize: can't init float"); |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | interp->modules = PyDict_New(); |
|---|
| 383 | n/a | if (interp->modules == NULL) |
|---|
| 384 | n/a | Py_FatalError("Py_Initialize: can't make modules dictionary"); |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | /* Init Unicode implementation; relies on the codec registry */ |
|---|
| 387 | n/a | if (_PyUnicode_Init() < 0) |
|---|
| 388 | n/a | Py_FatalError("Py_Initialize: can't initialize unicode"); |
|---|
| 389 | n/a | if (_PyStructSequence_Init() < 0) |
|---|
| 390 | n/a | Py_FatalError("Py_Initialize: can't initialize structseq"); |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | bimod = _PyBuiltin_Init(); |
|---|
| 393 | n/a | if (bimod == NULL) |
|---|
| 394 | n/a | Py_FatalError("Py_Initialize: can't initialize builtins modules"); |
|---|
| 395 | n/a | _PyImport_FixupBuiltin(bimod, "builtins"); |
|---|
| 396 | n/a | interp->builtins = PyModule_GetDict(bimod); |
|---|
| 397 | n/a | if (interp->builtins == NULL) |
|---|
| 398 | n/a | Py_FatalError("Py_Initialize: can't initialize builtins dict"); |
|---|
| 399 | n/a | Py_INCREF(interp->builtins); |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | /* initialize builtin exceptions */ |
|---|
| 402 | n/a | _PyExc_Init(bimod); |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | sysmod = _PySys_Init(); |
|---|
| 405 | n/a | if (sysmod == NULL) |
|---|
| 406 | n/a | Py_FatalError("Py_Initialize: can't initialize sys"); |
|---|
| 407 | n/a | interp->sysdict = PyModule_GetDict(sysmod); |
|---|
| 408 | n/a | if (interp->sysdict == NULL) |
|---|
| 409 | n/a | Py_FatalError("Py_Initialize: can't initialize sys dict"); |
|---|
| 410 | n/a | Py_INCREF(interp->sysdict); |
|---|
| 411 | n/a | _PyImport_FixupBuiltin(sysmod, "sys"); |
|---|
| 412 | n/a | PySys_SetPath(Py_GetPath()); |
|---|
| 413 | n/a | PyDict_SetItemString(interp->sysdict, "modules", |
|---|
| 414 | n/a | interp->modules); |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | /* Set up a preliminary stderr printer until we have enough |
|---|
| 417 | n/a | infrastructure for the io module in place. */ |
|---|
| 418 | n/a | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
|---|
| 419 | n/a | if (pstderr == NULL) |
|---|
| 420 | n/a | Py_FatalError("Py_Initialize: can't set preliminary stderr"); |
|---|
| 421 | n/a | _PySys_SetObjectId(&PyId_stderr, pstderr); |
|---|
| 422 | n/a | PySys_SetObject("__stderr__", pstderr); |
|---|
| 423 | n/a | Py_DECREF(pstderr); |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | _PyImport_Init(); |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | _PyImportHooks_Init(); |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | /* Initialize _warnings. */ |
|---|
| 430 | n/a | _PyWarnings_Init(); |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | if (!install_importlib) |
|---|
| 433 | n/a | return; |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | if (_PyTime_Init() < 0) |
|---|
| 436 | n/a | Py_FatalError("Py_Initialize: can't initialize time"); |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | import_init(interp, sysmod); |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | /* initialize the faulthandler module */ |
|---|
| 441 | n/a | if (_PyFaulthandler_Init()) |
|---|
| 442 | n/a | Py_FatalError("Py_Initialize: can't initialize faulthandler"); |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | if (initfsencoding(interp) < 0) |
|---|
| 445 | n/a | Py_FatalError("Py_Initialize: unable to load the file system codec"); |
|---|
| 446 | n/a | |
|---|
| 447 | n/a | if (install_sigs) |
|---|
| 448 | n/a | initsigs(); /* Signal handling stuff, including initintr() */ |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | if (_PyTraceMalloc_Init() < 0) |
|---|
| 451 | n/a | Py_FatalError("Py_Initialize: can't initialize tracemalloc"); |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | initmain(interp); /* Module __main__ */ |
|---|
| 454 | n/a | if (initstdio() < 0) |
|---|
| 455 | n/a | Py_FatalError( |
|---|
| 456 | n/a | "Py_Initialize: can't initialize sys standard streams"); |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | /* Initialize warnings. */ |
|---|
| 459 | n/a | if (PySys_HasWarnOptions()) { |
|---|
| 460 | n/a | PyObject *warnings_module = PyImport_ImportModule("warnings"); |
|---|
| 461 | n/a | if (warnings_module == NULL) { |
|---|
| 462 | n/a | fprintf(stderr, "'import warnings' failed; traceback:\n"); |
|---|
| 463 | n/a | PyErr_Print(); |
|---|
| 464 | n/a | } |
|---|
| 465 | n/a | Py_XDECREF(warnings_module); |
|---|
| 466 | n/a | } |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | if (!Py_NoSiteFlag) |
|---|
| 469 | n/a | initsite(); /* Module site */ |
|---|
| 470 | n/a | } |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | void |
|---|
| 473 | n/a | Py_InitializeEx(int install_sigs) |
|---|
| 474 | n/a | { |
|---|
| 475 | n/a | _Py_InitializeEx_Private(install_sigs, 1); |
|---|
| 476 | n/a | } |
|---|
| 477 | n/a | |
|---|
| 478 | n/a | void |
|---|
| 479 | n/a | Py_Initialize(void) |
|---|
| 480 | n/a | { |
|---|
| 481 | n/a | Py_InitializeEx(1); |
|---|
| 482 | n/a | } |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | |
|---|
| 485 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 486 | n/a | extern void dump_counts(FILE*); |
|---|
| 487 | n/a | #endif |
|---|
| 488 | n/a | |
|---|
| 489 | n/a | /* Flush stdout and stderr */ |
|---|
| 490 | n/a | |
|---|
| 491 | n/a | static int |
|---|
| 492 | n/a | file_is_closed(PyObject *fobj) |
|---|
| 493 | n/a | { |
|---|
| 494 | n/a | int r; |
|---|
| 495 | n/a | PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); |
|---|
| 496 | n/a | if (tmp == NULL) { |
|---|
| 497 | n/a | PyErr_Clear(); |
|---|
| 498 | n/a | return 0; |
|---|
| 499 | n/a | } |
|---|
| 500 | n/a | r = PyObject_IsTrue(tmp); |
|---|
| 501 | n/a | Py_DECREF(tmp); |
|---|
| 502 | n/a | if (r < 0) |
|---|
| 503 | n/a | PyErr_Clear(); |
|---|
| 504 | n/a | return r > 0; |
|---|
| 505 | n/a | } |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | static int |
|---|
| 508 | n/a | flush_std_files(void) |
|---|
| 509 | n/a | { |
|---|
| 510 | n/a | PyObject *fout = _PySys_GetObjectId(&PyId_stdout); |
|---|
| 511 | n/a | PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); |
|---|
| 512 | n/a | PyObject *tmp; |
|---|
| 513 | n/a | int status = 0; |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { |
|---|
| 516 | n/a | tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL); |
|---|
| 517 | n/a | if (tmp == NULL) { |
|---|
| 518 | n/a | PyErr_WriteUnraisable(fout); |
|---|
| 519 | n/a | status = -1; |
|---|
| 520 | n/a | } |
|---|
| 521 | n/a | else |
|---|
| 522 | n/a | Py_DECREF(tmp); |
|---|
| 523 | n/a | } |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { |
|---|
| 526 | n/a | tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); |
|---|
| 527 | n/a | if (tmp == NULL) { |
|---|
| 528 | n/a | PyErr_Clear(); |
|---|
| 529 | n/a | status = -1; |
|---|
| 530 | n/a | } |
|---|
| 531 | n/a | else |
|---|
| 532 | n/a | Py_DECREF(tmp); |
|---|
| 533 | n/a | } |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | return status; |
|---|
| 536 | n/a | } |
|---|
| 537 | n/a | |
|---|
| 538 | n/a | /* Undo the effect of Py_Initialize(). |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | Beware: if multiple interpreter and/or thread states exist, these |
|---|
| 541 | n/a | are not wiped out; only the current thread and interpreter state |
|---|
| 542 | n/a | are deleted. But since everything else is deleted, those other |
|---|
| 543 | n/a | interpreter and thread states should no longer be used. |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | (XXX We should do better, e.g. wipe out all interpreters and |
|---|
| 546 | n/a | threads.) |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | Locking: as above. |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | */ |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | int |
|---|
| 553 | n/a | Py_FinalizeEx(void) |
|---|
| 554 | n/a | { |
|---|
| 555 | n/a | PyInterpreterState *interp; |
|---|
| 556 | n/a | PyThreadState *tstate; |
|---|
| 557 | n/a | int status = 0; |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | if (!initialized) |
|---|
| 560 | n/a | return status; |
|---|
| 561 | n/a | |
|---|
| 562 | n/a | wait_for_thread_shutdown(); |
|---|
| 563 | n/a | |
|---|
| 564 | n/a | /* The interpreter is still entirely intact at this point, and the |
|---|
| 565 | n/a | * exit funcs may be relying on that. In particular, if some thread |
|---|
| 566 | n/a | * or exit func is still waiting to do an import, the import machinery |
|---|
| 567 | n/a | * expects Py_IsInitialized() to return true. So don't say the |
|---|
| 568 | n/a | * interpreter is uninitialized until after the exit funcs have run. |
|---|
| 569 | n/a | * Note that Threading.py uses an exit func to do a join on all the |
|---|
| 570 | n/a | * threads created thru it, so this also protects pending imports in |
|---|
| 571 | n/a | * the threads created via Threading. |
|---|
| 572 | n/a | */ |
|---|
| 573 | n/a | call_py_exitfuncs(); |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | /* Get current thread state and interpreter pointer */ |
|---|
| 576 | n/a | tstate = PyThreadState_GET(); |
|---|
| 577 | n/a | interp = tstate->interp; |
|---|
| 578 | n/a | |
|---|
| 579 | n/a | /* Remaining threads (e.g. daemon threads) will automatically exit |
|---|
| 580 | n/a | after taking the GIL (in PyEval_RestoreThread()). */ |
|---|
| 581 | n/a | _Py_Finalizing = tstate; |
|---|
| 582 | n/a | initialized = 0; |
|---|
| 583 | n/a | |
|---|
| 584 | n/a | /* Flush sys.stdout and sys.stderr */ |
|---|
| 585 | n/a | if (flush_std_files() < 0) { |
|---|
| 586 | n/a | status = -1; |
|---|
| 587 | n/a | } |
|---|
| 588 | n/a | |
|---|
| 589 | n/a | /* Disable signal handling */ |
|---|
| 590 | n/a | PyOS_FiniInterrupts(); |
|---|
| 591 | n/a | |
|---|
| 592 | n/a | /* Collect garbage. This may call finalizers; it's nice to call these |
|---|
| 593 | n/a | * before all modules are destroyed. |
|---|
| 594 | n/a | * XXX If a __del__ or weakref callback is triggered here, and tries to |
|---|
| 595 | n/a | * XXX import a module, bad things can happen, because Python no |
|---|
| 596 | n/a | * XXX longer believes it's initialized. |
|---|
| 597 | n/a | * XXX Fatal Python error: Interpreter not initialized (version mismatch?) |
|---|
| 598 | n/a | * XXX is easy to provoke that way. I've also seen, e.g., |
|---|
| 599 | n/a | * XXX Exception exceptions.ImportError: 'No module named sha' |
|---|
| 600 | n/a | * XXX in <function callback at 0x008F5718> ignored |
|---|
| 601 | n/a | * XXX but I'm unclear on exactly how that one happens. In any case, |
|---|
| 602 | n/a | * XXX I haven't seen a real-life report of either of these. |
|---|
| 603 | n/a | */ |
|---|
| 604 | n/a | _PyGC_CollectIfEnabled(); |
|---|
| 605 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 606 | n/a | /* With COUNT_ALLOCS, it helps to run GC multiple times: |
|---|
| 607 | n/a | each collection might release some types from the type |
|---|
| 608 | n/a | list, so they become garbage. */ |
|---|
| 609 | n/a | while (_PyGC_CollectIfEnabled() > 0) |
|---|
| 610 | n/a | /* nothing */; |
|---|
| 611 | n/a | #endif |
|---|
| 612 | n/a | /* Destroy all modules */ |
|---|
| 613 | n/a | PyImport_Cleanup(); |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ |
|---|
| 616 | n/a | if (flush_std_files() < 0) { |
|---|
| 617 | n/a | status = -1; |
|---|
| 618 | n/a | } |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | /* Collect final garbage. This disposes of cycles created by |
|---|
| 621 | n/a | * class definitions, for example. |
|---|
| 622 | n/a | * XXX This is disabled because it caused too many problems. If |
|---|
| 623 | n/a | * XXX a __del__ or weakref callback triggers here, Python code has |
|---|
| 624 | n/a | * XXX a hard time running, because even the sys module has been |
|---|
| 625 | n/a | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). |
|---|
| 626 | n/a | * XXX One symptom is a sequence of information-free messages |
|---|
| 627 | n/a | * XXX coming from threads (if a __del__ or callback is invoked, |
|---|
| 628 | n/a | * XXX other threads can execute too, and any exception they encounter |
|---|
| 629 | n/a | * XXX triggers a comedy of errors as subsystem after subsystem |
|---|
| 630 | n/a | * XXX fails to find what it *expects* to find in sys to help report |
|---|
| 631 | n/a | * XXX the exception and consequent unexpected failures). I've also |
|---|
| 632 | n/a | * XXX seen segfaults then, after adding print statements to the |
|---|
| 633 | n/a | * XXX Python code getting called. |
|---|
| 634 | n/a | */ |
|---|
| 635 | n/a | #if 0 |
|---|
| 636 | n/a | _PyGC_CollectIfEnabled(); |
|---|
| 637 | n/a | #endif |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | /* Disable tracemalloc after all Python objects have been destroyed, |
|---|
| 640 | n/a | so it is possible to use tracemalloc in objects destructor. */ |
|---|
| 641 | n/a | _PyTraceMalloc_Fini(); |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
|---|
| 644 | n/a | _PyImport_Fini(); |
|---|
| 645 | n/a | |
|---|
| 646 | n/a | /* Cleanup typeobject.c's internal caches. */ |
|---|
| 647 | n/a | _PyType_Fini(); |
|---|
| 648 | n/a | |
|---|
| 649 | n/a | /* unload faulthandler module */ |
|---|
| 650 | n/a | _PyFaulthandler_Fini(); |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | /* Debugging stuff */ |
|---|
| 653 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 654 | n/a | dump_counts(stderr); |
|---|
| 655 | n/a | #endif |
|---|
| 656 | n/a | /* dump hash stats */ |
|---|
| 657 | n/a | _PyHash_Fini(); |
|---|
| 658 | n/a | |
|---|
| 659 | n/a | _PY_DEBUG_PRINT_TOTAL_REFS(); |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | #ifdef Py_TRACE_REFS |
|---|
| 662 | n/a | /* Display all objects still alive -- this can invoke arbitrary |
|---|
| 663 | n/a | * __repr__ overrides, so requires a mostly-intact interpreter. |
|---|
| 664 | n/a | * Alas, a lot of stuff may still be alive now that will be cleaned |
|---|
| 665 | n/a | * up later. |
|---|
| 666 | n/a | */ |
|---|
| 667 | n/a | if (Py_GETENV("PYTHONDUMPREFS")) |
|---|
| 668 | n/a | _Py_PrintReferences(stderr); |
|---|
| 669 | n/a | #endif /* Py_TRACE_REFS */ |
|---|
| 670 | n/a | |
|---|
| 671 | n/a | /* Clear interpreter state and all thread states. */ |
|---|
| 672 | n/a | PyInterpreterState_Clear(interp); |
|---|
| 673 | n/a | |
|---|
| 674 | n/a | /* Now we decref the exception classes. After this point nothing |
|---|
| 675 | n/a | can raise an exception. That's okay, because each Fini() method |
|---|
| 676 | n/a | below has been checked to make sure no exceptions are ever |
|---|
| 677 | n/a | raised. |
|---|
| 678 | n/a | */ |
|---|
| 679 | n/a | |
|---|
| 680 | n/a | _PyExc_Fini(); |
|---|
| 681 | n/a | |
|---|
| 682 | n/a | /* Sundry finalizers */ |
|---|
| 683 | n/a | PyMethod_Fini(); |
|---|
| 684 | n/a | PyFrame_Fini(); |
|---|
| 685 | n/a | PyCFunction_Fini(); |
|---|
| 686 | n/a | PyTuple_Fini(); |
|---|
| 687 | n/a | PyList_Fini(); |
|---|
| 688 | n/a | PySet_Fini(); |
|---|
| 689 | n/a | PyBytes_Fini(); |
|---|
| 690 | n/a | PyByteArray_Fini(); |
|---|
| 691 | n/a | PyLong_Fini(); |
|---|
| 692 | n/a | PyFloat_Fini(); |
|---|
| 693 | n/a | PyDict_Fini(); |
|---|
| 694 | n/a | PySlice_Fini(); |
|---|
| 695 | n/a | _PyGC_Fini(); |
|---|
| 696 | n/a | _PyRandom_Fini(); |
|---|
| 697 | n/a | _PyArg_Fini(); |
|---|
| 698 | n/a | PyAsyncGen_Fini(); |
|---|
| 699 | n/a | |
|---|
| 700 | n/a | /* Cleanup Unicode implementation */ |
|---|
| 701 | n/a | _PyUnicode_Fini(); |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | /* reset file system default encoding */ |
|---|
| 704 | n/a | if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { |
|---|
| 705 | n/a | PyMem_RawFree((char*)Py_FileSystemDefaultEncoding); |
|---|
| 706 | n/a | Py_FileSystemDefaultEncoding = NULL; |
|---|
| 707 | n/a | } |
|---|
| 708 | n/a | |
|---|
| 709 | n/a | /* XXX Still allocated: |
|---|
| 710 | n/a | - various static ad-hoc pointers to interned strings |
|---|
| 711 | n/a | - int and float free list blocks |
|---|
| 712 | n/a | - whatever various modules and libraries allocate |
|---|
| 713 | n/a | */ |
|---|
| 714 | n/a | |
|---|
| 715 | n/a | PyGrammar_RemoveAccelerators(&_PyParser_Grammar); |
|---|
| 716 | n/a | |
|---|
| 717 | n/a | /* Cleanup auto-thread-state */ |
|---|
| 718 | n/a | #ifdef WITH_THREAD |
|---|
| 719 | n/a | _PyGILState_Fini(); |
|---|
| 720 | n/a | #endif /* WITH_THREAD */ |
|---|
| 721 | n/a | |
|---|
| 722 | n/a | /* Delete current thread. After this, many C API calls become crashy. */ |
|---|
| 723 | n/a | PyThreadState_Swap(NULL); |
|---|
| 724 | n/a | |
|---|
| 725 | n/a | PyInterpreterState_Delete(interp); |
|---|
| 726 | n/a | |
|---|
| 727 | n/a | #ifdef Py_TRACE_REFS |
|---|
| 728 | n/a | /* Display addresses (& refcnts) of all objects still alive. |
|---|
| 729 | n/a | * An address can be used to find the repr of the object, printed |
|---|
| 730 | n/a | * above by _Py_PrintReferences. |
|---|
| 731 | n/a | */ |
|---|
| 732 | n/a | if (Py_GETENV("PYTHONDUMPREFS")) |
|---|
| 733 | n/a | _Py_PrintReferenceAddresses(stderr); |
|---|
| 734 | n/a | #endif /* Py_TRACE_REFS */ |
|---|
| 735 | n/a | #ifdef WITH_PYMALLOC |
|---|
| 736 | n/a | if (_PyMem_PymallocEnabled()) { |
|---|
| 737 | n/a | char *opt = Py_GETENV("PYTHONMALLOCSTATS"); |
|---|
| 738 | n/a | if (opt != NULL && *opt != '\0') |
|---|
| 739 | n/a | _PyObject_DebugMallocStats(stderr); |
|---|
| 740 | n/a | } |
|---|
| 741 | n/a | #endif |
|---|
| 742 | n/a | |
|---|
| 743 | n/a | call_ll_exitfuncs(); |
|---|
| 744 | n/a | return status; |
|---|
| 745 | n/a | } |
|---|
| 746 | n/a | |
|---|
| 747 | n/a | void |
|---|
| 748 | n/a | Py_Finalize(void) |
|---|
| 749 | n/a | { |
|---|
| 750 | n/a | Py_FinalizeEx(); |
|---|
| 751 | n/a | } |
|---|
| 752 | n/a | |
|---|
| 753 | n/a | /* Create and initialize a new interpreter and thread, and return the |
|---|
| 754 | n/a | new thread. This requires that Py_Initialize() has been called |
|---|
| 755 | n/a | first. |
|---|
| 756 | n/a | |
|---|
| 757 | n/a | Unsuccessful initialization yields a NULL pointer. Note that *no* |
|---|
| 758 | n/a | exception information is available even in this case -- the |
|---|
| 759 | n/a | exception information is held in the thread, and there is no |
|---|
| 760 | n/a | thread. |
|---|
| 761 | n/a | |
|---|
| 762 | n/a | Locking: as above. |
|---|
| 763 | n/a | |
|---|
| 764 | n/a | */ |
|---|
| 765 | n/a | |
|---|
| 766 | n/a | PyThreadState * |
|---|
| 767 | n/a | Py_NewInterpreter(void) |
|---|
| 768 | n/a | { |
|---|
| 769 | n/a | PyInterpreterState *interp; |
|---|
| 770 | n/a | PyThreadState *tstate, *save_tstate; |
|---|
| 771 | n/a | PyObject *bimod, *sysmod; |
|---|
| 772 | n/a | |
|---|
| 773 | n/a | if (!initialized) |
|---|
| 774 | n/a | Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); |
|---|
| 775 | n/a | |
|---|
| 776 | n/a | #ifdef WITH_THREAD |
|---|
| 777 | n/a | /* Issue #10915, #15751: The GIL API doesn't work with multiple |
|---|
| 778 | n/a | interpreters: disable PyGILState_Check(). */ |
|---|
| 779 | n/a | _PyGILState_check_enabled = 0; |
|---|
| 780 | n/a | #endif |
|---|
| 781 | n/a | |
|---|
| 782 | n/a | interp = PyInterpreterState_New(); |
|---|
| 783 | n/a | if (interp == NULL) |
|---|
| 784 | n/a | return NULL; |
|---|
| 785 | n/a | |
|---|
| 786 | n/a | tstate = PyThreadState_New(interp); |
|---|
| 787 | n/a | if (tstate == NULL) { |
|---|
| 788 | n/a | PyInterpreterState_Delete(interp); |
|---|
| 789 | n/a | return NULL; |
|---|
| 790 | n/a | } |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | save_tstate = PyThreadState_Swap(tstate); |
|---|
| 793 | n/a | |
|---|
| 794 | n/a | /* XXX The following is lax in error checking */ |
|---|
| 795 | n/a | |
|---|
| 796 | n/a | interp->modules = PyDict_New(); |
|---|
| 797 | n/a | |
|---|
| 798 | n/a | bimod = _PyImport_FindBuiltin("builtins"); |
|---|
| 799 | n/a | if (bimod != NULL) { |
|---|
| 800 | n/a | interp->builtins = PyModule_GetDict(bimod); |
|---|
| 801 | n/a | if (interp->builtins == NULL) |
|---|
| 802 | n/a | goto handle_error; |
|---|
| 803 | n/a | Py_INCREF(interp->builtins); |
|---|
| 804 | n/a | } |
|---|
| 805 | n/a | |
|---|
| 806 | n/a | /* initialize builtin exceptions */ |
|---|
| 807 | n/a | _PyExc_Init(bimod); |
|---|
| 808 | n/a | |
|---|
| 809 | n/a | sysmod = _PyImport_FindBuiltin("sys"); |
|---|
| 810 | n/a | if (bimod != NULL && sysmod != NULL) { |
|---|
| 811 | n/a | PyObject *pstderr; |
|---|
| 812 | n/a | |
|---|
| 813 | n/a | interp->sysdict = PyModule_GetDict(sysmod); |
|---|
| 814 | n/a | if (interp->sysdict == NULL) |
|---|
| 815 | n/a | goto handle_error; |
|---|
| 816 | n/a | Py_INCREF(interp->sysdict); |
|---|
| 817 | n/a | PySys_SetPath(Py_GetPath()); |
|---|
| 818 | n/a | PyDict_SetItemString(interp->sysdict, "modules", |
|---|
| 819 | n/a | interp->modules); |
|---|
| 820 | n/a | /* Set up a preliminary stderr printer until we have enough |
|---|
| 821 | n/a | infrastructure for the io module in place. */ |
|---|
| 822 | n/a | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
|---|
| 823 | n/a | if (pstderr == NULL) |
|---|
| 824 | n/a | Py_FatalError("Py_Initialize: can't set preliminary stderr"); |
|---|
| 825 | n/a | _PySys_SetObjectId(&PyId_stderr, pstderr); |
|---|
| 826 | n/a | PySys_SetObject("__stderr__", pstderr); |
|---|
| 827 | n/a | Py_DECREF(pstderr); |
|---|
| 828 | n/a | |
|---|
| 829 | n/a | _PyImportHooks_Init(); |
|---|
| 830 | n/a | |
|---|
| 831 | n/a | import_init(interp, sysmod); |
|---|
| 832 | n/a | |
|---|
| 833 | n/a | if (initfsencoding(interp) < 0) |
|---|
| 834 | n/a | goto handle_error; |
|---|
| 835 | n/a | |
|---|
| 836 | n/a | if (initstdio() < 0) |
|---|
| 837 | n/a | Py_FatalError( |
|---|
| 838 | n/a | "Py_Initialize: can't initialize sys standard streams"); |
|---|
| 839 | n/a | initmain(interp); |
|---|
| 840 | n/a | if (!Py_NoSiteFlag) |
|---|
| 841 | n/a | initsite(); |
|---|
| 842 | n/a | } |
|---|
| 843 | n/a | |
|---|
| 844 | n/a | if (!PyErr_Occurred()) |
|---|
| 845 | n/a | return tstate; |
|---|
| 846 | n/a | |
|---|
| 847 | n/a | handle_error: |
|---|
| 848 | n/a | /* Oops, it didn't work. Undo it all. */ |
|---|
| 849 | n/a | |
|---|
| 850 | n/a | PyErr_PrintEx(0); |
|---|
| 851 | n/a | PyThreadState_Clear(tstate); |
|---|
| 852 | n/a | PyThreadState_Swap(save_tstate); |
|---|
| 853 | n/a | PyThreadState_Delete(tstate); |
|---|
| 854 | n/a | PyInterpreterState_Delete(interp); |
|---|
| 855 | n/a | |
|---|
| 856 | n/a | return NULL; |
|---|
| 857 | n/a | } |
|---|
| 858 | n/a | |
|---|
| 859 | n/a | /* Delete an interpreter and its last thread. This requires that the |
|---|
| 860 | n/a | given thread state is current, that the thread has no remaining |
|---|
| 861 | n/a | frames, and that it is its interpreter's only remaining thread. |
|---|
| 862 | n/a | It is a fatal error to violate these constraints. |
|---|
| 863 | n/a | |
|---|
| 864 | n/a | (Py_FinalizeEx() doesn't have these constraints -- it zaps |
|---|
| 865 | n/a | everything, regardless.) |
|---|
| 866 | n/a | |
|---|
| 867 | n/a | Locking: as above. |
|---|
| 868 | n/a | |
|---|
| 869 | n/a | */ |
|---|
| 870 | n/a | |
|---|
| 871 | n/a | void |
|---|
| 872 | n/a | Py_EndInterpreter(PyThreadState *tstate) |
|---|
| 873 | n/a | { |
|---|
| 874 | n/a | PyInterpreterState *interp = tstate->interp; |
|---|
| 875 | n/a | |
|---|
| 876 | n/a | if (tstate != PyThreadState_GET()) |
|---|
| 877 | n/a | Py_FatalError("Py_EndInterpreter: thread is not current"); |
|---|
| 878 | n/a | if (tstate->frame != NULL) |
|---|
| 879 | n/a | Py_FatalError("Py_EndInterpreter: thread still has a frame"); |
|---|
| 880 | n/a | |
|---|
| 881 | n/a | wait_for_thread_shutdown(); |
|---|
| 882 | n/a | |
|---|
| 883 | n/a | if (tstate != interp->tstate_head || tstate->next != NULL) |
|---|
| 884 | n/a | Py_FatalError("Py_EndInterpreter: not the last thread"); |
|---|
| 885 | n/a | |
|---|
| 886 | n/a | PyImport_Cleanup(); |
|---|
| 887 | n/a | PyInterpreterState_Clear(interp); |
|---|
| 888 | n/a | PyThreadState_Swap(NULL); |
|---|
| 889 | n/a | PyInterpreterState_Delete(interp); |
|---|
| 890 | n/a | } |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | #ifdef MS_WINDOWS |
|---|
| 893 | n/a | static wchar_t *progname = L"python"; |
|---|
| 894 | n/a | #else |
|---|
| 895 | n/a | static wchar_t *progname = L"python3"; |
|---|
| 896 | n/a | #endif |
|---|
| 897 | n/a | |
|---|
| 898 | n/a | void |
|---|
| 899 | n/a | Py_SetProgramName(wchar_t *pn) |
|---|
| 900 | n/a | { |
|---|
| 901 | n/a | if (pn && *pn) |
|---|
| 902 | n/a | progname = pn; |
|---|
| 903 | n/a | } |
|---|
| 904 | n/a | |
|---|
| 905 | n/a | wchar_t * |
|---|
| 906 | n/a | Py_GetProgramName(void) |
|---|
| 907 | n/a | { |
|---|
| 908 | n/a | return progname; |
|---|
| 909 | n/a | } |
|---|
| 910 | n/a | |
|---|
| 911 | n/a | static wchar_t *default_home = NULL; |
|---|
| 912 | n/a | static wchar_t env_home[MAXPATHLEN+1]; |
|---|
| 913 | n/a | |
|---|
| 914 | n/a | void |
|---|
| 915 | n/a | Py_SetPythonHome(wchar_t *home) |
|---|
| 916 | n/a | { |
|---|
| 917 | n/a | default_home = home; |
|---|
| 918 | n/a | } |
|---|
| 919 | n/a | |
|---|
| 920 | n/a | wchar_t * |
|---|
| 921 | n/a | Py_GetPythonHome(void) |
|---|
| 922 | n/a | { |
|---|
| 923 | n/a | wchar_t *home = default_home; |
|---|
| 924 | n/a | if (home == NULL && !Py_IgnoreEnvironmentFlag) { |
|---|
| 925 | n/a | char* chome = Py_GETENV("PYTHONHOME"); |
|---|
| 926 | n/a | if (chome) { |
|---|
| 927 | n/a | size_t size = Py_ARRAY_LENGTH(env_home); |
|---|
| 928 | n/a | size_t r = mbstowcs(env_home, chome, size); |
|---|
| 929 | n/a | if (r != (size_t)-1 && r < size) |
|---|
| 930 | n/a | home = env_home; |
|---|
| 931 | n/a | } |
|---|
| 932 | n/a | |
|---|
| 933 | n/a | } |
|---|
| 934 | n/a | return home; |
|---|
| 935 | n/a | } |
|---|
| 936 | n/a | |
|---|
| 937 | n/a | /* Create __main__ module */ |
|---|
| 938 | n/a | |
|---|
| 939 | n/a | static void |
|---|
| 940 | n/a | initmain(PyInterpreterState *interp) |
|---|
| 941 | n/a | { |
|---|
| 942 | n/a | PyObject *m, *d, *loader, *ann_dict; |
|---|
| 943 | n/a | m = PyImport_AddModule("__main__"); |
|---|
| 944 | n/a | if (m == NULL) |
|---|
| 945 | n/a | Py_FatalError("can't create __main__ module"); |
|---|
| 946 | n/a | d = PyModule_GetDict(m); |
|---|
| 947 | n/a | ann_dict = PyDict_New(); |
|---|
| 948 | n/a | if ((ann_dict == NULL) || |
|---|
| 949 | n/a | (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { |
|---|
| 950 | n/a | Py_FatalError("Failed to initialize __main__.__annotations__"); |
|---|
| 951 | n/a | } |
|---|
| 952 | n/a | Py_DECREF(ann_dict); |
|---|
| 953 | n/a | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
|---|
| 954 | n/a | PyObject *bimod = PyImport_ImportModule("builtins"); |
|---|
| 955 | n/a | if (bimod == NULL) { |
|---|
| 956 | n/a | Py_FatalError("Failed to retrieve builtins module"); |
|---|
| 957 | n/a | } |
|---|
| 958 | n/a | if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { |
|---|
| 959 | n/a | Py_FatalError("Failed to initialize __main__.__builtins__"); |
|---|
| 960 | n/a | } |
|---|
| 961 | n/a | Py_DECREF(bimod); |
|---|
| 962 | n/a | } |
|---|
| 963 | n/a | /* Main is a little special - imp.is_builtin("__main__") will return |
|---|
| 964 | n/a | * False, but BuiltinImporter is still the most appropriate initial |
|---|
| 965 | n/a | * setting for its __loader__ attribute. A more suitable value will |
|---|
| 966 | n/a | * be set if __main__ gets further initialized later in the startup |
|---|
| 967 | n/a | * process. |
|---|
| 968 | n/a | */ |
|---|
| 969 | n/a | loader = PyDict_GetItemString(d, "__loader__"); |
|---|
| 970 | n/a | if (loader == NULL || loader == Py_None) { |
|---|
| 971 | n/a | PyObject *loader = PyObject_GetAttrString(interp->importlib, |
|---|
| 972 | n/a | "BuiltinImporter"); |
|---|
| 973 | n/a | if (loader == NULL) { |
|---|
| 974 | n/a | Py_FatalError("Failed to retrieve BuiltinImporter"); |
|---|
| 975 | n/a | } |
|---|
| 976 | n/a | if (PyDict_SetItemString(d, "__loader__", loader) < 0) { |
|---|
| 977 | n/a | Py_FatalError("Failed to initialize __main__.__loader__"); |
|---|
| 978 | n/a | } |
|---|
| 979 | n/a | Py_DECREF(loader); |
|---|
| 980 | n/a | } |
|---|
| 981 | n/a | } |
|---|
| 982 | n/a | |
|---|
| 983 | n/a | static int |
|---|
| 984 | n/a | initfsencoding(PyInterpreterState *interp) |
|---|
| 985 | n/a | { |
|---|
| 986 | n/a | PyObject *codec; |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | #ifdef MS_WINDOWS |
|---|
| 989 | n/a | if (Py_LegacyWindowsFSEncodingFlag) |
|---|
| 990 | n/a | { |
|---|
| 991 | n/a | Py_FileSystemDefaultEncoding = "mbcs"; |
|---|
| 992 | n/a | Py_FileSystemDefaultEncodeErrors = "replace"; |
|---|
| 993 | n/a | } |
|---|
| 994 | n/a | else |
|---|
| 995 | n/a | { |
|---|
| 996 | n/a | Py_FileSystemDefaultEncoding = "utf-8"; |
|---|
| 997 | n/a | Py_FileSystemDefaultEncodeErrors = "surrogatepass"; |
|---|
| 998 | n/a | } |
|---|
| 999 | n/a | #else |
|---|
| 1000 | n/a | if (Py_FileSystemDefaultEncoding == NULL) |
|---|
| 1001 | n/a | { |
|---|
| 1002 | n/a | Py_FileSystemDefaultEncoding = get_locale_encoding(); |
|---|
| 1003 | n/a | if (Py_FileSystemDefaultEncoding == NULL) |
|---|
| 1004 | n/a | Py_FatalError("Py_Initialize: Unable to get the locale encoding"); |
|---|
| 1005 | n/a | |
|---|
| 1006 | n/a | Py_HasFileSystemDefaultEncoding = 0; |
|---|
| 1007 | n/a | interp->fscodec_initialized = 1; |
|---|
| 1008 | n/a | return 0; |
|---|
| 1009 | n/a | } |
|---|
| 1010 | n/a | #endif |
|---|
| 1011 | n/a | |
|---|
| 1012 | n/a | /* the encoding is mbcs, utf-8 or ascii */ |
|---|
| 1013 | n/a | codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); |
|---|
| 1014 | n/a | if (!codec) { |
|---|
| 1015 | n/a | /* Such error can only occurs in critical situations: no more |
|---|
| 1016 | n/a | * memory, import a module of the standard library failed, |
|---|
| 1017 | n/a | * etc. */ |
|---|
| 1018 | n/a | return -1; |
|---|
| 1019 | n/a | } |
|---|
| 1020 | n/a | Py_DECREF(codec); |
|---|
| 1021 | n/a | interp->fscodec_initialized = 1; |
|---|
| 1022 | n/a | return 0; |
|---|
| 1023 | n/a | } |
|---|
| 1024 | n/a | |
|---|
| 1025 | n/a | /* Import the site module (not into __main__ though) */ |
|---|
| 1026 | n/a | |
|---|
| 1027 | n/a | static void |
|---|
| 1028 | n/a | initsite(void) |
|---|
| 1029 | n/a | { |
|---|
| 1030 | n/a | PyObject *m; |
|---|
| 1031 | n/a | m = PyImport_ImportModule("site"); |
|---|
| 1032 | n/a | if (m == NULL) { |
|---|
| 1033 | n/a | fprintf(stderr, "Failed to import the site module\n"); |
|---|
| 1034 | n/a | PyErr_Print(); |
|---|
| 1035 | n/a | Py_Finalize(); |
|---|
| 1036 | n/a | exit(1); |
|---|
| 1037 | n/a | } |
|---|
| 1038 | n/a | else { |
|---|
| 1039 | n/a | Py_DECREF(m); |
|---|
| 1040 | n/a | } |
|---|
| 1041 | n/a | } |
|---|
| 1042 | n/a | |
|---|
| 1043 | n/a | /* Check if a file descriptor is valid or not. |
|---|
| 1044 | n/a | Return 0 if the file descriptor is invalid, return non-zero otherwise. */ |
|---|
| 1045 | n/a | static int |
|---|
| 1046 | n/a | is_valid_fd(int fd) |
|---|
| 1047 | n/a | { |
|---|
| 1048 | n/a | int fd2; |
|---|
| 1049 | n/a | if (fd < 0) |
|---|
| 1050 | n/a | return 0; |
|---|
| 1051 | n/a | _Py_BEGIN_SUPPRESS_IPH |
|---|
| 1052 | n/a | /* Prefer dup() over fstat(). fstat() can require input/output whereas |
|---|
| 1053 | n/a | dup() doesn't, there is a low risk of EMFILE/ENFILE at Python |
|---|
| 1054 | n/a | startup. */ |
|---|
| 1055 | n/a | fd2 = dup(fd); |
|---|
| 1056 | n/a | if (fd2 >= 0) |
|---|
| 1057 | n/a | close(fd2); |
|---|
| 1058 | n/a | _Py_END_SUPPRESS_IPH |
|---|
| 1059 | n/a | return fd2 >= 0; |
|---|
| 1060 | n/a | } |
|---|
| 1061 | n/a | |
|---|
| 1062 | n/a | /* returns Py_None if the fd is not valid */ |
|---|
| 1063 | n/a | static PyObject* |
|---|
| 1064 | n/a | create_stdio(PyObject* io, |
|---|
| 1065 | n/a | int fd, int write_mode, const char* name, |
|---|
| 1066 | n/a | const char* encoding, const char* errors) |
|---|
| 1067 | n/a | { |
|---|
| 1068 | n/a | PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; |
|---|
| 1069 | n/a | const char* mode; |
|---|
| 1070 | n/a | const char* newline; |
|---|
| 1071 | n/a | PyObject *line_buffering; |
|---|
| 1072 | n/a | int buffering, isatty; |
|---|
| 1073 | n/a | _Py_IDENTIFIER(open); |
|---|
| 1074 | n/a | _Py_IDENTIFIER(isatty); |
|---|
| 1075 | n/a | _Py_IDENTIFIER(TextIOWrapper); |
|---|
| 1076 | n/a | _Py_IDENTIFIER(mode); |
|---|
| 1077 | n/a | |
|---|
| 1078 | n/a | if (!is_valid_fd(fd)) |
|---|
| 1079 | n/a | Py_RETURN_NONE; |
|---|
| 1080 | n/a | |
|---|
| 1081 | n/a | /* stdin is always opened in buffered mode, first because it shouldn't |
|---|
| 1082 | n/a | make a difference in common use cases, second because TextIOWrapper |
|---|
| 1083 | n/a | depends on the presence of a read1() method which only exists on |
|---|
| 1084 | n/a | buffered streams. |
|---|
| 1085 | n/a | */ |
|---|
| 1086 | n/a | if (Py_UnbufferedStdioFlag && write_mode) |
|---|
| 1087 | n/a | buffering = 0; |
|---|
| 1088 | n/a | else |
|---|
| 1089 | n/a | buffering = -1; |
|---|
| 1090 | n/a | if (write_mode) |
|---|
| 1091 | n/a | mode = "wb"; |
|---|
| 1092 | n/a | else |
|---|
| 1093 | n/a | mode = "rb"; |
|---|
| 1094 | n/a | buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi", |
|---|
| 1095 | n/a | fd, mode, buffering, |
|---|
| 1096 | n/a | Py_None, Py_None, /* encoding, errors */ |
|---|
| 1097 | n/a | Py_None, 0); /* newline, closefd */ |
|---|
| 1098 | n/a | if (buf == NULL) |
|---|
| 1099 | n/a | goto error; |
|---|
| 1100 | n/a | |
|---|
| 1101 | n/a | if (buffering) { |
|---|
| 1102 | n/a | _Py_IDENTIFIER(raw); |
|---|
| 1103 | n/a | raw = _PyObject_GetAttrId(buf, &PyId_raw); |
|---|
| 1104 | n/a | if (raw == NULL) |
|---|
| 1105 | n/a | goto error; |
|---|
| 1106 | n/a | } |
|---|
| 1107 | n/a | else { |
|---|
| 1108 | n/a | raw = buf; |
|---|
| 1109 | n/a | Py_INCREF(raw); |
|---|
| 1110 | n/a | } |
|---|
| 1111 | n/a | |
|---|
| 1112 | n/a | #ifdef MS_WINDOWS |
|---|
| 1113 | n/a | /* Windows console IO is always UTF-8 encoded */ |
|---|
| 1114 | n/a | if (PyWindowsConsoleIO_Check(raw)) |
|---|
| 1115 | n/a | encoding = "utf-8"; |
|---|
| 1116 | n/a | #endif |
|---|
| 1117 | n/a | |
|---|
| 1118 | n/a | text = PyUnicode_FromString(name); |
|---|
| 1119 | n/a | if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) |
|---|
| 1120 | n/a | goto error; |
|---|
| 1121 | n/a | res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); |
|---|
| 1122 | n/a | if (res == NULL) |
|---|
| 1123 | n/a | goto error; |
|---|
| 1124 | n/a | isatty = PyObject_IsTrue(res); |
|---|
| 1125 | n/a | Py_DECREF(res); |
|---|
| 1126 | n/a | if (isatty == -1) |
|---|
| 1127 | n/a | goto error; |
|---|
| 1128 | n/a | if (isatty || Py_UnbufferedStdioFlag) |
|---|
| 1129 | n/a | line_buffering = Py_True; |
|---|
| 1130 | n/a | else |
|---|
| 1131 | n/a | line_buffering = Py_False; |
|---|
| 1132 | n/a | |
|---|
| 1133 | n/a | Py_CLEAR(raw); |
|---|
| 1134 | n/a | Py_CLEAR(text); |
|---|
| 1135 | n/a | |
|---|
| 1136 | n/a | #ifdef MS_WINDOWS |
|---|
| 1137 | n/a | /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" |
|---|
| 1138 | n/a | newlines to "\n". |
|---|
| 1139 | n/a | sys.stdout and sys.stderr: translate "\n" to "\r\n". */ |
|---|
| 1140 | n/a | newline = NULL; |
|---|
| 1141 | n/a | #else |
|---|
| 1142 | n/a | /* sys.stdin: split lines at "\n". |
|---|
| 1143 | n/a | sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ |
|---|
| 1144 | n/a | newline = "\n"; |
|---|
| 1145 | n/a | #endif |
|---|
| 1146 | n/a | |
|---|
| 1147 | n/a | stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO", |
|---|
| 1148 | n/a | buf, encoding, errors, |
|---|
| 1149 | n/a | newline, line_buffering); |
|---|
| 1150 | n/a | Py_CLEAR(buf); |
|---|
| 1151 | n/a | if (stream == NULL) |
|---|
| 1152 | n/a | goto error; |
|---|
| 1153 | n/a | |
|---|
| 1154 | n/a | if (write_mode) |
|---|
| 1155 | n/a | mode = "w"; |
|---|
| 1156 | n/a | else |
|---|
| 1157 | n/a | mode = "r"; |
|---|
| 1158 | n/a | text = PyUnicode_FromString(mode); |
|---|
| 1159 | n/a | if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) |
|---|
| 1160 | n/a | goto error; |
|---|
| 1161 | n/a | Py_CLEAR(text); |
|---|
| 1162 | n/a | return stream; |
|---|
| 1163 | n/a | |
|---|
| 1164 | n/a | error: |
|---|
| 1165 | n/a | Py_XDECREF(buf); |
|---|
| 1166 | n/a | Py_XDECREF(stream); |
|---|
| 1167 | n/a | Py_XDECREF(text); |
|---|
| 1168 | n/a | Py_XDECREF(raw); |
|---|
| 1169 | n/a | |
|---|
| 1170 | n/a | if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { |
|---|
| 1171 | n/a | /* Issue #24891: the file descriptor was closed after the first |
|---|
| 1172 | n/a | is_valid_fd() check was called. Ignore the OSError and set the |
|---|
| 1173 | n/a | stream to None. */ |
|---|
| 1174 | n/a | PyErr_Clear(); |
|---|
| 1175 | n/a | Py_RETURN_NONE; |
|---|
| 1176 | n/a | } |
|---|
| 1177 | n/a | return NULL; |
|---|
| 1178 | n/a | } |
|---|
| 1179 | n/a | |
|---|
| 1180 | n/a | /* Initialize sys.stdin, stdout, stderr and builtins.open */ |
|---|
| 1181 | n/a | static int |
|---|
| 1182 | n/a | initstdio(void) |
|---|
| 1183 | n/a | { |
|---|
| 1184 | n/a | PyObject *iomod = NULL, *wrapper; |
|---|
| 1185 | n/a | PyObject *bimod = NULL; |
|---|
| 1186 | n/a | PyObject *m; |
|---|
| 1187 | n/a | PyObject *std = NULL; |
|---|
| 1188 | n/a | int status = 0, fd; |
|---|
| 1189 | n/a | PyObject * encoding_attr; |
|---|
| 1190 | n/a | char *pythonioencoding = NULL, *encoding, *errors; |
|---|
| 1191 | n/a | |
|---|
| 1192 | n/a | /* Hack to avoid a nasty recursion issue when Python is invoked |
|---|
| 1193 | n/a | in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ |
|---|
| 1194 | n/a | if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { |
|---|
| 1195 | n/a | goto error; |
|---|
| 1196 | n/a | } |
|---|
| 1197 | n/a | Py_DECREF(m); |
|---|
| 1198 | n/a | |
|---|
| 1199 | n/a | if (!(m = PyImport_ImportModule("encodings.latin_1"))) { |
|---|
| 1200 | n/a | goto error; |
|---|
| 1201 | n/a | } |
|---|
| 1202 | n/a | Py_DECREF(m); |
|---|
| 1203 | n/a | |
|---|
| 1204 | n/a | if (!(bimod = PyImport_ImportModule("builtins"))) { |
|---|
| 1205 | n/a | goto error; |
|---|
| 1206 | n/a | } |
|---|
| 1207 | n/a | |
|---|
| 1208 | n/a | if (!(iomod = PyImport_ImportModule("io"))) { |
|---|
| 1209 | n/a | goto error; |
|---|
| 1210 | n/a | } |
|---|
| 1211 | n/a | if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { |
|---|
| 1212 | n/a | goto error; |
|---|
| 1213 | n/a | } |
|---|
| 1214 | n/a | |
|---|
| 1215 | n/a | /* Set builtins.open */ |
|---|
| 1216 | n/a | if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { |
|---|
| 1217 | n/a | Py_DECREF(wrapper); |
|---|
| 1218 | n/a | goto error; |
|---|
| 1219 | n/a | } |
|---|
| 1220 | n/a | Py_DECREF(wrapper); |
|---|
| 1221 | n/a | |
|---|
| 1222 | n/a | encoding = _Py_StandardStreamEncoding; |
|---|
| 1223 | n/a | errors = _Py_StandardStreamErrors; |
|---|
| 1224 | n/a | if (!encoding || !errors) { |
|---|
| 1225 | n/a | pythonioencoding = Py_GETENV("PYTHONIOENCODING"); |
|---|
| 1226 | n/a | if (pythonioencoding) { |
|---|
| 1227 | n/a | char *err; |
|---|
| 1228 | n/a | pythonioencoding = _PyMem_Strdup(pythonioencoding); |
|---|
| 1229 | n/a | if (pythonioencoding == NULL) { |
|---|
| 1230 | n/a | PyErr_NoMemory(); |
|---|
| 1231 | n/a | goto error; |
|---|
| 1232 | n/a | } |
|---|
| 1233 | n/a | err = strchr(pythonioencoding, ':'); |
|---|
| 1234 | n/a | if (err) { |
|---|
| 1235 | n/a | *err = '\0'; |
|---|
| 1236 | n/a | err++; |
|---|
| 1237 | n/a | if (*err && !errors) { |
|---|
| 1238 | n/a | errors = err; |
|---|
| 1239 | n/a | } |
|---|
| 1240 | n/a | } |
|---|
| 1241 | n/a | if (*pythonioencoding && !encoding) { |
|---|
| 1242 | n/a | encoding = pythonioencoding; |
|---|
| 1243 | n/a | } |
|---|
| 1244 | n/a | } |
|---|
| 1245 | n/a | if (!errors && !(pythonioencoding && *pythonioencoding)) { |
|---|
| 1246 | n/a | /* When the LC_CTYPE locale is the POSIX locale ("C locale"), |
|---|
| 1247 | n/a | stdin and stdout use the surrogateescape error handler by |
|---|
| 1248 | n/a | default, instead of the strict error handler. */ |
|---|
| 1249 | n/a | char *loc = setlocale(LC_CTYPE, NULL); |
|---|
| 1250 | n/a | if (loc != NULL && strcmp(loc, "C") == 0) |
|---|
| 1251 | n/a | errors = "surrogateescape"; |
|---|
| 1252 | n/a | } |
|---|
| 1253 | n/a | } |
|---|
| 1254 | n/a | |
|---|
| 1255 | n/a | /* Set sys.stdin */ |
|---|
| 1256 | n/a | fd = fileno(stdin); |
|---|
| 1257 | n/a | /* Under some conditions stdin, stdout and stderr may not be connected |
|---|
| 1258 | n/a | * and fileno() may point to an invalid file descriptor. For example |
|---|
| 1259 | n/a | * GUI apps don't have valid standard streams by default. |
|---|
| 1260 | n/a | */ |
|---|
| 1261 | n/a | std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); |
|---|
| 1262 | n/a | if (std == NULL) |
|---|
| 1263 | n/a | goto error; |
|---|
| 1264 | n/a | PySys_SetObject("__stdin__", std); |
|---|
| 1265 | n/a | _PySys_SetObjectId(&PyId_stdin, std); |
|---|
| 1266 | n/a | Py_DECREF(std); |
|---|
| 1267 | n/a | |
|---|
| 1268 | n/a | /* Set sys.stdout */ |
|---|
| 1269 | n/a | fd = fileno(stdout); |
|---|
| 1270 | n/a | std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); |
|---|
| 1271 | n/a | if (std == NULL) |
|---|
| 1272 | n/a | goto error; |
|---|
| 1273 | n/a | PySys_SetObject("__stdout__", std); |
|---|
| 1274 | n/a | _PySys_SetObjectId(&PyId_stdout, std); |
|---|
| 1275 | n/a | Py_DECREF(std); |
|---|
| 1276 | n/a | |
|---|
| 1277 | n/a | #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
|---|
| 1278 | n/a | /* Set sys.stderr, replaces the preliminary stderr */ |
|---|
| 1279 | n/a | fd = fileno(stderr); |
|---|
| 1280 | n/a | std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); |
|---|
| 1281 | n/a | if (std == NULL) |
|---|
| 1282 | n/a | goto error; |
|---|
| 1283 | n/a | |
|---|
| 1284 | n/a | /* Same as hack above, pre-import stderr's codec to avoid recursion |
|---|
| 1285 | n/a | when import.c tries to write to stderr in verbose mode. */ |
|---|
| 1286 | n/a | encoding_attr = PyObject_GetAttrString(std, "encoding"); |
|---|
| 1287 | n/a | if (encoding_attr != NULL) { |
|---|
| 1288 | n/a | const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); |
|---|
| 1289 | n/a | if (std_encoding != NULL) { |
|---|
| 1290 | n/a | PyObject *codec_info = _PyCodec_Lookup(std_encoding); |
|---|
| 1291 | n/a | Py_XDECREF(codec_info); |
|---|
| 1292 | n/a | } |
|---|
| 1293 | n/a | Py_DECREF(encoding_attr); |
|---|
| 1294 | n/a | } |
|---|
| 1295 | n/a | PyErr_Clear(); /* Not a fatal error if codec isn't available */ |
|---|
| 1296 | n/a | |
|---|
| 1297 | n/a | if (PySys_SetObject("__stderr__", std) < 0) { |
|---|
| 1298 | n/a | Py_DECREF(std); |
|---|
| 1299 | n/a | goto error; |
|---|
| 1300 | n/a | } |
|---|
| 1301 | n/a | if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { |
|---|
| 1302 | n/a | Py_DECREF(std); |
|---|
| 1303 | n/a | goto error; |
|---|
| 1304 | n/a | } |
|---|
| 1305 | n/a | Py_DECREF(std); |
|---|
| 1306 | n/a | #endif |
|---|
| 1307 | n/a | |
|---|
| 1308 | n/a | if (0) { |
|---|
| 1309 | n/a | error: |
|---|
| 1310 | n/a | status = -1; |
|---|
| 1311 | n/a | } |
|---|
| 1312 | n/a | |
|---|
| 1313 | n/a | /* We won't need them anymore. */ |
|---|
| 1314 | n/a | if (_Py_StandardStreamEncoding) { |
|---|
| 1315 | n/a | PyMem_RawFree(_Py_StandardStreamEncoding); |
|---|
| 1316 | n/a | _Py_StandardStreamEncoding = NULL; |
|---|
| 1317 | n/a | } |
|---|
| 1318 | n/a | if (_Py_StandardStreamErrors) { |
|---|
| 1319 | n/a | PyMem_RawFree(_Py_StandardStreamErrors); |
|---|
| 1320 | n/a | _Py_StandardStreamErrors = NULL; |
|---|
| 1321 | n/a | } |
|---|
| 1322 | n/a | PyMem_Free(pythonioencoding); |
|---|
| 1323 | n/a | Py_XDECREF(bimod); |
|---|
| 1324 | n/a | Py_XDECREF(iomod); |
|---|
| 1325 | n/a | return status; |
|---|
| 1326 | n/a | } |
|---|
| 1327 | n/a | |
|---|
| 1328 | n/a | |
|---|
| 1329 | n/a | static void |
|---|
| 1330 | n/a | _Py_FatalError_DumpTracebacks(int fd) |
|---|
| 1331 | n/a | { |
|---|
| 1332 | n/a | fputc('\n', stderr); |
|---|
| 1333 | n/a | fflush(stderr); |
|---|
| 1334 | n/a | |
|---|
| 1335 | n/a | /* display the current Python stack */ |
|---|
| 1336 | n/a | _Py_DumpTracebackThreads(fd, NULL, NULL); |
|---|
| 1337 | n/a | } |
|---|
| 1338 | n/a | |
|---|
| 1339 | n/a | /* Print the current exception (if an exception is set) with its traceback, |
|---|
| 1340 | n/a | or display the current Python stack. |
|---|
| 1341 | n/a | |
|---|
| 1342 | n/a | Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is |
|---|
| 1343 | n/a | called on catastrophic cases. |
|---|
| 1344 | n/a | |
|---|
| 1345 | n/a | Return 1 if the traceback was displayed, 0 otherwise. */ |
|---|
| 1346 | n/a | |
|---|
| 1347 | n/a | static int |
|---|
| 1348 | n/a | _Py_FatalError_PrintExc(int fd) |
|---|
| 1349 | n/a | { |
|---|
| 1350 | n/a | PyObject *ferr, *res; |
|---|
| 1351 | n/a | PyObject *exception, *v, *tb; |
|---|
| 1352 | n/a | int has_tb; |
|---|
| 1353 | n/a | |
|---|
| 1354 | n/a | if (PyThreadState_GET() == NULL) { |
|---|
| 1355 | n/a | /* The GIL is released: trying to acquire it is likely to deadlock, |
|---|
| 1356 | n/a | just give up. */ |
|---|
| 1357 | n/a | return 0; |
|---|
| 1358 | n/a | } |
|---|
| 1359 | n/a | |
|---|
| 1360 | n/a | PyErr_Fetch(&exception, &v, &tb); |
|---|
| 1361 | n/a | if (exception == NULL) { |
|---|
| 1362 | n/a | /* No current exception */ |
|---|
| 1363 | n/a | return 0; |
|---|
| 1364 | n/a | } |
|---|
| 1365 | n/a | |
|---|
| 1366 | n/a | ferr = _PySys_GetObjectId(&PyId_stderr); |
|---|
| 1367 | n/a | if (ferr == NULL || ferr == Py_None) { |
|---|
| 1368 | n/a | /* sys.stderr is not set yet or set to None, |
|---|
| 1369 | n/a | no need to try to display the exception */ |
|---|
| 1370 | n/a | return 0; |
|---|
| 1371 | n/a | } |
|---|
| 1372 | n/a | |
|---|
| 1373 | n/a | PyErr_NormalizeException(&exception, &v, &tb); |
|---|
| 1374 | n/a | if (tb == NULL) { |
|---|
| 1375 | n/a | tb = Py_None; |
|---|
| 1376 | n/a | Py_INCREF(tb); |
|---|
| 1377 | n/a | } |
|---|
| 1378 | n/a | PyException_SetTraceback(v, tb); |
|---|
| 1379 | n/a | if (exception == NULL) { |
|---|
| 1380 | n/a | /* PyErr_NormalizeException() failed */ |
|---|
| 1381 | n/a | return 0; |
|---|
| 1382 | n/a | } |
|---|
| 1383 | n/a | |
|---|
| 1384 | n/a | has_tb = (tb != Py_None); |
|---|
| 1385 | n/a | PyErr_Display(exception, v, tb); |
|---|
| 1386 | n/a | Py_XDECREF(exception); |
|---|
| 1387 | n/a | Py_XDECREF(v); |
|---|
| 1388 | n/a | Py_XDECREF(tb); |
|---|
| 1389 | n/a | |
|---|
| 1390 | n/a | /* sys.stderr may be buffered: call sys.stderr.flush() */ |
|---|
| 1391 | n/a | res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); |
|---|
| 1392 | n/a | if (res == NULL) |
|---|
| 1393 | n/a | PyErr_Clear(); |
|---|
| 1394 | n/a | else |
|---|
| 1395 | n/a | Py_DECREF(res); |
|---|
| 1396 | n/a | |
|---|
| 1397 | n/a | return has_tb; |
|---|
| 1398 | n/a | } |
|---|
| 1399 | n/a | |
|---|
| 1400 | n/a | /* Print fatal error message and abort */ |
|---|
| 1401 | n/a | |
|---|
| 1402 | n/a | void |
|---|
| 1403 | n/a | Py_FatalError(const char *msg) |
|---|
| 1404 | n/a | { |
|---|
| 1405 | n/a | const int fd = fileno(stderr); |
|---|
| 1406 | n/a | static int reentrant = 0; |
|---|
| 1407 | n/a | #ifdef MS_WINDOWS |
|---|
| 1408 | n/a | size_t len; |
|---|
| 1409 | n/a | WCHAR* buffer; |
|---|
| 1410 | n/a | size_t i; |
|---|
| 1411 | n/a | #endif |
|---|
| 1412 | n/a | |
|---|
| 1413 | n/a | if (reentrant) { |
|---|
| 1414 | n/a | /* Py_FatalError() caused a second fatal error. |
|---|
| 1415 | n/a | Example: flush_std_files() raises a recursion error. */ |
|---|
| 1416 | n/a | goto exit; |
|---|
| 1417 | n/a | } |
|---|
| 1418 | n/a | reentrant = 1; |
|---|
| 1419 | n/a | |
|---|
| 1420 | n/a | fprintf(stderr, "Fatal Python error: %s\n", msg); |
|---|
| 1421 | n/a | fflush(stderr); /* it helps in Windows debug build */ |
|---|
| 1422 | n/a | |
|---|
| 1423 | n/a | /* Print the exception (if an exception is set) with its traceback, |
|---|
| 1424 | n/a | * or display the current Python stack. */ |
|---|
| 1425 | n/a | if (!_Py_FatalError_PrintExc(fd)) |
|---|
| 1426 | n/a | _Py_FatalError_DumpTracebacks(fd); |
|---|
| 1427 | n/a | |
|---|
| 1428 | n/a | /* The main purpose of faulthandler is to display the traceback. We already |
|---|
| 1429 | n/a | * did our best to display it. So faulthandler can now be disabled. |
|---|
| 1430 | n/a | * (Don't trigger it on abort().) */ |
|---|
| 1431 | n/a | _PyFaulthandler_Fini(); |
|---|
| 1432 | n/a | |
|---|
| 1433 | n/a | /* Check if the current Python thread hold the GIL */ |
|---|
| 1434 | n/a | if (PyThreadState_GET() != NULL) { |
|---|
| 1435 | n/a | /* Flush sys.stdout and sys.stderr */ |
|---|
| 1436 | n/a | flush_std_files(); |
|---|
| 1437 | n/a | } |
|---|
| 1438 | n/a | |
|---|
| 1439 | n/a | #ifdef MS_WINDOWS |
|---|
| 1440 | n/a | len = strlen(msg); |
|---|
| 1441 | n/a | |
|---|
| 1442 | n/a | /* Convert the message to wchar_t. This uses a simple one-to-one |
|---|
| 1443 | n/a | conversion, assuming that the this error message actually uses ASCII |
|---|
| 1444 | n/a | only. If this ceases to be true, we will have to convert. */ |
|---|
| 1445 | n/a | buffer = alloca( (len+1) * (sizeof *buffer)); |
|---|
| 1446 | n/a | for( i=0; i<=len; ++i) |
|---|
| 1447 | n/a | buffer[i] = msg[i]; |
|---|
| 1448 | n/a | OutputDebugStringW(L"Fatal Python error: "); |
|---|
| 1449 | n/a | OutputDebugStringW(buffer); |
|---|
| 1450 | n/a | OutputDebugStringW(L"\n"); |
|---|
| 1451 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 1452 | n/a | |
|---|
| 1453 | n/a | exit: |
|---|
| 1454 | n/a | #if defined(MS_WINDOWS) && defined(_DEBUG) |
|---|
| 1455 | n/a | DebugBreak(); |
|---|
| 1456 | n/a | #endif |
|---|
| 1457 | n/a | abort(); |
|---|
| 1458 | n/a | } |
|---|
| 1459 | n/a | |
|---|
| 1460 | n/a | /* Clean up and exit */ |
|---|
| 1461 | n/a | |
|---|
| 1462 | n/a | #ifdef WITH_THREAD |
|---|
| 1463 | n/a | # include "pythread.h" |
|---|
| 1464 | n/a | #endif |
|---|
| 1465 | n/a | |
|---|
| 1466 | n/a | static void (*pyexitfunc)(void) = NULL; |
|---|
| 1467 | n/a | /* For the atexit module. */ |
|---|
| 1468 | n/a | void _Py_PyAtExit(void (*func)(void)) |
|---|
| 1469 | n/a | { |
|---|
| 1470 | n/a | pyexitfunc = func; |
|---|
| 1471 | n/a | } |
|---|
| 1472 | n/a | |
|---|
| 1473 | n/a | static void |
|---|
| 1474 | n/a | call_py_exitfuncs(void) |
|---|
| 1475 | n/a | { |
|---|
| 1476 | n/a | if (pyexitfunc == NULL) |
|---|
| 1477 | n/a | return; |
|---|
| 1478 | n/a | |
|---|
| 1479 | n/a | (*pyexitfunc)(); |
|---|
| 1480 | n/a | PyErr_Clear(); |
|---|
| 1481 | n/a | } |
|---|
| 1482 | n/a | |
|---|
| 1483 | n/a | /* Wait until threading._shutdown completes, provided |
|---|
| 1484 | n/a | the threading module was imported in the first place. |
|---|
| 1485 | n/a | The shutdown routine will wait until all non-daemon |
|---|
| 1486 | n/a | "threading" threads have completed. */ |
|---|
| 1487 | n/a | static void |
|---|
| 1488 | n/a | wait_for_thread_shutdown(void) |
|---|
| 1489 | n/a | { |
|---|
| 1490 | n/a | #ifdef WITH_THREAD |
|---|
| 1491 | n/a | _Py_IDENTIFIER(_shutdown); |
|---|
| 1492 | n/a | PyObject *result; |
|---|
| 1493 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 1494 | n/a | PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, |
|---|
| 1495 | n/a | "threading"); |
|---|
| 1496 | n/a | if (threading == NULL) { |
|---|
| 1497 | n/a | /* threading not imported */ |
|---|
| 1498 | n/a | PyErr_Clear(); |
|---|
| 1499 | n/a | return; |
|---|
| 1500 | n/a | } |
|---|
| 1501 | n/a | result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL); |
|---|
| 1502 | n/a | if (result == NULL) { |
|---|
| 1503 | n/a | PyErr_WriteUnraisable(threading); |
|---|
| 1504 | n/a | } |
|---|
| 1505 | n/a | else { |
|---|
| 1506 | n/a | Py_DECREF(result); |
|---|
| 1507 | n/a | } |
|---|
| 1508 | n/a | Py_DECREF(threading); |
|---|
| 1509 | n/a | #endif |
|---|
| 1510 | n/a | } |
|---|
| 1511 | n/a | |
|---|
| 1512 | n/a | #define NEXITFUNCS 32 |
|---|
| 1513 | n/a | static void (*exitfuncs[NEXITFUNCS])(void); |
|---|
| 1514 | n/a | static int nexitfuncs = 0; |
|---|
| 1515 | n/a | |
|---|
| 1516 | n/a | int Py_AtExit(void (*func)(void)) |
|---|
| 1517 | n/a | { |
|---|
| 1518 | n/a | if (nexitfuncs >= NEXITFUNCS) |
|---|
| 1519 | n/a | return -1; |
|---|
| 1520 | n/a | exitfuncs[nexitfuncs++] = func; |
|---|
| 1521 | n/a | return 0; |
|---|
| 1522 | n/a | } |
|---|
| 1523 | n/a | |
|---|
| 1524 | n/a | static void |
|---|
| 1525 | n/a | call_ll_exitfuncs(void) |
|---|
| 1526 | n/a | { |
|---|
| 1527 | n/a | while (nexitfuncs > 0) |
|---|
| 1528 | n/a | (*exitfuncs[--nexitfuncs])(); |
|---|
| 1529 | n/a | |
|---|
| 1530 | n/a | fflush(stdout); |
|---|
| 1531 | n/a | fflush(stderr); |
|---|
| 1532 | n/a | } |
|---|
| 1533 | n/a | |
|---|
| 1534 | n/a | void |
|---|
| 1535 | n/a | Py_Exit(int sts) |
|---|
| 1536 | n/a | { |
|---|
| 1537 | n/a | if (Py_FinalizeEx() < 0) { |
|---|
| 1538 | n/a | sts = 120; |
|---|
| 1539 | n/a | } |
|---|
| 1540 | n/a | |
|---|
| 1541 | n/a | exit(sts); |
|---|
| 1542 | n/a | } |
|---|
| 1543 | n/a | |
|---|
| 1544 | n/a | static void |
|---|
| 1545 | n/a | initsigs(void) |
|---|
| 1546 | n/a | { |
|---|
| 1547 | n/a | #ifdef SIGPIPE |
|---|
| 1548 | n/a | PyOS_setsig(SIGPIPE, SIG_IGN); |
|---|
| 1549 | n/a | #endif |
|---|
| 1550 | n/a | #ifdef SIGXFZ |
|---|
| 1551 | n/a | PyOS_setsig(SIGXFZ, SIG_IGN); |
|---|
| 1552 | n/a | #endif |
|---|
| 1553 | n/a | #ifdef SIGXFSZ |
|---|
| 1554 | n/a | PyOS_setsig(SIGXFSZ, SIG_IGN); |
|---|
| 1555 | n/a | #endif |
|---|
| 1556 | n/a | PyOS_InitInterrupts(); /* May imply initsignal() */ |
|---|
| 1557 | n/a | if (PyErr_Occurred()) { |
|---|
| 1558 | n/a | Py_FatalError("Py_Initialize: can't import signal"); |
|---|
| 1559 | n/a | } |
|---|
| 1560 | n/a | } |
|---|
| 1561 | n/a | |
|---|
| 1562 | n/a | |
|---|
| 1563 | n/a | /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. |
|---|
| 1564 | n/a | * |
|---|
| 1565 | n/a | * All of the code in this function must only use async-signal-safe functions, |
|---|
| 1566 | n/a | * listed at `man 7 signal` or |
|---|
| 1567 | n/a | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
|---|
| 1568 | n/a | */ |
|---|
| 1569 | n/a | void |
|---|
| 1570 | n/a | _Py_RestoreSignals(void) |
|---|
| 1571 | n/a | { |
|---|
| 1572 | n/a | #ifdef SIGPIPE |
|---|
| 1573 | n/a | PyOS_setsig(SIGPIPE, SIG_DFL); |
|---|
| 1574 | n/a | #endif |
|---|
| 1575 | n/a | #ifdef SIGXFZ |
|---|
| 1576 | n/a | PyOS_setsig(SIGXFZ, SIG_DFL); |
|---|
| 1577 | n/a | #endif |
|---|
| 1578 | n/a | #ifdef SIGXFSZ |
|---|
| 1579 | n/a | PyOS_setsig(SIGXFSZ, SIG_DFL); |
|---|
| 1580 | n/a | #endif |
|---|
| 1581 | n/a | } |
|---|
| 1582 | n/a | |
|---|
| 1583 | n/a | |
|---|
| 1584 | n/a | /* |
|---|
| 1585 | n/a | * The file descriptor fd is considered ``interactive'' if either |
|---|
| 1586 | n/a | * a) isatty(fd) is TRUE, or |
|---|
| 1587 | n/a | * b) the -i flag was given, and the filename associated with |
|---|
| 1588 | n/a | * the descriptor is NULL or "<stdin>" or "???". |
|---|
| 1589 | n/a | */ |
|---|
| 1590 | n/a | int |
|---|
| 1591 | n/a | Py_FdIsInteractive(FILE *fp, const char *filename) |
|---|
| 1592 | n/a | { |
|---|
| 1593 | n/a | if (isatty((int)fileno(fp))) |
|---|
| 1594 | n/a | return 1; |
|---|
| 1595 | n/a | if (!Py_InteractiveFlag) |
|---|
| 1596 | n/a | return 0; |
|---|
| 1597 | n/a | return (filename == NULL) || |
|---|
| 1598 | n/a | (strcmp(filename, "<stdin>") == 0) || |
|---|
| 1599 | n/a | (strcmp(filename, "???") == 0); |
|---|
| 1600 | n/a | } |
|---|
| 1601 | n/a | |
|---|
| 1602 | n/a | |
|---|
| 1603 | n/a | /* Wrappers around sigaction() or signal(). */ |
|---|
| 1604 | n/a | |
|---|
| 1605 | n/a | PyOS_sighandler_t |
|---|
| 1606 | n/a | PyOS_getsig(int sig) |
|---|
| 1607 | n/a | { |
|---|
| 1608 | n/a | #ifdef HAVE_SIGACTION |
|---|
| 1609 | n/a | struct sigaction context; |
|---|
| 1610 | n/a | if (sigaction(sig, NULL, &context) == -1) |
|---|
| 1611 | n/a | return SIG_ERR; |
|---|
| 1612 | n/a | return context.sa_handler; |
|---|
| 1613 | n/a | #else |
|---|
| 1614 | n/a | PyOS_sighandler_t handler; |
|---|
| 1615 | n/a | /* Special signal handling for the secure CRT in Visual Studio 2005 */ |
|---|
| 1616 | n/a | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
|---|
| 1617 | n/a | switch (sig) { |
|---|
| 1618 | n/a | /* Only these signals are valid */ |
|---|
| 1619 | n/a | case SIGINT: |
|---|
| 1620 | n/a | case SIGILL: |
|---|
| 1621 | n/a | case SIGFPE: |
|---|
| 1622 | n/a | case SIGSEGV: |
|---|
| 1623 | n/a | case SIGTERM: |
|---|
| 1624 | n/a | case SIGBREAK: |
|---|
| 1625 | n/a | case SIGABRT: |
|---|
| 1626 | n/a | break; |
|---|
| 1627 | n/a | /* Don't call signal() with other values or it will assert */ |
|---|
| 1628 | n/a | default: |
|---|
| 1629 | n/a | return SIG_ERR; |
|---|
| 1630 | n/a | } |
|---|
| 1631 | n/a | #endif /* _MSC_VER && _MSC_VER >= 1400 */ |
|---|
| 1632 | n/a | handler = signal(sig, SIG_IGN); |
|---|
| 1633 | n/a | if (handler != SIG_ERR) |
|---|
| 1634 | n/a | signal(sig, handler); |
|---|
| 1635 | n/a | return handler; |
|---|
| 1636 | n/a | #endif |
|---|
| 1637 | n/a | } |
|---|
| 1638 | n/a | |
|---|
| 1639 | n/a | /* |
|---|
| 1640 | n/a | * All of the code in this function must only use async-signal-safe functions, |
|---|
| 1641 | n/a | * listed at `man 7 signal` or |
|---|
| 1642 | n/a | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
|---|
| 1643 | n/a | */ |
|---|
| 1644 | n/a | PyOS_sighandler_t |
|---|
| 1645 | n/a | PyOS_setsig(int sig, PyOS_sighandler_t handler) |
|---|
| 1646 | n/a | { |
|---|
| 1647 | n/a | #ifdef HAVE_SIGACTION |
|---|
| 1648 | n/a | /* Some code in Modules/signalmodule.c depends on sigaction() being |
|---|
| 1649 | n/a | * used here if HAVE_SIGACTION is defined. Fix that if this code |
|---|
| 1650 | n/a | * changes to invalidate that assumption. |
|---|
| 1651 | n/a | */ |
|---|
| 1652 | n/a | struct sigaction context, ocontext; |
|---|
| 1653 | n/a | context.sa_handler = handler; |
|---|
| 1654 | n/a | sigemptyset(&context.sa_mask); |
|---|
| 1655 | n/a | context.sa_flags = 0; |
|---|
| 1656 | n/a | if (sigaction(sig, &context, &ocontext) == -1) |
|---|
| 1657 | n/a | return SIG_ERR; |
|---|
| 1658 | n/a | return ocontext.sa_handler; |
|---|
| 1659 | n/a | #else |
|---|
| 1660 | n/a | PyOS_sighandler_t oldhandler; |
|---|
| 1661 | n/a | oldhandler = signal(sig, handler); |
|---|
| 1662 | n/a | #ifdef HAVE_SIGINTERRUPT |
|---|
| 1663 | n/a | siginterrupt(sig, 1); |
|---|
| 1664 | n/a | #endif |
|---|
| 1665 | n/a | return oldhandler; |
|---|
| 1666 | n/a | #endif |
|---|
| 1667 | n/a | } |
|---|
| 1668 | n/a | |
|---|
| 1669 | n/a | #ifdef __cplusplus |
|---|
| 1670 | n/a | } |
|---|
| 1671 | n/a | #endif |
|---|