From 119f8c677bfeb9e0eafb5595749d22781efc9ad4 Mon Sep 17 00:00:00 2001 From: hasrat17 Date: Sat, 5 Jul 2025 01:12:26 +0530 Subject: [PATCH 1/2] Fix: Add type check for category in warning.simplefilter & warning.filterwarnings --- Lib/warnings.py | 8 ++++++-- Misc/ACKS | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/warnings.py b/Lib/warnings.py index 3bfba9749abe33..46a0bb31e4f83a 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -145,8 +145,9 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0, 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" + if not (isinstance(category, type) and issubclass(category, Warning)): + raise TypeError("category must be a Warning subclass, " + "not '{:s}'".format(type(category).__name__)) assert isinstance(module, str), "module must be a string" assert isinstance(lineno, int) and lineno >= 0, \ "lineno must be an int >= 0" @@ -177,6 +178,9 @@ def simplefilter(action, category=Warning, lineno=0, append=False): """ assert action in ("error", "ignore", "always", "default", "module", "once"), "invalid action: %r" % (action,) + if not (isinstance(category, type) and issubclass(category, Warning)): + raise TypeError("category must be a Warning subclass, " + "not '{:s}'".format(type(category).__name__)) assert isinstance(lineno, int) and lineno >= 0, \ "lineno must be an int >= 0" _add_filter(action, None, category, None, lineno, append=append) diff --git a/Misc/ACKS b/Misc/ACKS index a6e63a991f9288..6da74cdb5d645e 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -2093,5 +2093,6 @@ Jelle Zijlstra Gennadiy Zlobin Doug Zongker Peter Åstrand +Hasrat Ali Arzoo (Entries should be added in rough alphabetical order by last names) From 0ba005c3bc7e7632b7eba16d503c5810d8dbaced Mon Sep 17 00:00:00 2001 From: hasrat17 Date: Sat, 5 Jul 2025 02:16:52 +0530 Subject: [PATCH 2/2] Handled tuple of warning subclass in category --- Lib/warnings.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/warnings.py b/Lib/warnings.py index 46a0bb31e4f83a..277257548df5ff 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -130,6 +130,12 @@ def _formatwarnmsg(msg): msg.filename, msg.lineno, msg.line) return _formatwarnmsg_impl(msg) +def _valid_warning_category(category): + """Return True if category is a Warning subclass or tuple of such.""" + if isinstance(category, tuple): + return all(isinstance(c, type) and issubclass(c, Warning) for c in category) + return isinstance(category, type) and issubclass(category, Warning) + def filterwarnings(action, message="", category=Warning, module="", lineno=0, append=False): """Insert an entry into the list of warnings filters (at the front). @@ -145,7 +151,7 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0, assert action in ("error", "ignore", "always", "default", "module", "once"), "invalid action: %r" % (action,) assert isinstance(message, str), "message must be a string" - if not (isinstance(category, type) and issubclass(category, Warning)): + if not _valid_warning_category(category): raise TypeError("category must be a Warning subclass, " "not '{:s}'".format(type(category).__name__)) assert isinstance(module, str), "module must be a string" @@ -178,7 +184,7 @@ def simplefilter(action, category=Warning, lineno=0, append=False): """ assert action in ("error", "ignore", "always", "default", "module", "once"), "invalid action: %r" % (action,) - if not (isinstance(category, type) and issubclass(category, Warning)): + if not _valid_warning_category(category): raise TypeError("category must be a Warning subclass, " "not '{:s}'".format(type(category).__name__)) assert isinstance(lineno, int) and lineno >= 0, \ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy