diff Python/errors.c @ 15591:36286632ff3a legacy-trunk

Add PyErr_Warn().
author Guido van Rossum <guido@python.org>
date Fri, 15 Dec 2000 21:58:52 +0000
parents 8aa947e1eee8
children 09f6c1372056
line wrap: on
line diff
--- a/Python/errors.c	Fri Dec 15 21:58:29 2000 +0000
+++ b/Python/errors.c	Fri Dec 15 21:58:52 2000 +0000
@@ -588,3 +588,37 @@
 	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;
+	}
+}