summaryrefslogtreecommitdiffstats
path: root/Python/_warnings.c
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2014-07-11 16:50:25 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2014-07-11 16:50:25 (GMT)
commitd8089e0d04a98ab7997eff7abc9abf2a4f6854b8 (patch)
tree3690ed07fb49970ca55ce6a7fd2477232697dfda /Python/_warnings.c
parent6e1ccfe87261a9bc3818d1b4c2409eb1b7db19c5 (diff)
downloadcpython-d8089e0d04a98ab7997eff7abc9abf2a4f6854b8.zip
cpython-d8089e0d04a98ab7997eff7abc9abf2a4f6854b8.tar.gz
cpython-d8089e0d04a98ab7997eff7abc9abf2a4f6854b8.tar.bz2
Issue #16382: Improve exception message of warnings.warn() for bad category.
Initial patch by Phil Elson.
Diffstat (limited to 'Python/_warnings.c')
-rw-r--r--Python/_warnings.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 6013d7d..363c1f2 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -619,16 +619,17 @@ get_category(PyObject *message, PyObject *category)
if (rc == 1)
category = (PyObject*)message->ob_type;
- else if (category == NULL)
+ else if (category == NULL || category == Py_None)
category = PyExc_UserWarning;
/* Validate category. */
rc = PyObject_IsSubclass(category, PyExc_Warning);
- if (rc == -1)
- return NULL;
- if (rc == 0) {
- PyErr_SetString(PyExc_ValueError,
- "category is not a subclass of Warning");
+ /* category is not a subclass of PyExc_Warning or
+ PyObject_IsSubclass raised an error */
+ if (rc == -1 || rc == 0) {
+ PyErr_Format(PyExc_TypeError,
+ "category must be a Warning subclass, not '%s'",
+ Py_TYPE(category)->tp_name);
return NULL;
}