Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 29877:72cbc41ccc2f legacy-trunk
Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
If the initial import of warnings fails, clear the error. When the module
is actually needed, if the original import failed, see if it has managed
to find its way to sys.modules yet and if so, remember it.
| author | Mark Hammond <mhammond@skippinet.com.au> |
|---|---|
| date | Tue, 15 Jul 2003 23:03:55 +0000 |
| parents | 03af46d1ac28 |
| children | ed8709b1a3ef |
comparison
equal
deleted
inserted
replaced
| 29876:a0a7231c6fe0 | 29877:72cbc41ccc2f |
|---|---|
| 67 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions | 67 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions |
| 68 true divisions (which they will be in 2.3). */ | 68 true divisions (which they will be in 2.3). */ |
| 69 int _Py_QnewFlag = 0; | 69 int _Py_QnewFlag = 0; |
| 70 | 70 |
| 71 /* Reference to 'warnings' module, to avoid importing it | 71 /* Reference to 'warnings' module, to avoid importing it |
| 72 on the fly when the import lock may be held. See 683658 | 72 on the fly when the import lock may be held. See 683658/771097 |
| 73 */ | 73 */ |
| 74 PyObject *PyModule_WarningsModule = NULL; | 74 static PyObject *warnings_module = NULL; |
| 75 | |
| 76 /* Returns a borrowed reference to the 'warnings' module, or NULL. | |
| 77 If the module is returned, it is guaranteed to have been obtained | |
| 78 without acquiring the import lock | |
| 79 */ | |
| 80 PyObject *PyModule_GetWarningsModule() | |
| 81 { | |
| 82 PyObject *typ, *val, *tb; | |
| 83 PyObject *all_modules; | |
| 84 /* If we managed to get the module at init time, just use it */ | |
| 85 if (warnings_module) | |
| 86 return warnings_module; | |
| 87 /* If it wasn't available at init time, it may be available | |
| 88 now in sys.modules (common scenario is frozen apps: import | |
| 89 at init time fails, but the frozen init code sets up sys.path | |
| 90 correctly, then does an implicit import of warnings for us | |
| 91 */ | |
| 92 /* Save and restore any exceptions */ | |
| 93 PyErr_Fetch(&typ, &val, &tb); | |
| 94 | |
| 95 all_modules = PySys_GetObject("__modules__"); | |
| 96 if (all_modules) { | |
| 97 warnings_module = PyDict_GetItemString(all_modules, "warnings"); | |
| 98 /* We keep a ref in the global */ | |
| 99 Py_XINCREF(warnings_module); | |
| 100 } | |
| 101 PyErr_Restore(typ, val, tb); | |
| 102 return warnings_module; | |
| 103 } | |
| 75 | 104 |
| 76 static int initialized = 0; | 105 static int initialized = 0; |
| 77 | 106 |
| 78 /* API to access the initialized flag -- useful for esoteric use */ | 107 /* API to access the initialized flag -- useful for esoteric use */ |
| 79 | 108 |
| 188 /* auto-thread-state API, if available */ | 217 /* auto-thread-state API, if available */ |
| 189 #ifdef WITH_THREAD | 218 #ifdef WITH_THREAD |
| 190 _PyGILState_Init(interp, tstate); | 219 _PyGILState_Init(interp, tstate); |
| 191 #endif /* WITH_THREAD */ | 220 #endif /* WITH_THREAD */ |
| 192 | 221 |
| 193 PyModule_WarningsModule = PyImport_ImportModule("warnings"); | 222 warnings_module = PyImport_ImportModule("warnings"); |
| 223 if (!warnings_module) | |
| 224 PyErr_Clear(); | |
| 194 | 225 |
| 195 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) | 226 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 196 /* On Unix, set the file system encoding according to the | 227 /* On Unix, set the file system encoding according to the |
| 197 user's preference, if the CODESET names a well-known | 228 user's preference, if the CODESET names a well-known |
| 198 Python codec, and Py_FileSystemDefaultEncoding isn't | 229 Python codec, and Py_FileSystemDefaultEncoding isn't |
| 260 | 291 |
| 261 /* Disable signal handling */ | 292 /* Disable signal handling */ |
| 262 PyOS_FiniInterrupts(); | 293 PyOS_FiniInterrupts(); |
| 263 | 294 |
| 264 /* drop module references we saved */ | 295 /* drop module references we saved */ |
| 265 Py_XDECREF(PyModule_WarningsModule); | 296 Py_XDECREF(warnings_module); |
| 266 PyModule_WarningsModule = NULL; | 297 warnings_module = NULL; |
| 267 | 298 |
| 268 /* Collect garbage. This may call finalizers; it's nice to call these | 299 /* Collect garbage. This may call finalizers; it's nice to call these |
| 269 before all modules are destroyed. */ | 300 before all modules are destroyed. */ |
| 270 PyGC_Collect(); | 301 PyGC_Collect(); |
| 271 | 302 |
