@@ -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
16451666class StdPrinterTests (EmbeddingTestsMixin , unittest .TestCase ):
16461667 # Test PyStdPrinter_Type which is used by _PySys_SetPreliminaryStderr():
0 commit comments