7
7
import gc
8
8
from functools import wraps
9
9
import asyncio
10
+ import warnings
10
11
from test .support import import_helper
11
12
12
13
support .requires_working_socket (module = True )
@@ -1943,13 +1944,13 @@ def run_test(self, func, jumpFrom, jumpTo, expected, error=None,
1943
1944
if warning is None and error is None :
1944
1945
func (output )
1945
1946
elif warning is not None and error is None :
1946
- with self .assertWarnsRegex (* warning ):
1947
+ with self .assertWarnsRegex (* warning ) as warning_context :
1947
1948
func (output )
1948
1949
elif warning is None and error is not None :
1949
1950
with self .assertRaisesRegex (* error ):
1950
1951
func (output )
1951
1952
else :
1952
- with self .assertWarnsRegex ( * warning ) as warning_context , self .assertRaisesRegex ( * error ) as error_context :
1953
+ with self .assertRaisesRegex ( * error ) as error_context , self .assertWarnsRegex ( * warning ) as warning_context :
1953
1954
func (output )
1954
1955
1955
1956
sys .settrace (None )
@@ -1970,8 +1971,8 @@ def run_async_test(self, func, jumpFrom, jumpTo, expected, error=None,
1970
1971
with self .assertRaisesRegex (* error ):
1971
1972
asyncio .run (func (output ))
1972
1973
else :
1973
- raise Exception ( "Not currently possible to assert both a warning and an exception in tandem, the former is seemingly ignored by the unittest framework" )
1974
- assertTrue ( False )
1974
+ with self . assertRaisesRegex ( * error ) as error_context , self . assertWarnsRegex ( * warning ) as warning_context :
1975
+ func ( output )
1975
1976
1976
1977
sys .settrace (None )
1977
1978
asyncio .set_event_loop_policy (None )
@@ -2704,12 +2705,26 @@ class fake_function:
2704
2705
sys .settrace (None )
2705
2706
self .compare_jump_output ([2 , 3 , 2 , 3 , 4 ], namespace ["output" ])
2706
2707
2707
- @jump_test (2 , 4 , [1 ], event = 'call' , error = (ValueError , "can't jump from"
2708
- " the 'call' trace event of a new frame" ),
2709
- warning = (RuntimeWarning , unbound_locals ))
2708
+ @jump_test (1 , 2 , [1 , 2 ], event = 'call' , error = (ValueError , 'Test Value Error' ),
2709
+ warning = (RuntimeWarning , 'Test Runtime Warning' ))
2710
+ def test_error_and_warning_in_tandem (output ):
2711
+ output .append (1 )
2712
+ output .append (2 )
2713
+ warnings .warn (RuntimeWarning ("Test Runtime Warning" ))
2714
+ raise ValueError ("Test Value Error" )
2715
+
2716
+ @jump_test (1 , 2 , [1 , 2 ], event = 'call' , error = (ValueError , 'Test Value Error' ),
2717
+ warning = (RuntimeWarning , 'Test Runtime Warning' ))
2718
+ def test_warning_and_error_in_tandem (output ):
2719
+ output .append (1 )
2720
+ output .append (2 )
2721
+ raise ValueError ("Test Value Error" )
2722
+ warnings .warn (RuntimeWarning ("Test Runtime Warning" ))
2723
+
2724
+ @jump_test (2 , 3 , [1 ], event = 'call' , error = (ValueError , "can't jump from"
2725
+ " the 'call' trace event of a new frame" ))
2710
2726
def test_no_jump_from_call (output ):
2711
2727
output .append (1 )
2712
- x = [i for i in range (10 )]
2713
2728
def nested ():
2714
2729
output .append (3 )
2715
2730
nested ()
0 commit comments