aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorRémi Lapeyre <remi.lapeyre@lenstra.fr>2023-12-01 12:17:47 +0100
committerGitHub <noreply@github.com>2023-12-01 11:17:47 +0000
commita65a3d4806a4087f229b5ab6ab28d3e0b0a2d840 (patch)
treebcea132374d4eb92da9a8aac6afdd79cddea6588 /Lib/warnings.py
parent847e4fe0e81f0e6e54ef52a9be63e3fb74b0779a (diff)
downloadcpython-a65a3d4806a4087f229b5ab6ab28d3e0b0a2d840.tar.gz
cpython-a65a3d4806a4087f229b5ab6ab28d3e0b0a2d840.zip
bpo-39912: Raise appropriate exceptions in filterwarnings() and simplefilter() (GH-18878)
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py30
1 files changed, 18 insertions, 12 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 924f872172d..b8ff078569d 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -139,14 +139,18 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
'lineno' -- an integer line number, 0 matches all warnings
'append' -- if true, append to the list of filters
"""
- assert action in ("error", "ignore", "always", "default", "module",
- "once"), "invalid action: %r" % (action,)
- assert isinstance(message, str), "message must be a string"
- assert isinstance(category, type), "category must be a class"
- assert issubclass(category, Warning), "category must be a Warning subclass"
- assert isinstance(module, str), "module must be a string"
- assert isinstance(lineno, int) and lineno >= 0, \
- "lineno must be an int >= 0"
+ if action not in {"error", "ignore", "always", "default", "module", "once"}:
+ raise ValueError(f"invalid action: {action!r}")
+ if not isinstance(message, str):
+ raise TypeError("message must be a string")
+ if not isinstance(category, type) or not issubclass(category, Warning):
+ raise TypeError("category must be a Warning subclass")
+ if not isinstance(module, str):
+ raise TypeError("module must be a string")
+ if not isinstance(lineno, int):
+ raise TypeError("lineno must be an int")
+ if lineno < 0:
+ raise ValueError("lineno must be an int >= 0")
if message or module:
import re
@@ -172,10 +176,12 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
'lineno' -- an integer line number, 0 matches all warnings
'append' -- if true, append to the list of filters
"""
- assert action in ("error", "ignore", "always", "default", "module",
- "once"), "invalid action: %r" % (action,)
- assert isinstance(lineno, int) and lineno >= 0, \
- "lineno must be an int >= 0"
+ if action not in {"error", "ignore", "always", "default", "module", "once"}:
+ raise ValueError(f"invalid action: {action!r}")
+ if not isinstance(lineno, int):
+ raise TypeError("lineno must be an int")
+ if lineno < 0:
+ raise ValueError("lineno must be an int >= 0")
_add_filter(action, None, category, None, lineno, append=append)
def _add_filter(*item, append):