]> granicus.if.org Git - python/commitdiff
Add PyErr_WarnExplicit(), which calls warnings.warn_explicit(), with
authorGuido van Rossum <guido@python.org>
Wed, 28 Feb 2001 21:46:24 +0000 (21:46 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 28 Feb 2001 21:46:24 +0000 (21:46 +0000)
explicit filename, lineno etc. arguments.

Python/errors.c

index 41050ce72105e75ab5ccca0f797f0b61345e8ff3..7f30f7ee0cc4cc33490c7b12b8abe5a056601b2d 100644 (file)
@@ -623,6 +623,48 @@ PyErr_Warn(PyObject *category, char *message)
        }
 }
 
+
+/* Warning with explicit origin */
+int
+PyErr_WarnExplicit(PyObject *category, char *message,
+                  char *filename, int lineno,
+                  char *module, PyObject *registry)
+{
+       PyObject *mod, *dict, *func = NULL;
+
+       mod = PyImport_ImportModule("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 *args, *res;
+
+               if (category == NULL)
+                       category = PyExc_RuntimeWarning;
+               if (registry == NULL)
+                       registry = Py_None;
+               args = Py_BuildValue("(sOsizO)", message, category,
+                                    filename, lineno, module, registry);
+               if (args == NULL)
+                       return -1;
+               res = PyEval_CallObject(func, args);
+               Py_DECREF(args);
+               if (res == NULL)
+                       return -1;
+               Py_DECREF(res);
+               return 0;
+       }
+}
+
+
+/* XXX There's a comment missing here */
+
 void
 PyErr_SyntaxLocation(char *filename, int lineno)
 {