mirror of
https://github.com/python/cpython.git
synced 2025-09-01 22:47:59 +00:00
gh-121163: Add "all" as an valid alias for "always" in warnings.simplefilter() (#121164)
Add support for ``all`` as an valid alias for ``always`` in ``warnings.simplefilter()`` and ``warnings.filterswarnings()``.
This commit is contained in:
parent
2a455bbe8f
commit
1a84bdc237
5 changed files with 49 additions and 43 deletions
|
@ -145,6 +145,8 @@ the disposition of the match. Each entry is a tuple of the form (*action*,
|
||||||
+---------------+----------------------------------------------+
|
+---------------+----------------------------------------------+
|
||||||
| ``"always"`` | always print matching warnings |
|
| ``"always"`` | always print matching warnings |
|
||||||
+---------------+----------------------------------------------+
|
+---------------+----------------------------------------------+
|
||||||
|
| ``"all"`` | alias to "always" |
|
||||||
|
+---------------+----------------------------------------------+
|
||||||
| ``"module"`` | print the first occurrence of matching |
|
| ``"module"`` | print the first occurrence of matching |
|
||||||
| | warnings for each module where the warning |
|
| | warnings for each module where the warning |
|
||||||
| | is issued (regardless of line number) |
|
| | is issued (regardless of line number) |
|
||||||
|
|
|
@ -155,12 +155,13 @@ class FilterTests(BaseTest):
|
||||||
f()
|
f()
|
||||||
self.assertEqual(len(w), 1)
|
self.assertEqual(len(w), 1)
|
||||||
|
|
||||||
def test_always(self):
|
def test_always_and_all(self):
|
||||||
|
for mode in {"always", "all"}:
|
||||||
with original_warnings.catch_warnings(record=True,
|
with original_warnings.catch_warnings(record=True,
|
||||||
module=self.module) as w:
|
module=self.module) as w:
|
||||||
self.module.resetwarnings()
|
self.module.resetwarnings()
|
||||||
self.module.filterwarnings("always", category=UserWarning)
|
self.module.filterwarnings(mode, category=UserWarning)
|
||||||
message = "FilterTests.test_always"
|
message = "FilterTests.test_always_and_all"
|
||||||
def f():
|
def f():
|
||||||
self.module.warn(message, UserWarning)
|
self.module.warn(message, UserWarning)
|
||||||
f()
|
f()
|
||||||
|
@ -170,11 +171,12 @@ class FilterTests(BaseTest):
|
||||||
self.assertEqual(len(w), 2)
|
self.assertEqual(len(w), 2)
|
||||||
self.assertEqual(w[-1].message.args[0], message)
|
self.assertEqual(w[-1].message.args[0], message)
|
||||||
|
|
||||||
def test_always_after_default(self):
|
def test_always_and_all_after_default(self):
|
||||||
|
for mode in {"always", "all"}:
|
||||||
with original_warnings.catch_warnings(record=True,
|
with original_warnings.catch_warnings(record=True,
|
||||||
module=self.module) as w:
|
module=self.module) as w:
|
||||||
self.module.resetwarnings()
|
self.module.resetwarnings()
|
||||||
message = "FilterTests.test_always_after_ignore"
|
message = "FilterTests.test_always_and_all_after_ignore"
|
||||||
def f():
|
def f():
|
||||||
self.module.warn(message, UserWarning)
|
self.module.warn(message, UserWarning)
|
||||||
f()
|
f()
|
||||||
|
@ -182,7 +184,7 @@ class FilterTests(BaseTest):
|
||||||
self.assertEqual(w[-1].message.args[0], message)
|
self.assertEqual(w[-1].message.args[0], message)
|
||||||
f()
|
f()
|
||||||
self.assertEqual(len(w), 1)
|
self.assertEqual(len(w), 1)
|
||||||
self.module.filterwarnings("always", category=UserWarning)
|
self.module.filterwarnings(mode, category=UserWarning)
|
||||||
f()
|
f()
|
||||||
self.assertEqual(len(w), 2)
|
self.assertEqual(len(w), 2)
|
||||||
self.assertEqual(w[-1].message.args[0], message)
|
self.assertEqual(w[-1].message.args[0], message)
|
||||||
|
|
|
@ -132,7 +132,7 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
|
||||||
append=False):
|
append=False):
|
||||||
"""Insert an entry into the list of warnings filters (at the front).
|
"""Insert an entry into the list of warnings filters (at the front).
|
||||||
|
|
||||||
'action' -- one of "error", "ignore", "always", "default", "module",
|
'action' -- one of "error", "ignore", "always", "all", "default", "module",
|
||||||
or "once"
|
or "once"
|
||||||
'message' -- a regex that the warning message must match
|
'message' -- a regex that the warning message must match
|
||||||
'category' -- a class that the warning must be a subclass of
|
'category' -- a class that the warning must be a subclass of
|
||||||
|
@ -140,7 +140,7 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
|
||||||
'lineno' -- an integer line number, 0 matches all warnings
|
'lineno' -- an integer line number, 0 matches all warnings
|
||||||
'append' -- if true, append to the list of filters
|
'append' -- if true, append to the list of filters
|
||||||
"""
|
"""
|
||||||
if action not in {"error", "ignore", "always", "default", "module", "once"}:
|
if action not in {"error", "ignore", "always", "all", "default", "module", "once"}:
|
||||||
raise ValueError(f"invalid action: {action!r}")
|
raise ValueError(f"invalid action: {action!r}")
|
||||||
if not isinstance(message, str):
|
if not isinstance(message, str):
|
||||||
raise TypeError("message must be a string")
|
raise TypeError("message must be a string")
|
||||||
|
@ -171,13 +171,13 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
|
||||||
"""Insert a simple entry into the list of warnings filters (at the front).
|
"""Insert a simple entry into the list of warnings filters (at the front).
|
||||||
|
|
||||||
A simple filter matches all modules and messages.
|
A simple filter matches all modules and messages.
|
||||||
'action' -- one of "error", "ignore", "always", "default", "module",
|
'action' -- one of "error", "ignore", "always", "all", "default", "module",
|
||||||
or "once"
|
or "once"
|
||||||
'category' -- a class that the warning must be a subclass of
|
'category' -- a class that the warning must be a subclass of
|
||||||
'lineno' -- an integer line number, 0 matches all warnings
|
'lineno' -- an integer line number, 0 matches all warnings
|
||||||
'append' -- if true, append to the list of filters
|
'append' -- if true, append to the list of filters
|
||||||
"""
|
"""
|
||||||
if action not in {"error", "ignore", "always", "default", "module", "once"}:
|
if action not in {"error", "ignore", "always", "all", "default", "module", "once"}:
|
||||||
raise ValueError(f"invalid action: {action!r}")
|
raise ValueError(f"invalid action: {action!r}")
|
||||||
if not isinstance(lineno, int):
|
if not isinstance(lineno, int):
|
||||||
raise TypeError("lineno must be an int")
|
raise TypeError("lineno must be an int")
|
||||||
|
@ -248,8 +248,7 @@ def _setoption(arg):
|
||||||
def _getaction(action):
|
def _getaction(action):
|
||||||
if not action:
|
if not action:
|
||||||
return "default"
|
return "default"
|
||||||
if action == "all": return "always" # Alias
|
for a in ('default', 'always', 'all', 'ignore', 'module', 'once', 'error'):
|
||||||
for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
|
|
||||||
if a.startswith(action):
|
if a.startswith(action):
|
||||||
return a
|
return a
|
||||||
raise _OptionError("invalid action: %r" % (action,))
|
raise _OptionError("invalid action: %r" % (action,))
|
||||||
|
@ -397,7 +396,7 @@ def warn_explicit(message, category, filename, lineno,
|
||||||
if onceregistry.get(oncekey):
|
if onceregistry.get(oncekey):
|
||||||
return
|
return
|
||||||
onceregistry[oncekey] = 1
|
onceregistry[oncekey] = 1
|
||||||
elif action == "always":
|
elif action in {"always", "all"}:
|
||||||
pass
|
pass
|
||||||
elif action == "module":
|
elif action == "module":
|
||||||
registry[key] = 1
|
registry[key] = 1
|
||||||
|
@ -690,7 +689,7 @@ def _warn_unawaited_coroutine(coro):
|
||||||
|
|
||||||
# filters contains a sequence of filter 5-tuples
|
# filters contains a sequence of filter 5-tuples
|
||||||
# The components of the 5-tuple are:
|
# The components of the 5-tuple are:
|
||||||
# - an action: error, ignore, always, default, module, or once
|
# - an action: error, ignore, always, all, default, module, or once
|
||||||
# - a compiled regex that must match the warning message
|
# - a compiled regex that must match the warning message
|
||||||
# - a class representing the warning category
|
# - a class representing the warning category
|
||||||
# - a compiled regex that must match the module that is being warned
|
# - a compiled regex that must match the module that is being warned
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Add support for ``all`` as an valid ``action`` for :func:`warnings.simplefilter`
|
||||||
|
and :func:`warnings.filterswarnings`.
|
||||||
|
|
|
@ -704,9 +704,9 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store in the registry that we've been here, *except* when the action
|
/* Store in the registry that we've been here, *except* when the action
|
||||||
is "always". */
|
is "always" or "all". */
|
||||||
rc = 0;
|
rc = 0;
|
||||||
if (!_PyUnicode_EqualToASCIIString(action, "always")) {
|
if (!_PyUnicode_EqualToASCIIString(action, "always") && !_PyUnicode_EqualToASCIIString(action, "all")) {
|
||||||
if (registry != NULL && registry != Py_None &&
|
if (registry != NULL && registry != Py_None &&
|
||||||
PyDict_SetItem(registry, key, Py_True) < 0)
|
PyDict_SetItem(registry, key, Py_True) < 0)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue