8000 gh-121163: Add "all" as an valid alias for "always" in warnings.simpl… · python/cpython@1a84bdc · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a84bdc

Browse files
authored
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()``.
1 parent 2a455bb commit 1a84bdc

File tree

5 files changed

+49
-43
lines changed

5 files changed

+49
-43
lines changed

Doc/library/warnings.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ the disposition of the match. Each entry is a tuple of the form (*action*,
145145
+---------------+----------------------------------------------+
146146
| ``"always"`` | always print matching warnings |
147147
+---------------+----------------------------------------------+
148+
| ``"all"`` | alias to "always" |
149+
+---------------+----------------------------------------------+
148150
| ``"module"`` | print the first occurrence of matching |
149151
| | warnings for each module where the warning |
150152
| | is issued (regardless of line number) |

Lib/test/test_warnings/__init__.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -155,40 +155,42 @@ def f():
155155
f()
156156
self.assertEqual(len(w), 1)
157157

158-
def test_always(self):
159-
with original_warnings.catch_warnings(record=True,
160-
module=self.module) as w:
161-
self.module.resetwarnings()
162-
self.module.filterwarnings("always", category=UserWarning)
163-
message = "FilterTests.test_always"
164-
def f():
165-
self.module.warn(message, UserWarning)
166-
f()
167-
self.assertEqual(len(w), 1)
168-
self.assertEqual(w[-1].message.args[0], message)
169-
f()
170-
self.assertEqual(len(w), 2)
171-
self.assertEqual(w[-1].message.args[0], message)
158+
def test_always_and_all(self):
159+
for mode in {"always", "all"}:
160+
with original_warnings.catch_warnings(record=True,
161+
module=self.module) as w:
162+
self.module.resetwarnings()
163+
self.module.filterwarnings(mode, category=UserWarning)
164+
message = "FilterTests.test_always_and_all"
165+
def f():
166+
self.module.warn(message, UserWarning)
167+
f()
168+
self.assertEqual(len(w), 1)
169+
self.assertEqual(w[-1].message.args[0], message)
170+
f()
171+
self.assertEqual(len(w), 2)
172+
self.assertEqual(w[-1].message.args[0], message)
172173

173-
def test_always_after_default(self):
174-
with original_warnings.catch_warnings(record=True,
175-
module=self.module) as w:
176-
self.module.resetwarnings()
177-
message = "FilterTests.test_always_after_ignore"
178-
def f():
179-
self.module.warn(message, UserWarning)
180-
f()
181-
self.assertEqual(len(w), 1)
182-
self.assertEqual(w[-1].message.args[0], message)
183-
f()
184-
self.assertEqual(len(w), 1)
185-
self.module.filterwarnings("always", category=UserWarning)
186-
f()
187-
self.assertEqual(len(w), 2)
188-
self.assertEqual(w[-1].message.args[0], message)
189-
f()
190-
self.assertEqual(len(w), 3)
191-
self.assertEqual(w[-1].message.args[0], message)
174+
def test_always_and_all_after_default(self):
175+
for mode in {"always", "all"}:
176+
with original_warnings.catch_warnings(record=True,
177+
module=self.module) as w:
178+
self.module.resetwarnings()
179+
message = "FilterTests.test_always_and_all_after_ignore"
180+
def f():
181+
self.module.warn(message, UserWarning)
182+
f()
183+
self.assertEqual(len(w), 1)
184+
self.assertEqual(w[-1].message.args[0], message)
185+
f()
186+
self.assertEqual(len(w), 1)
187+
self.module.filterwarnings(mode, category=UserWarning)
188+
f()
189+
self.assertEqual(len(w), 2)
190+
self.assertEqual(w[-1].message.args[0], message)
191+
f()
192+
self.assertEqual(len(w), 3)
193+
self.assertEqual(w[-1].message.args[0], message)
192194

193195
def test_default(self):
194196
with original_warnings.catch_warnings(record=True,

Lib/warnings.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
132132
append=False):
133133
"""Insert an entry into the list of warnings filters (at the front).
134134
135-
'action' -- one of "error", "ignore", "always", "default", "module",
135+
'action' -- one of "error", "ignore", "always", "all", "default", "module",
136136
or "once"
137137
'message' -- a regex that the warning message must match
138138
'category' -- a class that the warning must be a subclass of
139139
'module' -- a regex that the module name must match
140140
'lineno' -- an integer line number, 0 matches all warnings
141141
'append' -- if true, append to the list of filters
142142
"""
143-
if action not in {"error", "ignore", "always", "default", "module", "once"}:
143+
if action not in {"error", "ignore", "always", "all", "default", "module", "once"}:
144144
raise ValueError(f"invalid action: {action!r}")
145145
if not isinstance(message, str):
146146
raise TypeError("message must be a string")
@@ -171,13 +171,13 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
171171
"""Insert a simple entry into the list of warnings filters (at the front).
172172
173173
A simple filter matches all modules and messages.
174-
'action' -- one of "error", "ignore", "always", "default", "module",
174+
'action' -- one of "error", "ignore", "always", "all", "default", "module",
175175
or "once"
176176
'category' -- a class that the warning must be a subclass of
177177
'lineno' -- an integer line number, 0 matches all warnings
178178
'append' -- if true, append to the list of filters
179179
"""
180-
if action not in {"error", "ignore", "always", "default", "module", "once"}:
180+
if action not in {"error", "ignore", "always", "all", "default", "module", "once"}:
181181
raise ValueError(f"invalid action: {action!r}")
182182
if not isinstance(lineno, int):
183183
raise TypeError("lineno must be an int")
@@ -248,8 +248,7 @@ def _setoption(arg):
248248
def _getaction(action):
249249
if not action:
250250
return "default"
251-
if action == "all": return "always" # Alias
252-
for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
251+
for a in ('default', 'always', 'all', 'ignore', 'module', 'once', 'error'):
253252
if a.startswith(action):
254253
return a
255254
raise _OptionError("invalid action: %r" % (action,))
@@ -397,7 +396,7 @@ def warn_explicit(message, category, filename, lineno,
397396
if onceregistry.get(oncekey):
398397
return
399398
onceregistry[oncekey] = 1
400-
elif action == "always":
399+
elif action in {"always", "all"}:
401400
pass
402401
elif action == "module":
403402
registry[key] = 1
@@ -690,7 +689,7 @@ def extract():
690689

691690
# filters contains a sequence of filter 5-tuples
692691
# The components of the 5-tuple are:
693-
# - an action: error, ignore, always, default, module, or once
692+
# - an action: error, ignore, always, all, default, module, or once
694693
# - a compiled regex that must match the warning message
695694
# - a class representing the warning category
696695
# - a compiled regex that must match the module that is being warned
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add support for ``all`` as an valid ``action`` for :func:`warnings.simplefilter`
2+
and :func:`warnings.filterswarnings`.
3+

Python/_warnings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,9 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,
704704
}
705705

706706
/* Store in the registry that we've been here, *except* when the action
707-
is "always". */
707+
is "always" or "all". */
708708
rc = 0;
709-
if (!_PyUnicode_EqualToASCIIString(action, "always")) {
709+
if (!_PyUnicode_EqualToASCIIString(action, "always") && !_PyUnicode_EqualToASCIIString(action, "all")) {
710710
if (registry != NULL && registry != Py_None &&
711711
PyDict_SetItem(registry, key, Py_True) < 0)
712712
{

0 commit comments

Comments
 (0)
0