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.
This commit is contained in:
Brett Cannon 2008-04-12 23:44:07 +00:00
parent e6c03033af
commit e974689038
18 changed files with 1473 additions and 393 deletions

View file

@ -83,38 +83,12 @@ int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
true divisions (which they will be in 2.3). */
int _Py_QnewFlag = 0;
/* Reference to 'warnings' module, to avoid importing it
on the fly when the import lock may be held. See 683658/771097
*/
static PyObject *warnings_module = NULL;
/* Returns a borrowed reference to the 'warnings' module, or NULL.
If the module is returned, it is guaranteed to have been obtained
without acquiring the import lock
*/
PyObject *PyModule_GetWarningsModule(void)
/* PyModule_GetWarningsModule is no longer necessary as of 2.6
since _warnings is builtin. This API should not be used. */
PyObject *
PyModule_GetWarningsModule(void)
{
PyObject *typ, *val, *tb;
PyObject *all_modules;
/* If we managed to get the module at init time, just use it */
if (warnings_module)
return warnings_module;
/* If it wasn't available at init time, it may be available
now in sys.modules (common scenario is frozen apps: import
at init time fails, but the frozen init code sets up sys.path
correctly, then does an implicit import of warnings for us
*/
/* Save and restore any exceptions */
PyErr_Fetch(&typ, &val, &tb);
all_modules = PySys_GetObject("modules");
if (all_modules) {
warnings_module = PyDict_GetItemString(all_modules, "warnings");
/* We keep a ref in the global */
Py_XINCREF(warnings_module);
}
PyErr_Restore(typ, val, tb);
return warnings_module;
return PyImport_ImportModule("warnings");
}
static int initialized = 0;
@ -244,6 +218,15 @@ Py_InitializeEx(int install_sigs)
if (install_sigs)
initsigs(); /* Signal handling stuff, including initintr() */
/* Initialize warnings. */
_PyWarnings_Init();
if (PySys_HasWarnOptions()) {
PyObject *warnings_module = PyImport_ImportModule("warnings");
if (!warnings_module)
PyErr_Clear();
Py_XDECREF(warnings_module);
}
initmain(); /* Module __main__ */
if (!Py_NoSiteFlag)
@ -254,30 +237,6 @@ Py_InitializeEx(int install_sigs)
_PyGILState_Init(interp, tstate);
#endif /* WITH_THREAD */
warnings_module = PyImport_ImportModule("warnings");
if (!warnings_module) {
PyErr_Clear();
}
else {
PyObject *o;
char *action[8];
if (Py_BytesWarningFlag > 1)
*action = "error";
else if (Py_BytesWarningFlag)
*action = "default";
else
*action = "ignore";
o = PyObject_CallMethod(warnings_module,
"simplefilter", "sO",
*action, PyExc_BytesWarning);
if (o == NULL)
Py_FatalError("Py_Initialize: can't initialize"
"warning filter for BytesWarning.");
Py_DECREF(o);
}
#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
/* On Unix, set the file system encoding according to the
user's preference, if the CODESET names a well-known
@ -397,10 +356,6 @@ Py_Finalize(void)
/* Disable signal handling */
PyOS_FiniInterrupts();
/* drop module references we saved */
Py_XDECREF(warnings_module);
warnings_module = NULL;
/* Clear type lookup cache */
PyType_ClearCache();