Skip to content

Commit 42b50d1

Browse files
Add new tests for stack effect (ported from python#5076).
1 parent 4ca9831 commit 42b50d1

File tree

1 file changed

+69
-10
lines changed

1 file changed

+69
-10
lines changed

Lib/test/test_compile.py

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -714,17 +714,30 @@ class TestStackSizeStability(unittest.TestCase):
714714
# Check that repeating certain snippets doesn't increase the stack size
715715
# beyond what a single snippet requires.
716716

717-
def check_stack_size(self, snippet):
718-
sizes = []
719-
for i in range(2, 5):
717+
def check_stack_size(self, snippet, async_=False):
718+
def compile_snippet(i):
720719
ns = {}
721-
code = """def f():\n""" + i * snippet
722-
code = compile(code, "<foo>", "exec")
720+
script = """def func():\n""" + i * snippet
721+
if async_:
722+
script = "async " + script
723+
code = compile(script, "<script>", "exec")
723724
exec(code, ns, ns)
724-
sizes.append(ns['f'].__code__.co_stacksize)
725+
return ns['func'].__code__
725726

726-
self.assertEqual(len(set(sizes)), 1,
727-
"stack sizes diverge with # of consecutive snippets: %s" % (sizes,))
727+
sizes = [compile_snippet(i).co_stacksize for i in range(2, 5)]
728+
if len(set(sizes)) != 1:
729+
import dis, io
730+
out = io.StringIO()
731+
dis.dis(compile_snippet(1), file=out)
732+
self.fail("stack sizes diverge with # of consecutive snippets: "
733+
"%s\n%s\n%s" % (sizes, snippet, out.getvalue()))
734+
735+
def test_if(self):
736+
snippet = """
737+
if x:
738+
a
739+
"""
740+
self.check_stack_size(snippet)
728741

729742
def test_if_else(self):
730743
snippet = """
@@ -776,13 +789,36 @@ def test_try_except_as(self):
776789

777790
def test_try_finally(self):
778791
snippet = """
779-
try:
792+
try:
793+
a
794+
finally:
795+
b
796+
"""
797+
self.check_stack_size(snippet)
798+
799+
def test_with(self):
800+
snippet = """
801+
with x as y:
780802
a
781-
finally:
803+
"""
804+
self.check_stack_size(snippet)
805+
806+
def test_while_else(self):
807+
snippet = """
808+
while x:
809+
a
810+
else:
782811
b
783812
"""
784813
self.check_stack_size(snippet)
785814

815+
def test_for(self):
816+
snippet = """
817+
for x in y:
818+
a
819+
"""
820+
self.check_stack_size(snippet)
821+
786822
def test_for_else(self):
787823
snippet = """
788824
for x in y:
@@ -806,6 +842,29 @@ def test_for_break_continue(self):
806842
"""
807843
self.check_stack_size(snippet)
808844

845+
def test_async_with(self):
846+
snippet = """
847+
async with x as y:
848+
a
849+
"""
850+
self.check_stack_size(snippet, async_=True)
851+
852+
def test_async_for(self):
853+
snippet = """
854+
async for x in y:
855+
a
856+
"""
857+
self.check_stack_size(snippet, async_=True)
858+
859+
def test_async_for_else(self):
860+
snippet = """
861+
async for x in y:
862+
a
863+
else:
864+
b
865+
"""
866+
self.check_stack_size(snippet, async_=True)
867+
809868

810869
if __name__ == "__main__":
811870
unittest.main()

0 commit comments

Comments
 (0)