Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ def test_once(self):
42)
self.assertEqual(len(w), 0)

def test_once_across_modules(self):
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter("once", UserWarning)
for name in ("module_a", "module_b"):
namespace = {"__name__": name, "warnings": self.module}
exec("warnings.warn('once across modules')\n"
"warnings.warn('once across modules')", namespace)
self.assertEqual(len(w), 1)

def test_filter_module(self):
MS_WINDOWS = (sys.platform == 'win32')
with self.module.catch_warnings(record=True) as w:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the ``"once"`` warning filter to suppress repeated warnings across
modules, matching the documented behavior and the pure-Python implementation.
8 changes: 3 additions & 5 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,11 +873,9 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,
}

if (_PyUnicode_EqualToASCIIString(action, "once")) {
if (registry == NULL || registry == Py_None) {
registry = get_once_registry(interp);
if (registry == NULL)
goto cleanup;
}
registry = get_once_registry(interp);
if (registry == NULL)
goto cleanup;
/* WarningsState.once_registry[(text, category)] = 1 */
rc = update_registry(interp, registry, text, category, 0);
}
Expand Down
Loading