comparison Python/pythonrun.c @ 46414:840064802952 legacy-trunk

Re-implement the 'warnings' module in C. This allows for usage of the 'warnings' code in places where it was previously not possible (e.g., the parser). It could also potentially lead to a speed-up in interpreter start-up if the C version of the code (_warnings) is imported over the use of the Python version in key places. Closes issue #1631171.
author Brett Cannon <bcannon@gmail.com>
date Sat, 12 Apr 2008 23:44:07 +0000
parents 248676988c54
children 1a2dbc84bdfc
comparison
equal deleted inserted replaced
46413:2a9858255de1 46414:840064802952
81 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed, 81 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
82 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions 82 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
83 true divisions (which they will be in 2.3). */ 83 true divisions (which they will be in 2.3). */
84 int _Py_QnewFlag = 0; 84 int _Py_QnewFlag = 0;
85 85
86 /* Reference to 'warnings' module, to avoid importing it 86 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
87 on the fly when the import lock may be held. See 683658/771097 87 since _warnings is builtin. This API should not be used. */
88 */ 88 PyObject *
89 static PyObject *warnings_module = NULL; 89 PyModule_GetWarningsModule(void)
90 90 {
91 /* Returns a borrowed reference to the 'warnings' module, or NULL. 91 return PyImport_ImportModule("warnings");
92 If the module is returned, it is guaranteed to have been obtained
93 without acquiring the import lock
94 */
95 PyObject *PyModule_GetWarningsModule(void)
96 {
97 PyObject *typ, *val, *tb;
98 PyObject *all_modules;
99 /* If we managed to get the module at init time, just use it */
100 if (warnings_module)
101 return warnings_module;
102 /* If it wasn't available at init time, it may be available
103 now in sys.modules (common scenario is frozen apps: import
104 at init time fails, but the frozen init code sets up sys.path
105 correctly, then does an implicit import of warnings for us
106 */
107 /* Save and restore any exceptions */
108 PyErr_Fetch(&typ, &val, &tb);
109
110 all_modules = PySys_GetObject("modules");
111 if (all_modules) {
112 warnings_module = PyDict_GetItemString(all_modules, "warnings");
113 /* We keep a ref in the global */
114 Py_XINCREF(warnings_module);
115 }
116 PyErr_Restore(typ, val, tb);
117 return warnings_module;
118 } 92 }
119 93
120 static int initialized = 0; 94 static int initialized = 0;
121 95
122 /* API to access the initialized flag -- useful for esoteric use */ 96 /* API to access the initialized flag -- useful for esoteric use */
242 216
243 _PyImportHooks_Init(); 217 _PyImportHooks_Init();
244 218
245 if (install_sigs) 219 if (install_sigs)
246 initsigs(); /* Signal handling stuff, including initintr() */ 220 initsigs(); /* Signal handling stuff, including initintr() */
221
222 /* Initialize warnings. */
223 _PyWarnings_Init();
224 if (PySys_HasWarnOptions()) {
225 PyObject *warnings_module = PyImport_ImportModule("warnings");
226 if (!warnings_module)
227 PyErr_Clear();
228 Py_XDECREF(warnings_module);
229 }
247 230
248 initmain(); /* Module __main__ */ 231 initmain(); /* Module __main__ */
249 if (!Py_NoSiteFlag) 232 if (!Py_NoSiteFlag)
250 initsite(); /* Module site */ 233 initsite(); /* Module site */
251 234
252 /* auto-thread-state API, if available */ 235 /* auto-thread-state API, if available */
253 #ifdef WITH_THREAD 236 #ifdef WITH_THREAD
254 _PyGILState_Init(interp, tstate); 237 _PyGILState_Init(interp, tstate);
255 #endif /* WITH_THREAD */ 238 #endif /* WITH_THREAD */
256
257 warnings_module = PyImport_ImportModule("warnings");
258 if (!warnings_module) {
259 PyErr_Clear();
260 }
261 else {
262 PyObject *o;
263 char *action[8];
264
265 if (Py_BytesWarningFlag > 1)
266 *action = "error";
267 else if (Py_BytesWarningFlag)
268 *action = "default";
269 else
270 *action = "ignore";
271
272 o = PyObject_CallMethod(warnings_module,
273 "simplefilter", "sO",
274 *action, PyExc_BytesWarning);
275 if (o == NULL)
276 Py_FatalError("Py_Initialize: can't initialize"
277 "warning filter for BytesWarning.");
278 Py_DECREF(o);
279 }
280 239
281 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) 240 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
282 /* On Unix, set the file system encoding according to the 241 /* On Unix, set the file system encoding according to the
283 user's preference, if the CODESET names a well-known 242 user's preference, if the CODESET names a well-known
284 Python codec, and Py_FileSystemDefaultEncoding isn't 243 Python codec, and Py_FileSystemDefaultEncoding isn't
394 tstate = PyThreadState_GET(); 353 tstate = PyThreadState_GET();
395 interp = tstate->interp; 354 interp = tstate->interp;
396 355
397 /* Disable signal handling */ 356 /* Disable signal handling */
398 PyOS_FiniInterrupts(); 357 PyOS_FiniInterrupts();
399
400 /* drop module references we saved */
401 Py_XDECREF(warnings_module);
402 warnings_module = NULL;
403 358
404 /* Clear type lookup cache */ 359 /* Clear type lookup cache */
405 PyType_ClearCache(); 360 PyType_ClearCache();
406 361
407 /* Collect garbage. This may call finalizers; it's nice to call these 362 /* Collect garbage. This may call finalizers; it's nice to call these