]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46857: Fix refleak in OSError INIT_ALIAS() (GH-31594)
authorVictor Stinner <vstinner@python.org>
Sat, 26 Feb 2022 23:28:24 +0000 (00:28 +0100)
committerGitHub <noreply@github.com>
Sat, 26 Feb 2022 23:28:24 +0000 (00:28 +0100)
_Py_GetRefTotal() no longer decrements _PySet_Dummy refcount.

Lib/test/test_embed.py
Objects/exceptions.c
Objects/object.c

index 450bbec7005a8e798c9d4ae15f63297a94e68ecc..80b9674c1c2582f092a7ca8c453e4e741e2f2843 100644 (file)
@@ -1657,15 +1657,11 @@ class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
             self.fail(f"unexpected output: {out!a}")
         refs = int(match.group(1))
         blocks = int(match.group(2))
+        self.assertEqual(refs, 0, out)
         if not MS_WINDOWS:
-            # bpo-46417: Tolerate negative reference count which can occur because
-            # of bugs in C extensions. It is only wrong if it's greater than 0.
-            self.assertLessEqual(refs, 0, out)
             self.assertEqual(blocks, 0, out)
         else:
-            # bpo-46857: on Windows, Python still leaks 1 reference and 1
-            # memory block at exit.
-            self.assertLessEqual(refs, 1, out)
+            # bpo-46857: on Windows, Python still leaks 1 memory block at exit
             self.assertIn(blocks, (0, 1), out)
 
 
index 977dce502038278844c61329c5b87fd7a340a455..9dbbd40f1de1c4a8cbff144179d51d8554c637f1 100644 (file)
 
 
 /* Compatibility aliases */
-PyObject *PyExc_EnvironmentError = NULL;
-PyObject *PyExc_IOError = NULL;
+PyObject *PyExc_EnvironmentError = NULL;  // borrowed ref
+PyObject *PyExc_IOError = NULL;  // borrowed ref
 #ifdef MS_WINDOWS
-PyObject *PyExc_WindowsError = NULL;
+PyObject *PyExc_WindowsError = NULL;  // borrowed ref
 #endif
 
 
@@ -3647,10 +3647,8 @@ _PyBuiltins_AddExceptions(PyObject *bltinmod)
 
 #define INIT_ALIAS(NAME, TYPE) \
     do { \
-        Py_INCREF(PyExc_ ## TYPE); \
-        Py_XDECREF(PyExc_ ## NAME); \
         PyExc_ ## NAME = PyExc_ ## TYPE; \
-        if (PyDict_SetItemString(mod_dict, # NAME, PyExc_ ## NAME)) { \
+        if (PyDict_SetItemString(mod_dict, # NAME, PyExc_ ## TYPE)) { \
             return -1; \
         } \
     } while (0)
index 77a457223764a7dce98749d3c59bac625bca634e..38919ff47a94a822a4cef2f6a7caa23626c97f27 100644 (file)
@@ -61,12 +61,7 @@ Py_ssize_t _Py_RefTotal;
 Py_ssize_t
 _Py_GetRefTotal(void)
 {
-    PyObject *o;
-    Py_ssize_t total = _Py_RefTotal;
-    o = _PySet_Dummy;
-    if (o != NULL)
-        total -= Py_REFCNT(o);
-    return total;
+    return _Py_RefTotal;
 }
 
 void