@@ -30,6 +30,36 @@ def _testfunc(num):
30
30
pytest .skip ("_testunc() not available" )
31
31
from _cffi_backend import __version__
32
32
33
+
34
+ @contextlib .contextmanager
35
+ def _assert_unraisable (error_type : type [Exception ] | None , message : str = '' , traceback_tokens : list [str ] | None = None ):
36
+ """Assert that a given sys.unraisablehook interaction occurred (or did not occur, if error_type is None) while this context was active"""
37
+ raised_errors : list [Exception ] = []
38
+ raised_traceback : str = ''
39
+
40
+ # sys.unraisablehook is called more than once for chained exceptions; accumulate the errors and tracebacks for inspection
41
+ def _capture_unraisable_hook (ur_args ):
42
+ nonlocal raised_traceback
43
+ raised_errors .append (ur_args .exc_value )
44
+
45
+ # NB: need to use the old etype/value/tb form until 3.10 is the minimum
46
+ raised_traceback += (ur_args .err_msg or '' + '\n ' ) + '' .join (traceback .format_exception (None , ur_args .exc_value , ur_args .exc_traceback ))
47
+
48
+
49
+ with pytest .MonkeyPatch .context () as mp :
50
+ mp .setattr (sys , 'unraisablehook' , _capture_unraisable_hook )
51
+ yield
52
+
53
+ if error_type is None :
54
+ assert not raised_errors
55
+ assert not raised_traceback
56
+ return
57
+
58
+ assert any (type (raised_error ) is error_type for raised_error in raised_errors )
59
+ assert any (message in str (raised_error ) for raised_error in raised_errors )
60
+ for t in traceback_tokens or []:
61
+ assert t in raised_traceback
62
+
33
63
# ____________________________________________________________
34
64
35
65
import sys
@@ -1329,6 +1359,7 @@ def test_write_variable():
1329
1359
ll .close_lib ()
1330
1360
pytest .raises (ValueError , ll .write_variable , BVoidP , "stderr" , stderr )
1331
1361
1362
+
1332
1363
def test_callback ():
1333
1364
BInt = new_primitive_type ("int" )
1334
1365
def make_callback ():
@@ -1344,35 +1375,6 @@ def cb(n):
1344
1375
e = pytest .raises (TypeError , f )
1345
1376
assert str (e .value ) == "'int(*)(int)' expects 1 arguments, got 0"
1346
1377
1347
- @contextlib .contextmanager
1348
- def _assert_unraisable (error_type : type [Exception ] | None , message : str = '' , traceback_tokens : list [str ] | None = None ):
1349
- """Assert that a given sys.unraisablehook interaction occurred (or did not occur, if error_type is None) while this context was active"""
1350
- raised_errors : list [Exception ] = []
1351
- raised_traceback : str = ''
1352
-
1353
- # sys.unraisablehook is called more than once for chained exceptions; accumulate the errors and tracebacks for inspection
1354
- def _capture_unraisable_hook (ur_args ):
1355
- nonlocal raised_traceback
1356
- raised_errors .append (ur_args .exc_value )
1357
-
1358
- # NB: need to use the old etype/value/tb form until 3.10 is the minimum
1359
- raised_traceback += (ur_args .err_msg or '' + '\n ' ) + '' .join (traceback .format_exception (None , ur_args .exc_value , ur_args .exc_traceback ))
1360
-
1361
-
1362
- with pytest .MonkeyPatch .context () as mp :
1363
- mp .setattr (sys , 'unraisablehook' , _capture_unraisable_hook )
1364
- yield
1365
-
1366
- if error_type is None :
1367
- assert not raised_errors
1368
- assert not raised_traceback
1369
- return
1370
-
1371
- assert any (type (raised_error ) is error_type for raised_error in raised_errors )
1372
- assert any (message in str (raised_error ) for raised_error in raised_errors )
1373
- for t in traceback_tokens or []:
1374
- assert t in raised_traceback
1375
-
1376
1378
1377
1379
def test_callback_exception ():
1378
1380
def check_value (x ):
0 commit comments