@@ -57,6 +57,36 @@ def defined_in___main__(name, script, *, remove=False):
5757 mainns .pop (name , None )
5858
5959
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+
6090class ModuleTests (TestBase ):
6191
6292 def test_queue_aliases (self ):
@@ -1121,7 +1151,7 @@ def test_stateless_funcs(self):
11211151
11221152 func = call_func_return_unpickleable
11231153 with self .subTest ('no args, returns unpickleable' ):
1124- with self .assert_fails_not_shareable ( ):
1154+ with self .assertRaises ( interpreters . NotShareableError ):
11251155 interp .call (func )
11261156
11271157 def test_stateless_func_returns_arg (self ):
@@ -1318,7 +1348,7 @@ def {funcname}():
13181348
13191349 with self .subTest ('pickleable, added dynamically' ):
13201350 with defined_in___main__ (funcname , script ) as arg :
1321- with self .assert_fails_not_shareable ( ):
1351+ with self .assertRaises ( interpreters . NotShareableError ):
13221352 interp .call (defs .spam_returns_arg , arg )
13231353
13241354 with self .subTest ('lying about __main__' ):
@@ -1365,7 +1395,7 @@ def test_call_invalid(self):
13651395
13661396 func = get_call_func_closure
13671397 with self .subTest (func ):
1368- with self .assert_fails_not_shareable ( ):
1398+ with self .assertRaises ( interpreters . NotShareableError ):
13691399 interp .call (func , 42 )
13701400
13711401 func = get_call_func_closure (42 )
@@ -1376,12 +1406,12 @@ def test_call_invalid(self):
13761406 func = call_func_complex
13771407 op = 'closure'
13781408 with self .subTest (f'{ func } ({ op } )' ):
1379- with self .assert_fails_not_shareable ( ):
1409+ with self .assertRaises ( interpreters . NotShareableError ):
13801410 interp .call (func , op , value = '~~~' )
13811411
13821412 op = 'custom-inner'
13831413 with self .subTest (f'{ func } ({ op } )' ):
1384- with self .assert_fails_not_shareable ( ):
1414+ with self .assertRaises ( interpreters . NotShareableError ):
13851415 interp .call (func , op , 'eggs!' )
13861416
13871417 def test_call_in_thread (self ):
@@ -1924,18 +1954,14 @@ def test_exec(self):
19241954 with results :
19251955 exc = _interpreters .exec (interpid , script )
19261956 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!' ,
19351959 # We check these in other tests.
19361960 formatted = exc .formatted ,
19371961 errdisplay = exc .errdisplay ,
1938- ))
1962+ )
1963+ self .assertEqual (out , '' )
1964+ self .assert_ns_equal (exc , expected )
19391965
19401966 with self .subTest ('from C-API' ):
19411967 with self .interpreter_from_capi () as interpid :
@@ -1983,18 +2009,14 @@ def test_call(self):
19832009 with self .subTest ('uncaught exception' ):
19842010 func = defs .spam_raises
19852011 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!' ,
19942014 # We check these in other tests.
19952015 formatted = exc .formatted ,
19962016 errdisplay = exc .errdisplay ,
1997- ))
2017+ )
2018+ self .assertIsNone (res )
2019+ self .assertEqual (exc , expected )
19982020
19992021 @requires_test_modules
20002022 def test_set___main___attrs (self ):
0 commit comments