Skip to content

Commit 022e4b3

Browse files
committed
Added testcase and refactored warning logic for debug
1 parent 4edb4c7 commit 022e4b3

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

Lib/_py_warnings.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,18 @@ def _formatwarnmsg(msg):
251251
return _wm._formatwarnmsg_impl(msg)
252252

253253
def _valid_warning_category(category):
254-
"""Return True if category is a Warning subclass or tuple of such."""
254+
"""
255+
Return True if category is a Warning subclass or tuple of such.
256+
Always perform class checks; only perform tuple iteration in debug mode.
257+
"""
258+
if isinstance(category, type) and issubclass(category, Warning):
259+
return True
255260
if isinstance(category, tuple):
256-
return all(isinstance(c, type) and issubclass(c, Warning) for c in category)
257-
return isinstance(category, type) and issubclass(category, Warning)
261+
if __debug__:
262+
return all(isinstance(c, type) and issubclass(c, Warning)
263+
for c in category)
264+
return True
265+
return False
258266

259267
def filterwarnings(action, message="", category=Warning, module="", lineno=0,
260268
append=False):

Lib/test/test_warnings/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,33 @@ def test_argument_validation(self):
398398
with self.assertRaises(ValueError):
399399
self.module.simplefilter('ignore', lineno=-1)
400400

401+
def test_invalid_category_types(self):
402+
with self.assertRaises(TypeError):
403+
self.module.filterwarnings("ignore", category="notawarning")
404+
with self.assertRaises(TypeError):
405+
self.module.filterwarnings("ignore", category=123)
406+
with self.assertRaises(TypeError):
407+
self.module.filterwarnings("ignore", category=17.02)
408+
with self.assertRaises(TypeError):
409+
self.module.filterwarnings("ignore", category=True)
410+
with self.assertRaises(TypeError):
411+
self.module.filterwarnings(
412+
"ignore", category=(UserWarning, 17)
413+
)
414+
415+
with self.assertRaises(TypeError):
416+
self.module.simplefilter("ignore", category="notawarning")
417+
with self.assertRaises(TypeError):
418+
self.module.simplefilter("ignore", category=123)
419+
with self.assertRaises(TypeError):
420+
self.module.filterwarnings("ignore", category=17.02)
421+
with self.assertRaises(TypeError):
422+
self.module.filterwarnings("ignore", category=True)
423+
with self.assertRaises(TypeError):
424+
self.module.simplefilter(
425+
"ignore", category=(UserWarning, 'abc')
426+
)
427+
401428
def test_catchwarnings_with_simplefilter_ignore(self):
402429
with self.module.catch_warnings(module=self.module):
403430
self.module.resetwarnings()

Misc/ACKS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Emmanuel Arias
7474
Alicia Arlen
7575
Jeffrey Armstrong
7676
Justin Turner Arthur
77+
Hasrat Ali Arzoo
7778
Jason Asbahr
7879
David Ascher
7980
Ammar Askar
@@ -2149,6 +2150,5 @@ Jelle Zijlstra
21492150
Gennadiy Zlobin
21502151
Doug Zongker
21512152
Peter Åstrand
2152-
Hasrat Ali Arzoo
21532153

21542154
(Entries should be added in rough alphabetical order by last names)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added validation for the ``category`` argument in
2+
``warnings.filterwarnings()`` and ``warnings.simplefilter()``

0 commit comments

Comments
 (0)