]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-27535: Fix memory leak with warnings ignore (#4489)
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 27 Nov 2017 15:57:07 +0000 (16:57 +0100)
committerGitHub <noreply@github.com>
Mon, 27 Nov 2017 15:57:07 +0000 (16:57 +0100)
The warnings module doesn't leak memory anymore in the hidden
warnings registry for the "ignore" action of warnings filters.

The warn_explicit() function doesn't add the warning key to the
registry anymore for the "ignore" action.

Lib/test/test_warnings/__init__.py
Lib/warnings.py
Misc/NEWS.d/next/Library/2017-11-21-16-05-35.bpo-27535.JLhcNz.rst [new file with mode: 0644]
Python/_warnings.c

index e007dc7e39e030277c5b0df62f7979bc3d501f69..f2fdaa5386b7d41236b6a7a5d90e0d6ff133b3a5 100644 (file)
@@ -125,6 +125,7 @@ class FilterTests(BaseTest):
             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,
index c4bb22ec92a6e53aa1b13f7b783c1f01bdeae8e4..d7ea057a688ab12e594fdd58490de456940cf0dc 100644 (file)
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2017-11-21-16-05-35.bpo-27535.JLhcNz.rst b/Misc/NEWS.d/next/Library/2017-11-21-16-05-35.bpo-27535.JLhcNz.rst
new file mode 100644 (file)
index 0000000..51bcfb7
--- /dev/null
@@ -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.
index 27f5b813a7ef99aa52dc5fe6188f2ed2fe88111a..29370369e25d873a4811c6871fa7c36bf57351fc 100644 (file)
@@ -528,16 +528,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)