]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-1635741: test_embed cheks that Python does not leak (GH-31555)
authorVictor Stinner <vstinner@python.org>
Thu, 24 Feb 2022 23:03:20 +0000 (00:03 +0100)
committerGitHub <noreply@github.com>
Thu, 24 Feb 2022 23:03:20 +0000 (00:03 +0100)
Lib/test/test_cmd_line.py
Lib/test/test_embed.py

index 352109ed4b2ff1821a7d097be2962838eba42545..1521b5b50ca249351ae5867b5faa009819c690a7 100644 (file)
@@ -118,7 +118,7 @@ class CmdLineTest(unittest.TestCase):
         self.assertEqual(out.rstrip(), b'{}')
         self.assertEqual(err, b'')
         # "-X showrefcount" shows the refcount, but only in debug builds
-        rc, out, err = run_python('-X', 'showrefcount', '-c', code)
+        rc, out, err = run_python('-I', '-X', 'showrefcount', '-c', code)
         self.assertEqual(out.rstrip(), b"{'showrefcount': True}")
         if Py_DEBUG:
             # bpo-46417: Tolerate negative reference count which can occur
index 15c6b05916f34d61d3979664ff623838054fbb09..e376331063d861c12d6dcad85a806a343b8c8f19 100644 (file)
@@ -1641,6 +1641,27 @@ class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
         """).lstrip()
         self.assertEqual(out, expected)
 
+    @unittest.skipUnless(hasattr(sys, 'gettotalrefcount'),
+                         '-X showrefcount requires a Python debug build')
+    def test_no_memleak(self):
+        # bpo-1635741: Python must release all memory at exit
+        cmd = [sys.executable, "-I", "-X", "showrefcount", "-c", "pass"]
+        proc = subprocess.run(cmd,
+                              stdout=subprocess.PIPE,
+                              stderr=subprocess.STDOUT,
+                              text=True)
+        self.assertEqual(proc.returncode, 0)
+        out = proc.stdout.rstrip()
+        match = re.match(r'^\[(-?\d+) refs, (-?\d+) blocks\]', out)
+        if not match:
+            self.fail(f"unexpected output: {out!a}")
+        refs = int(match.group(1))
+        blocks = int(match.group(2))
+        # 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)
+
 
 class StdPrinterTests(EmbeddingTestsMixin, unittest.TestCase):
     # Test PyStdPrinter_Type which is used by _PySys_SetPreliminaryStderr():