mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #16382: Improve exception message of warnings.warn() for bad category.
Initial patch by Phil Elson.
This commit is contained in:
parent
6e1ccfe872
commit
d8089e0d04
4 changed files with 48 additions and 7 deletions
|
@ -370,6 +370,41 @@ class WarnTests(BaseTest):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
self.module.warn(BadStrWarning())
|
self.module.warn(BadStrWarning())
|
||||||
|
|
||||||
|
def test_warning_classes(self):
|
||||||
|
class MyWarningClass(Warning):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class NonWarningSubclass:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# passing a non-subclass of Warning should raise a TypeError
|
||||||
|
with self.assertRaises(TypeError) as cm:
|
||||||
|
self.module.warn('bad warning category', '')
|
||||||
|
self.assertIn('category must be a Warning subclass, not ',
|
||||||
|
str(cm.exception))
|
||||||
|
|
||||||
|
with self.assertRaises(TypeError) as cm:
|
||||||
|
self.module.warn('bad warning category', NonWarningSubclass)
|
||||||
|
self.assertIn('category must be a Warning subclass, not ',
|
||||||
|
str(cm.exception))
|
||||||
|
|
||||||
|
# check that warning instances also raise a TypeError
|
||||||
|
with self.assertRaises(TypeError) as cm:
|
||||||
|
self.module.warn('bad warning category', MyWarningClass())
|
||||||
|
self.assertIn('category must be a Warning subclass, not ',
|
||||||
|
str(cm.exception))
|
||||||
|
|
||||||
|
with self.assertWarns(MyWarningClass) as cm:
|
||||||
|
self.module.warn('good warning category', MyWarningClass)
|
||||||
|
self.assertEqual('good warning category', str(cm.warning))
|
||||||
|
|
||||||
|
with self.assertWarns(UserWarning) as cm:
|
||||||
|
self.module.warn('good warning category', None)
|
||||||
|
self.assertEqual('good warning category', str(cm.warning))
|
||||||
|
|
||||||
|
with self.assertWarns(MyWarningClass) as cm:
|
||||||
|
self.module.warn('good warning category', MyWarningClass)
|
||||||
|
self.assertIsInstance(cm.warning, Warning)
|
||||||
|
|
||||||
class CWarnTests(WarnTests, unittest.TestCase):
|
class CWarnTests(WarnTests, unittest.TestCase):
|
||||||
module = c_warnings
|
module = c_warnings
|
||||||
|
|
|
@ -162,7 +162,9 @@ def warn(message, category=None, stacklevel=1):
|
||||||
# Check category argument
|
# Check category argument
|
||||||
if category is None:
|
if category is None:
|
||||||
category = UserWarning
|
category = UserWarning
|
||||||
assert issubclass(category, Warning)
|
if not (isinstance(category, type) and issubclass(category, Warning)):
|
||||||
|
raise TypeError("category must be a Warning subclass, "
|
||||||
|
"not '{:s}'".format(type(category).__name__))
|
||||||
# Get context information
|
# Get context information
|
||||||
try:
|
try:
|
||||||
caller = sys._getframe(stacklevel)
|
caller = sys._getframe(stacklevel)
|
||||||
|
|
|
@ -108,6 +108,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #16382: Improve exception message of warnings.warn() for bad
|
||||||
|
category. Initial patch by Phil Elson.
|
||||||
|
|
||||||
- Issue #21932: os.read() now uses a :c:func:`Py_ssize_t` type instead of
|
- Issue #21932: os.read() now uses a :c:func:`Py_ssize_t` type instead of
|
||||||
:c:type:`int` for the size to support reading more than 2 GB at once. On
|
:c:type:`int` for the size to support reading more than 2 GB at once. On
|
||||||
Windows, the size is truncted to INT_MAX. As any call to os.read(), the OS
|
Windows, the size is truncted to INT_MAX. As any call to os.read(), the OS
|
||||||
|
|
|
@ -619,16 +619,17 @@ get_category(PyObject *message, PyObject *category)
|
||||||
|
|
||||||
if (rc == 1)
|
if (rc == 1)
|
||||||
category = (PyObject*)message->ob_type;
|
category = (PyObject*)message->ob_type;
|
||||||
else if (category == NULL)
|
else if (category == NULL || category == Py_None)
|
||||||
category = PyExc_UserWarning;
|
category = PyExc_UserWarning;
|
||||||
|
|
||||||
/* Validate category. */
|
/* Validate category. */
|
||||||
rc = PyObject_IsSubclass(category, PyExc_Warning);
|
rc = PyObject_IsSubclass(category, PyExc_Warning);
|
||||||
if (rc == -1)
|
/* category is not a subclass of PyExc_Warning or
|
||||||
return NULL;
|
PyObject_IsSubclass raised an error */
|
||||||
if (rc == 0) {
|
if (rc == -1 || rc == 0) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"category is not a subclass of Warning");
|
"category must be a Warning subclass, not '%s'",
|
||||||
|
Py_TYPE(category)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue