Skip to content
Closed
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
1 change: 1 addition & 0 deletions Lib/test/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def test_ignore(self):
self.module.filterwarnings("ignore", category=UserWarning)
self.module.warn("FilterTests.test_ignore", UserWarning)
self.assertEqual(len(w), 0)
self.assertEqual(list(__warningregistry__), [])

def test_always(self):
with original_warnings.catch_warnings(record=True,
Expand Down
1 change: 0 additions & 1 deletion Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ def warn_explicit(message, category, filename, lineno,
action = defaultaction
# Early exit actions
if action == "ignore":
registry[key] = 1
return

# Prime the linecache for formatting, in case the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The warnings module doesn't leak memory anymore in the hidden warnings
registry for the "ignore" action of warnings filters. warn_explicit()
function doesn't add the warning key to the registry anymore for the
"ignore" action.
13 changes: 9 additions & 4 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,21 @@ warn_explicit(PyObject *category, PyObject *message,
goto cleanup;
}

if (strcmp(action, "ignore") == 0) {
goto return_none;
}

/* Store in the registry that we've been here, *except* when the action
is "always". */
rc = 0;
if (strcmp(action, "always") != 0) {
if (registry != NULL && registry != Py_None &&
PyDict_SetItem(registry, key, Py_True) < 0)
PyDict_SetItem(registry, key, Py_True) < 0)
{
goto cleanup;
else if (strcmp(action, "ignore") == 0)
goto return_none;
else if (strcmp(action, "once") == 0) {
}

if (strcmp(action, "once") == 0) {
if (registry == NULL || registry == Py_None) {
registry = get_once_registry();
if (registry == NULL)
Expand Down