Issue #16382: Improve exception message of warnings.warn() for bad category.

Initial patch by Phil Elson.
This commit is contained in:
Berker Peksag 2014-07-11 19:50:25 +03:00
parent 6e1ccfe872
commit d8089e0d04
4 changed files with 48 additions and 7 deletions

View file

@ -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;
}