Skip to content

bpo-27535: Fix memory leak with warnings ignore #4489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged
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/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,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__), ['version'])

def test_ignore_after_default(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 @@ -364,7 +364,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 @@ -525,16 +525,21 @@ warn_explicit(PyObject *category, PyObject *message,
goto cleanup;
}

if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
goto return_none;
}

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

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