Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2fd4565

Browse files
committed
Add PyErr_WarnExplicit(), which calls warnings.warn_explicit(), with
explicit filename, lineno etc. arguments.
1 parent 1bcb7e9 commit 2fd4565

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Python/errors.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,48 @@ PyErr_Warn(PyObject *category, char *message)
623623
}
624624
}
625625

626+
627+
/* Warning with explicit origin */
628+
int
629+
PyErr_WarnExplicit(PyObject *category, char *message,
630+
char *filename, int lineno,
631+
char *module, PyObject *registry)
632+
{
633+
PyObject *mod, *dict, *func = NULL;
634+
635+
mod = PyImport_ImportModule("warnings");
636+
if (mod != NULL) {
637+
dict = PyModule_GetDict(mod);
638+
func = PyDict_GetItemString(dict, "warn_explicit");
639+
Py_DECREF(mod);
640+
}
641+
if (func == NULL) {
642+
PySys_WriteStderr("warning: %s\n", message);
643+
return 0;
644+
}
645+
else {
646+
PyObject *args, *res;
647+
648+
if (category == NULL)
649+
category = PyExc_RuntimeWarning;
650+
if (registry == NULL)
651+
registry = Py_None;
652+
args = Py_BuildValue("(sOsizO)", message, category,
653+
filename, lineno, module, registry);
654+
if (args == NULL)
655+
return -1;
656+
res = PyEval_CallObject(func, args);
657+
Py_DECREF(args);
658+
if (res == NULL)
659+
return -1;
660+
Py_DECREF(res);
661+
return 0;
662+
}
663+
}
664+
665+
666+
/* XXX There's a comment missing here */
667+
626668
void
627669
PyErr_SyntaxLocation(char *filename, int lineno)
628670
{

0 commit comments

Comments
 (0)