Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c9c178f

Browse filesBrowse files
authored
bpo-1635741: test_embed cheks that Python does not leak (GH-31555)
1 parent 38f331d commit c9c178f
Copy full SHA for c9c178f

File tree

2 files changed

+22
-1
lines changed
Filter options

2 files changed

+22
-1
lines changed

‎Lib/test/test_cmd_line.py

Copy file name to clipboardExpand all lines: Lib/test/test_cmd_line.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def run_python(*args):
118118
self.assertEqual(out.rstrip(), b'{}')
119119
self.assertEqual(err, b'')
120120
# "-X showrefcount" shows the refcount, but only in debug builds
121-
rc, out, err = run_python('-X', 'showrefcount', '-c', code)
121+
rc, out, err = run_python('-I', '-X', 'showrefcount', '-c', code)
122122
self.assertEqual(out.rstrip(), b"{'showrefcount': True}")
123123
if Py_DEBUG:
124124
# bpo-46417: Tolerate negative reference count which can occur

‎Lib/test/test_embed.py

Copy file name to clipboardExpand all lines: Lib/test/test_embed.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,27 @@ def test_frozenmain(self):
16411641
""").lstrip()
16421642
self.assertEqual(out, expected)
16431643

1644+
@unittest.skipUnless(hasattr(sys, 'gettotalrefcount'),
1645+
'-X showrefcount requires a Python debug build')
1646+
def test_no_memleak(self):
1647+
# bpo-1635741: Python must release all memory at exit
1648+
cmd = [sys.executable, "-I", "-X", "showrefcount", "-c", "pass"]
1649+
proc = subprocess.run(cmd,
1650+
stdout=subprocess.PIPE,
1651+
stderr=subprocess.STDOUT,
1652+
text=True)
1653+
self.assertEqual(proc.returncode, 0)
1654+
out = proc.stdout.rstrip()
1655+
match = re.match(r'^\[(-?\d+) refs, (-?\d+) blocks\]', out)
1656+
if not match:
1657+
self.fail(f"unexpected output: {out!a}")
1658+
refs = int(match.group(1))
1659+
blocks = int(match.group(2))
1660+
# bpo-46417: Tolerate negative reference count which can occur because
1661+
# of bugs in C extensions. It is only wrong if it's greater than 0.
1662+
self.assertLessEqual(refs, 0, out)
1663+
self.assertEqual(blocks, 0, out)
1664+
16441665

16451666
class StdPrinterTests(EmbeddingTestsMixin, unittest.TestCase):
16461667
# Test PyStdPrinter_Type which is used by _PySys_SetPreliminaryStderr():

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.