Skip to content

Commit 9f89608

Browse files
committed
add regression tests
Only tests for functions that effectively leak on error paths are added.
1 parent 9276481 commit 9f89608

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Lib/test/test_compile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,21 @@ def test_compile_filename(self):
651651
compile('pass', filename, 'exec')
652652
self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
653653

654+
def test_compile_filename_refleak(self):
655+
# Regression tests for reference leak in PyUnicode_FSDecoder.
656+
# See https://github.com/python/cpython/issues/139748.
657+
mortal_str = 'this is a mortal string'
658+
# check error path when 'mode' AC conversion failed
659+
self.assertRaises(TypeError, compile, b'', mortal_str, mode=1234)
660+
# check error path when 'optimize' AC conversion failed
661+
self.assertRaises(OverflowError, compile, b'', mortal_str,
662+
'exec', optimize=1 << 1000)
663+
# check error path when 'dont_inherit' AC conversion failed
664+
class EvilBool:
665+
def __bool__(self): raise ValueError
666+
self.assertRaises(ValueError, compile, b'', mortal_str,
667+
'exec', dont_inherit=EvilBool())
668+
654669
@support.cpython_only
655670
def test_same_filename_used(self):
656671
s = """def f(): pass\ndef g(): pass"""

Lib/test/test_symtable.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,13 @@ def test_nested_genexpr(self):
579579
self.assertEqual(sorted(st.get_identifiers()), [".0", "y"])
580580
self.assertEqual(st.get_children(), [])
581581

582+
def test__symtable_refleak(self):
583+
# Regression test for reference leak in PyUnicode_FSDecoder.
584+
# See https://github.com/python/cpython/issues/139748.
585+
mortal_str = 'this is a mortal string'
586+
# check error path when 'compile_type' AC conversion failed
587+
self.assertRaises(TypeError, symtable.symtable, '', mortal_str, 1)
588+
582589

583590
class ComprehensionTests(unittest.TestCase):
584591
def get_identifiers_recursive(self, st, res):

0 commit comments

Comments
 (0)