Skip to content

Commit 00eb17c

Browse files
committed
Update _valid_warning_category and test for debug mode
1 parent 656ec39 commit 00eb17c

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

Lib/_py_warnings.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ def _valid_warning_category(category):
255255
Return True if category is a Warning subclass or tuple of such.
256256
Always perform class checks; only perform tuple iteration in debug mode.
257257
"""
258+
if not __debug__:
259+
return True
258260
if isinstance(category, type) and issubclass(category, Warning):
259261
return True
260262
if isinstance(category, tuple):
261-
if __debug__:
262-
return all(isinstance(c, type) and issubclass(c, Warning)
263-
for c in category)
264-
return True
263+
return all(isinstance(c, type) and issubclass(c, Warning)
264+
for c in category)
265265
return False
266266

267267
def filterwarnings(action, message="", category=Warning, module="", lineno=0,

Lib/test/test_warnings/__init__.py

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

401+
# these tests fail if python is run with -O, so check __debug__
402+
@unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
401403
def test_invalid_category_types(self):
402404
with self.assertRaises(TypeError):
403405
self.module.filterwarnings("ignore", category="notawarning")

0 commit comments

Comments
 (0)