Skip to content

Commit 640e89f

Browse files
authored
rearrange code to make PyPy testing happier (#59)
1 parent e45937e commit 640e89f

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/c/test_c.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@ def _testfunc(num):
3030
pytest.skip("_testunc() not available")
3131
from _cffi_backend import __version__
3232

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+
3363
# ____________________________________________________________
3464

3565
import sys
@@ -1329,6 +1359,7 @@ def test_write_variable():
13291359
ll.close_lib()
13301360
pytest.raises(ValueError, ll.write_variable, BVoidP, "stderr", stderr)
13311361

1362+
13321363
def test_callback():
13331364
BInt = new_primitive_type("int")
13341365
def make_callback():
@@ -1344,35 +1375,6 @@ def cb(n):
13441375
e = pytest.raises(TypeError, f)
13451376
assert str(e.value) == "'int(*)(int)' expects 1 arguments, got 0"
13461377

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-
13761378

13771379
def test_callback_exception():
13781380
def check_value(x):

0 commit comments

Comments
 (0)