@@ -57,6 +57,36 @@ def defined_in___main__(name, script, *, remove=False):
57
57
mainns .pop (name , None )
58
58
59
59
60
+ def build_excinfo (exctype , msg = None , formatted = None , errdisplay = None ):
61
+ if isinstance (exctype , type ):
62
+ assert issubclass (exctype , BaseException ), exctype
63
+ exctype = types .SimpleNamespace (
64
+ __name__ = exctype .__name__ ,
65
+ __qualname__ = exctype .__qualname__ ,
66
+ __module__ = exctype .__module__ ,
67
+ )
68
+ elif isinstance (exctype , str ):
69
+ module , _ , name = exctype .rpartition (exctype )
70
+ if not module and name in __builtins__ :
71
+ module = 'builtins'
72
+ exctype = types .SimpleNamespace (
73
+ __name__ = name ,
74
+ __qualname__ = exctype ,
75
+ __module__ = module or None ,
76
+ )
77
+ else :
78
+ assert isinstance (exctype , types .SimpleNamespace )
79
+ assert msg is None or isinstance (msg , str ), msg
80
+ assert formatted is None or isinstance (formatted , str ), formatted
81
+ assert errdisplay is None or isinstance (errdisplay , str ), errdisplay
82
+ return types .SimpleNamespace (
83
+ type = exctype ,
84
+ msg = msg ,
85
+ formatted = formatted ,
86
+ errdisplay = errdisplay ,
87
+ )
88
+
89
+
60
90
class ModuleTests (TestBase ):
61
91
62
92
def test_queue_aliases (self ):
@@ -1121,7 +1151,7 @@ def test_stateless_funcs(self):
1121
1151
1122
1152
func = call_func_return_unpickleable
1123
1153
with self .subTest ('no args, returns unpickleable' ):
1124
- with self .assert_fails_not_shareable ( ):
1154
+ with self .assertRaises ( interpreters . NotShareableError ):
1125
1155
interp .call (func )
1126
1156
1127
1157
def test_stateless_func_returns_arg (self ):
@@ -1318,7 +1348,7 @@ def {funcname}():
1318
1348
1319
1349
with self .subTest ('pickleable, added dynamically' ):
1320
1350
with defined_in___main__ (funcname , script ) as arg :
1321
- with self .assert_fails_not_shareable ( ):
1351
+ with self .assertRaises ( interpreters . NotShareableError ):
1322
1352
interp .call (defs .spam_returns_arg , arg )
1323
1353
1324
1354
with self .subTest ('lying about __main__' ):
@@ -1365,7 +1395,7 @@ def test_call_invalid(self):
1365
1395
1366
1396
func = get_call_func_closure
1367
1397
with self .subTest (func ):
1368
- with self .assert_fails_not_shareable ( ):
1398
+ with self .assertRaises ( interpreters . NotShareableError ):
1369
1399
interp .call (func , 42 )
1370
1400
1371
1401
func = get_call_func_closure (42 )
@@ -1376,12 +1406,12 @@ def test_call_invalid(self):
1376
1406
func = call_func_complex
1377
1407
op = 'closure'
1378
1408
with self .subTest (f'{ func } ({ op } )' ):
1379
- with self .assert_fails_not_shareable ( ):
1409
+ with self .assertRaises ( interpreters . NotShareableError ):
1380
1410
interp .call (func , op , value = '~~~' )
1381
1411
1382
1412
op = 'custom-inner'
1383
1413
with self .subTest (f'{ func } ({ op } )' ):
1384
- with self .assert_fails_not_shareable ( ):
1414
+ with self .assertRaises ( interpreters . NotShareableError ):
1385
1415
interp .call (func , op , 'eggs!' )
1386
1416
1387
1417
def test_call_in_thread (self ):
@@ -1924,18 +1954,14 @@ def test_exec(self):
1924
1954
with results :
1925
1955
exc = _interpreters .exec (interpid , script )
1926
1956
out = results .stdout ()
1927
- self .assertEqual (out , '' )
1928
- self .assert_ns_equal (exc , types .SimpleNamespace (
1929
- type = types .SimpleNamespace (
1930
- __name__ = 'Exception' ,
1931
- __qualname__ = 'Exception' ,
1932
- __module__ = 'builtins' ,
1933
- ),
1934
- msg = 'uh-oh!' ,
1957
+ expected = build_excinfo (
1958
+ Exception , 'uh-oh!' ,
1935
1959
# We check these in other tests.
1936
1960
formatted = exc .formatted ,
1937
1961
errdisplay = exc .errdisplay ,
1938
- ))
1962
+ )
1963
+ self .assertEqual (out , '' )
1964
+ self .assert_ns_equal (exc , expected )
1939
1965
1940
1966
with self .subTest ('from C-API' ):
1941
1967
with self .interpreter_from_capi () as interpid :
@@ -1983,18 +2009,14 @@ def test_call(self):
1983
2009
with self .subTest ('uncaught exception' ):
1984
2010
func = defs .spam_raises
1985
2011
res , exc = _interpreters .call (interpid , func )
1986
- self .assertIsNone (res )
1987
- self .assertEqual (exc , types .SimpleNamespace (
1988
- type = types .SimpleNamespace (
1989
- __name__ = 'Exception' ,
1990
- __qualname__ = 'Exception' ,
1991
- __module__ = 'builtins' ,
1992
- ),
1993
- msg = 'spam!' ,
2012
+ expected = build_excinfo (
2013
+ Exception , 'spam!' ,
1994
2014
# We check these in other tests.
1995
2015
formatted = exc .formatted ,
1996
2016
errdisplay = exc .errdisplay ,
1997
- ))
2017
+ )
2018
+ self .assertIsNone (res )
2019
+ self .assertEqual (exc , expected )
1998
2020
1999
2021
@requires_test_modules
2000
2022
def test_set___main___attrs (self ):
0 commit comments