]> granicus.if.org Git - python/commitdiff
Add PyErr_Warn().
authorGuido van Rossum <guido@python.org>
Fri, 15 Dec 2000 21:58:52 +0000 (21:58 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 15 Dec 2000 21:58:52 +0000 (21:58 +0000)
Python/errors.c

index 3053e0027bdcf61d7e5dc2c649f31a2f4cb3fd66..908c0c188ec094c157db574b28443fdb2e0c6266 100644 (file)
@@ -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;
+       }
+}