mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
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:
parent
e6c03033af
commit
e974689038
18 changed files with 1473 additions and 393 deletions
|
|
@ -641,81 +641,6 @@ PyErr_WriteUnraisable(PyObject *obj)
|
|||
|
||||
extern PyObject *PyModule_GetWarningsModule(void);
|
||||
|
||||
/* Function to issue a warning message; may raise an exception. */
|
||||
int
|
||||
PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
|
||||
{
|
||||
PyObject *dict, *func = NULL;
|
||||
PyObject *warnings_module = PyModule_GetWarningsModule();
|
||||
|
||||
if (warnings_module != NULL) {
|
||||
dict = PyModule_GetDict(warnings_module);
|
||||
if (dict != NULL)
|
||||
func = PyDict_GetItemString(dict, "warn");
|
||||
}
|
||||
if (func == NULL) {
|
||||
PySys_WriteStderr("warning: %s\n", message);
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
PyObject *res;
|
||||
|
||||
if (category == NULL)
|
||||
category = PyExc_RuntimeWarning;
|
||||
res = PyObject_CallFunction(func, "sOn",
|
||||
message, category, stack_level);
|
||||
if (res == NULL)
|
||||
return -1;
|
||||
Py_DECREF(res);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* PyErr_Warn is only for backwards compatability and will be removed.
|
||||
Use PyErr_WarnEx instead. */
|
||||
|
||||
#undef PyErr_Warn
|
||||
|
||||
PyAPI_FUNC(int)
|
||||
PyErr_Warn(PyObject *category, char *message)
|
||||
{
|
||||
return PyErr_WarnEx(category, message, 1);
|
||||
}
|
||||
|
||||
/* Warning with explicit origin */
|
||||
int
|
||||
PyErr_WarnExplicit(PyObject *category, const char *message,
|
||||
const char *filename, int lineno,
|
||||
const char *module, PyObject *registry)
|
||||
{
|
||||
PyObject *mod, *dict, *func = NULL;
|
||||
|
||||
mod = PyImport_ImportModuleNoBlock("warnings");
|
||||
if (mod != NULL) {
|
||||
dict = PyModule_GetDict(mod);
|
||||
func = PyDict_GetItemString(dict, "warn_explicit");
|
||||
Py_DECREF(mod);
|
||||
}
|
||||
if (func == NULL) {
|
||||
PySys_WriteStderr("warning: %s\n", message);
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
PyObject *res;
|
||||
|
||||
if (category == NULL)
|
||||
category = PyExc_RuntimeWarning;
|
||||
if (registry == NULL)
|
||||
registry = Py_None;
|
||||
res = PyObject_CallFunction(func, "sOsizO", message, category,
|
||||
filename, lineno, module, registry);
|
||||
if (res == NULL)
|
||||
return -1;
|
||||
Py_DECREF(res);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Set file and line information for the current exception.
|
||||
If the exception is not a SyntaxError, also sets additional attributes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue