summaryrefslogtreecommitdiffstats
path: root/Lib/test
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 /Lib/test
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 'Lib/test')
-rw-r--r--Lib/test/test_warnings.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
index cf7f747..cd3288b 100644
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -370,6 +370,41 @@ class WarnTests(BaseTest):
with self.assertRaises(ValueError):
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):
module = c_warnings