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

Skip to content

Commit cfd42b5

Browse files
committed
Add PyErr_Warn().
1 parent d0977cd commit cfd42b5

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Python/errors.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,37 @@ PyErr_WriteUnraisable(PyObject *obj)
588588
Py_XDECREF(v);
589589
Py_XDECREF(tb);
590590
}
591+
592+
593+
/* Function to issue a warning message; may raise an exception. */
594+
int
595+
PyErr_Warn(PyObject *category, char *message)
596+
{
597+
PyObject *mod, *dict, *func = NULL;
598+
599+
mod = PyImport_ImportModule("warnings");
600+
if (mod != NULL) {
601+
dict = PyModule_GetDict(mod);
602+
func = PyDict_GetItemString(dict, "warn");
603+
Py_DECREF(mod);
604+
}
605+
if (func == NULL) {
606+
PySys_WriteStderr("warning: %s\n", message);
607+
return 0;
608+
}
609+
else {
610+
PyObject *args, *res;
611+
612+
if (category == NULL)
613+
category = PyExc_RuntimeWarning;
614+
args = Py_BuildValue("(sO)", message, category);
615+
if (args == NULL)
616+
return -1;
617+
res = PyEval_CallObject(func, args);
618+
Py_DECREF(args);
619+
if (res == NULL)
620+
return -1;
621+
Py_DECREF(res);
622+
return 0;
623+
}
624+
}

0 commit comments

Comments
 (0)