8
8
from functools import wraps
9
9
import asyncio
10
10
from test .support import import_helper
11
+ import contextlib
11
12
12
13
support .requires_working_socket (module = True )
13
14
@@ -1922,6 +1923,8 @@ def no_jump_without_trace_function():
1922
1923
1923
1924
1924
1925
class JumpTestCase (unittest .TestCase ):
1926
+ unbound_locals = r"assigning None to [0-9]+ unbound local"
1927
+
1925
1928
def setUp (self ):
1926
1929
self .addCleanup (sys .settrace , sys .gettrace ())
1927
1930
sys .settrace (None )
@@ -1933,53 +1936,59 @@ def compare_jump_output(self, expected, received):
1933
1936
"Received: " + repr (received ))
1934
1937
1935
1938
def run_test (self , func , jumpFrom , jumpTo , expected , error = None ,
1936
- event = 'line' , decorated = False ):
1939
+ event = 'line' , decorated = False , warning = None ):
1937
1940
tracer = JumpTracer (func , jumpFrom , jumpTo , event , decorated )
1938
1941
sys .settrace (tracer .trace )
1939
1942
output = []
1940
- if error is None :
1943
+
1944
+ with contextlib .ExitStack () as stack :
1945
+ if error is not None :
1946
+ stack .enter_context (self .assertRaisesRegex (* error ))
1947
+ if warning is not None :
1948
+ stack .enter_context (self .assertWarnsRegex (* warning ))
1941
1949
func (output )
1942
- else :
1943
- with self .assertRaisesRegex (* error ):
1944
- func (output )
1950
+
1945
1951
sys .settrace (None )
1946
1952
self .compare_jump_output (expected , output )
1947
1953
1948
1954
def run_async_test (self , func , jumpFrom , jumpTo , expected , error = None ,
1949
- event = 'line' , decorated = False ):
1955
+ event = 'line' , decorated = False , warning = None ):
1950
1956
tracer = JumpTracer (func , jumpFrom , jumpTo , event , decorated )
1951
1957
sys .settrace (tracer .trace )
1952
1958
output = []
1953
- if error is None :
1959
+
1960
+ with contextlib .ExitStack () as stack :
1961
+ if error is not None :
1962
+ stack .enter_context (self .assertRaisesRegex (* error ))
1963
+ if warning is not None :
1964
+ stack .enter_context (self .assertWarnsRegex (* warning ))
1954
1965
asyncio .run (func (output ))
1955
- else :
1956
- with self .assertRaisesRegex (* error ):
1957
- asyncio .run (func (output ))
1966
+
1958
1967
sys .settrace (None )
1959
1968
asyncio .set_event_loop_policy (None )
1960
1969
self .compare_jump_output (expected , output )
1961
1970
1962
- def jump_test (jumpFrom , jumpTo , expected , error = None , event = 'line' ):
1971
+ def jump_test (jumpFrom , jumpTo , expected , error = None , event = 'line' , warning = None ):
1963
1972
"""Decorator that creates a test that makes a jump
1964
1973
from one place to another in the following code.
1965
1974
"""
1966
1975
def decorator (func ):
1967
1976
@wraps (func )
1968
1977
def test (self ):
1969
1978
self .run_test (func , jumpFrom , jumpTo , expected ,
1970
- error = error , event = event , decorated = True )
1979
+ error = error , event = event , decorated = True , warning = warning )
1971
1980
return test
1972
1981
return decorator
1973
1982
1974
- def async_jump_test (jumpFrom , jumpTo , expected , error = None , event = 'line' ):
1983
+ def async_jump_test (jumpFrom , jumpTo , expected , error = None , event = 'line' , warning = None ):
1975
1984
"""Decorator that creates a test that makes a jump
1976
1985
from one place to another in the following asynchronous code.
1977
1986
"""
1978
1987
def decorator (func ):
1979
1988
@wraps (func )
1980
1989
def test (self ):
1981
1990
self .run_async_test (func , jumpFrom , jumpTo , expected ,
1982
- error = error , event = event , decorated = True )
1991
+ error = error , event = event , decorated = True , warning = warning )
1983
1992
return test
1984
1993
return decorator
1985
1994
@@ -1996,7 +2005,7 @@ def test_jump_simple_backwards(output):
1996
2005
output .append (1 )
1997
2006
output .append (2 )
1998
2007
1999
- @jump_test (3 , 5 , [2 , 5 ])
2008
+ @jump_test (3 , 5 , [2 , 5 ], warning = ( RuntimeWarning , unbound_locals ) )
2000
2009
def test_jump_out_of_block_forwards (output ):
2001
2010
for i in 1 , 2 :
2002
2011
output .append (2 )
@@ -2210,7 +2219,7 @@ def test_jump_within_except_block(output):
2210
2219
output .append (6 )
2211
2220
output .append (7 )
2212
2221
2213
- @jump_test (6 , 1 , [1 , 5 , 1 , 5 ])
2222
+ @jump_test (6 , 1 , [1 , 5 , 1 , 5 ], warning = ( RuntimeWarning , unbound_locals ) )
2214
2223
def test_jump_over_try_except (output ):
2215
2224
output .append (1 )
2216
2225
try :
@@ -2306,15 +2315,15 @@ def test_jump_out_of_complex_nested_blocks(output):
2306
2315
output .append (11 )
2307
2316
output .append (12 )
2308
2317
2309
- @jump_test (3 , 5 , [1 , 2 , 5 ])
2318
+ @jump_test (3 , 5 , [1 , 2 , 5 ], warning = ( RuntimeWarning , unbound_locals ) )
2310
2319
def test_jump_out_of_with_assignment (output ):
2311
2320
output .append (1 )
2312
2321
with tracecontext (output , 2 ) \
2313
2322
as x :
2314
2323
output .append (4 )
2315
2324
output .append (5 )
2316
2325
2317
- @async_jump_test (3 , 5 , [1 , 2 , 5 ])
2326
+ @async_jump_test (3 , 5 , [1 , 2 , 5 ], warning = ( RuntimeWarning , unbound_locals ) )
2318
2327
async def test_jump_out_of_async_with_assignment (output ):
2319
2328
output .append (1 )
2320
2329
async with asynctracecontext (output , 2 ) \
@@ -2350,7 +2359,7 @@ def test_jump_over_break_in_try_finally_block(output):
2350
2359
break
2351
2360
output .append (13 )
2352
2361
2353
- @jump_test (1 , 7 , [7 , 8 ])
2362
+ @jump_test (1 , 7 , [7 , 8 ], warning = ( RuntimeWarning , unbound_locals ) )
2354
2363
def test_jump_over_for_block_before_else (output ):
2355
2364
output .append (1 )
2356
2365
if not output : # always false
@@ -2361,7 +2370,7 @@ def test_jump_over_for_block_before_else(output):
2361
2370
output .append (7 )
2362
2371
output .append (8 )
2363
2372
2364
- @async_jump_test (1 , 7 , [7 , 8 ])
2373
+ @async_jump_test (1 , 7 , [7 , 8 ], warning = ( RuntimeWarning , unbound_locals ) )
2365
2374
async def test_jump_over_async_for_block_before_else (output ):
2366
2375
output .append (1 )
2367
2376
if not output : # always false
@@ -2436,6 +2445,7 @@ def test_no_jump_backwards_into_for_block(output):
2436
2445
output .append (2 )
2437
2446
output .append (3 )
2438
2447
2448
+
2439
2449
@async_jump_test (3 , 2 , [2 , 2 ], (ValueError , "can't jump into the body of a for loop" ))
2440
2450
async def test_no_jump_backwards_into_async_for_block (output ):
2441
2451
async for i in asynciter ([1 , 2 ]):
@@ -2501,7 +2511,7 @@ def test_jump_backwards_into_try_except_block(output):
2501
2511
output .append (6 )
2502
2512
2503
2513
# 'except' with a variable creates an implicit finally block
2504
- @jump_test (5 , 7 , [4 , 7 , 8 ])
2514
+ @jump_test (5 , 7 , [4 , 7 , 8 ], warning = ( RuntimeWarning , unbound_locals ) )
2505
2515
def test_jump_between_except_blocks_2 (output ):
2506
2516
try :
2507
2517
1 / 0
@@ -2664,7 +2674,7 @@ def test_large_function(self):
2664
2674
output.append(x) # line 1007
2665
2675
return""" % ('\n ' * 1000 ,), d )
2666
2676
f = d ['f' ]
2667
- self .run_test (f , 2 , 1007 , [0 ])
2677
+ self .run_test (f , 2 , 1007 , [0 ], warning = ( RuntimeWarning , self . unbound_locals ) )
2668
2678
2669
2679
def test_jump_to_firstlineno (self ):
2670
2680
# This tests that PDB can jump back to the first line in a
@@ -2714,21 +2724,21 @@ def gen():
2714
2724
next (gen ())
2715
2725
output .append (5 )
2716
2726
2717
- @jump_test (2 , 3 , [1 , 3 ])
2727
+ @jump_test (2 , 3 , [1 , 3 ], warning = ( RuntimeWarning , unbound_locals ) )
2718
2728
def test_jump_forward_over_listcomp (output ):
2719
2729
output .append (1 )
2720
2730
x = [i for i in range (10 )]
2721
2731
output .append (3 )
2722
2732
2723
2733
# checking for segfaults.
2724
2734
# See https://github.com/python/cpython/issues/92311
2725
- @jump_test (3 , 1 , [])
2735
+ @jump_test (3 , 1 , [], warning = ( RuntimeWarning , unbound_locals ) )
2726
2736
def test_jump_backward_over_listcomp (output ):
2727
2737
a = 1
2728
2738
x = [i for i in range (10 )]
2729
2739
c = 3
2730
2740
2731
- @jump_test (8 , 2 , [2 , 7 , 2 ])
2741
+ @jump_test (8 , 2 , [2 , 7 , 2 ], warning = ( RuntimeWarning , unbound_locals ) )
2732
2742
def test_jump_backward_over_listcomp_v2 (output ):
2733
2743
flag = False
2734
2744
output .append (2 )
@@ -2739,19 +2749,19 @@ def test_jump_backward_over_listcomp_v2(output):
2739
2749
output .append (7 )
2740
2750
output .append (8 )
2741
2751
2742
- @async_jump_test (2 , 3 , [1 , 3 ])
2752
+ @async_jump_test (2 , 3 , [1 , 3 ], warning = ( RuntimeWarning , unbound_locals ) )
2743
2753
async def test_jump_forward_over_async_listcomp (output ):
2744
2754
output .append (1 )
2745
2755
x = [i async for i in asynciter (range (10 ))]
2746
2756
output .append (3 )
2747
2757
2748
- @async_jump_test (3 , 1 , [])
2758
+ @async_jump_test (3 , 1 , [], warning = ( RuntimeWarning , unbound_locals ) )
2749
2759
async def test_jump_backward_over_async_listcomp (output ):
2750
2760
a = 1
2751
2761
x = [i async for i in asynciter (range (10 ))]
2752
2762
c = 3
2753
2763
2754
- @async_jump_test (8 , 2 , [2 , 7 , 2 ])
2764
+ @async_jump_test (8 , 2 , [2 , 7 , 2 ], warning = ( RuntimeWarning , unbound_locals ) )
2755
2765
async def test_jump_backward_over_async_listcomp_v2 (output ):
2756
2766
flag = False
2757
2767
output .append (2 )
@@ -2820,13 +2830,13 @@ def test_jump_with_null_on_stack_load_attr(output):
2820
2830
)
2821
2831
output .append (15 )
2822
2832
2823
- @jump_test (2 , 3 , [1 , 3 ])
2833
+ @jump_test (2 , 3 , [1 , 3 ], warning = ( RuntimeWarning , unbound_locals ) )
2824
2834
def test_jump_extended_args_unpack_ex_simple (output ):
2825
2835
output .append (1 )
2826
2836
_ , * _ , _ = output .append (2 ) or "Spam"
2827
2837
output .append (3 )
2828
2838
2829
- @jump_test (3 , 4 , [1 , 4 , 4 , 5 ])
2839
+ @jump_test (3 , 4 , [1 , 4 , 4 , 5 ], warning = ( RuntimeWarning , unbound_locals ) )
2830
2840
def test_jump_extended_args_unpack_ex_tricky (output ):
2831
2841
output .append (1 )
2832
2842
(
@@ -2848,9 +2858,9 @@ def test_jump_extended_args_for_iter(self):
2848
2858
namespace = {}
2849
2859
exec ("\n " .join (source ), namespace )
2850
2860
f = namespace ["f" ]
2851
- self .run_test (f , 2 , 100_000 , [1 , 100_000 ])
2861
+ self .run_test (f , 2 , 100_000 , [1 , 100_000 ], warning = ( RuntimeWarning , self . unbound_locals ) )
2852
2862
2853
- @jump_test (2 , 3 , [1 , 3 ])
2863
+ @jump_test (2 , 3 , [1 , 3 ], warning = ( RuntimeWarning , unbound_locals ) )
2854
2864
def test_jump_or_pop (output ):
2855
2865
output .append (1 )
2856
2866
_ = output .append (2 ) and "Spam"
0 commit comments