mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Add PyErr_Warn().
This commit is contained in:
parent
d0977cd670
commit
cfd42b556b
1 changed files with 34 additions and 0 deletions
|
@ -588,3 +588,37 @@ PyErr_WriteUnraisable(PyObject *obj)
|
|||
Py_XDECREF(v);
|
||||
Py_XDECREF(tb);
|
||||
}
|
||||
|
||||
|
||||
/* Function to issue a warning message; may raise an exception. */
|
||||
int
|
||||
PyErr_Warn(PyObject *category, char *message)
|
||||
{
|
||||
PyObject *mod, *dict, *func = NULL;
|
||||
|
||||
mod = PyImport_ImportModule("warnings");
|
||||
if (mod != NULL) {
|
||||
dict = PyModule_GetDict(mod);
|
||||
func = PyDict_GetItemString(dict, "warn");
|
||||
Py_DECREF(mod);
|
||||
}
|
||||
if (func == NULL) {
|
||||
PySys_WriteStderr("warning: %s\n", message);
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
PyObject *args, *res;
|
||||
|
||||
if (category == NULL)
|
||||
category = PyExc_RuntimeWarning;
|
||||
args = Py_BuildValue("(sO)", message, category);
|
||||
if (args == NULL)
|
||||
return -1;
|
||||
res = PyEval_CallObject(func, args);
|
||||
Py_DECREF(args);
|
||||
if (res == NULL)
|
||||
return -1;
|
||||
Py_DECREF(res);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue