@@ -1938,19 +1938,28 @@ def setUp(self):
1938
1938
( 1 , E .RAISE , capi .fire_event_raise , ValueError (2 )),
1939
1939
( 1 , E .EXCEPTION_HANDLED , capi .fire_event_exception_handled , ValueError (5 )),
1940
1940
( 1 , E .PY_UNWIND , capi .fire_event_py_unwind , ValueError (6 )),
1941
- ( 1 , E .STOP_ITERATION , capi .fire_event_stop_iteration , ValueError (7 )),
1941
+ ( 1 , E .STOP_ITERATION , capi .fire_event_stop_iteration , 7 ),
1942
+ ( 1 , E .STOP_ITERATION , capi .fire_event_stop_iteration , StopIteration (8 )),
1942
1943
]
1943
1944
1945
+ self .EXPECT_RAISED_EXCEPTION = [E .PY_THROW , E .RAISE , E .EXCEPTION_HANDLED , E .PY_UNWIND ]
1944
1946
1945
- def check_event_count (self , event , func , args , expected ):
1947
+
1948
+ def check_event_count (self , event , func , args , expected , callback_raises = None ):
1946
1949
class Counter :
1947
- def __init__ (self ):
1950
+ def __init__ (self , callback_raises ):
1951
+ self .callback_raises = callback_raises
1948
1952
self .count = 0
1953
+
1949
1954
def __call__ (self , * args ):
1950
1955
self .count += 1
1956
+ if self .callback_raises :
1957
+ exc = self .callback_raises
1958
+ self .callback_raises = None
1959
+ raise exc
1951
1960
1952
1961
try :
1953
- counter = Counter ()
1962
+ counter = Counter (callback_raises )
1954
1963
sys .monitoring .register_callback (TEST_TOOL , event , counter )
1955
1964
if event == E .C_RETURN or event == E .C_RAISE :
1956
1965
sys .monitoring .set_events (TEST_TOOL , E .CALL )
@@ -1987,8 +1996,9 @@ def test_fire_event(self):
1987
1996
1988
1997
def test_missing_exception (self ):
1989
1998
for _ , event , function , * args in self .cases :
1990
- if not ( args and isinstance ( args [ - 1 ], BaseException )) :
1999
+ if event not in self . EXPECT_RAISED_EXCEPTION :
1991
2000
continue
2001
+ assert args and isinstance (args [- 1 ], BaseException )
1992
2002
offset = 0
1993
2003
self .codelike = _testcapi .CodeLike (1 )
1994
2004
with self .subTest (function .__name__ ):
@@ -1997,6 +2007,17 @@ def test_missing_exception(self):
1997
2007
expected = ValueError (f"Firing event { evt } with no exception set" )
1998
2008
self .check_event_count (event , function , args_ , expected )
1999
2009
2010
+ def test_fire_event_failing_callback (self ):
2011
+ for expected , event , function , * args in self .cases :
2012
+ offset = 0
2013
+ self .codelike = _testcapi .CodeLike (1 )
2014
+ with self .subTest (function .__name__ ):
2015
+ args_ = (self .codelike , offset ) + tuple (args )
2016
+ exc = OSError (42 )
2017
+ with self .assertRaises (type (exc )):
2018
+ self .check_event_count (event , function , args_ , expected ,
2019
+ callback_raises = exc )
2020
+
2000
2021
2001
2022
CANNOT_DISABLE = { E .PY_THROW , E .RAISE , E .RERAISE ,
2002
2023
E .EXCEPTION_HANDLED , E .PY_UNWIND }
0 commit comments