@@ -714,17 +714,30 @@ class TestStackSizeStability(unittest.TestCase):
714
714
# Check that repeating certain snippets doesn't increase the stack size
715
715
# beyond what a single snippet requires.
716
716
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 ):
720
719
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" )
723
724
exec (code , ns , ns )
724
- sizes . append ( ns ['f ' ].__code__ . co_stacksize )
725
+ return ns ['func ' ].__code__
725
726
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 )
728
741
729
742
def test_if_else (self ):
730
743
snippet = """
@@ -776,13 +789,36 @@ def test_try_except_as(self):
776
789
777
790
def test_try_finally (self ):
778
791
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:
780
802
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:
782
811
b
783
812
"""
784
813
self .check_stack_size (snippet )
785
814
815
+ def test_for (self ):
816
+ snippet = """
817
+ for x in y:
818
+ a
819
+ """
820
+ self .check_stack_size (snippet )
821
+
786
822
def test_for_else (self ):
787
823
snippet = """
788
824
for x in y:
@@ -806,6 +842,29 @@ def test_for_break_continue(self):
806
842
"""
807
843
self .check_stack_size (snippet )
808
844
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
+
809
868
810
869
if __name__ == "__main__" :
811
870
unittest .main ()
0 commit comments