Skip to content

Commit a62ea5c

Browse files
_interpreters.RunFailedError -> interpreters.RunFailedError
1 parent 74191dd commit a62ea5c

File tree

3 files changed

+18
-67
lines changed

3 files changed

+18
-67
lines changed

Lib/test/support/interpreters.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@
1414

1515
__all__ = [
1616
'Interpreter', 'get_current', 'get_main', 'create', 'list_all',
17+
'RunFailedError',
1718
'SendChannel', 'RecvChannel',
1819
'create_channel', 'list_all_channels', 'is_shareable',
1920
'ChannelError', 'ChannelNotFoundError',
2021
'ChannelEmptyError',
2122
]
2223

2324

25+
class RunFailedError(RuntimeError):
26+
27+
def __init__(self, snapshot):
28+
if snapshot.type and snapshot.msg:
29+
msg = f'{snapshot.type}: {snapshot.msg}'
30+
else:
31+
msg = snapshot.type or snapshot.msg
32+
super().__init__(msg)
33+
self.snapshot = snapshot
34+
35+
2436
def create(*, isolated=True):
2537
"""Return a new (idle) Python interpreter."""
2638
id = _interpreters.create(isolated=isolated)
@@ -118,16 +130,7 @@ def run(self, src_str, /, *, init=None):
118130
"""
119131
err = _interpreters.exec(self._id, src_str, init)
120132
if err is not None:
121-
if err.name is not None:
122-
if err.msg is not None:
123-
msg = f'{err.name}: {err.msg}'
124-
else:
125-
msg = err.name
126-
elif err.msg is not None:
127-
msg = err.msg
128-
else:
129-
msg = None
130-
raise RunFailedError(msg)
133+
raise RunFailedError(err)
131134

132135

133136
def create_channel():

Lib/test/test_interpreters.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ def test_success(self):
478478

479479
self.assertEqual(out, 'it worked!')
480480

481+
def test_failure(self):
482+
interp = interpreters.create()
483+
with self.assertRaises(interpreters.RunFailedError):
484+
interp.run('raise Exception')
485+
481486
def test_in_thread(self):
482487
interp = interpreters.create()
483488
script, file = _captured_script('print("it worked!", end="")')

Modules/_xxsubinterpretersmodule.c

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,12 @@ _get_current_interp(void)
2828
return PyInterpreterState_Get();
2929
}
3030

31-
static PyObject *
32-
add_new_exception(PyObject *mod, const char *name, PyObject *base)
33-
{
34-
assert(!PyObject_HasAttrStringWithError(mod, name));
35-
PyObject *exctype = PyErr_NewException(name, base, NULL);
36-
if (exctype == NULL) {
37-
return NULL;
38-
}
39-
int res = PyModule_AddType(mod, (PyTypeObject *)exctype);
40-
if (res < 0) {
41-
Py_DECREF(exctype);
42-
return NULL;
43-
}
44-
return exctype;
45-
}
46-
47-
#define ADD_NEW_EXCEPTION(MOD, NAME, BASE) \
48-
add_new_exception(MOD, MODULE_NAME "." Py_STRINGIFY(NAME), BASE)
49-
5031

5132
/* module state *************************************************************/
5233

5334
typedef struct {
5435
/* heap types */
5536
PyTypeObject *ExceptionSnapshotType;
56-
57-
/* exceptions */
58-
PyObject *RunFailedError;
5937
} module_state;
6038

6139
static inline module_state *
@@ -73,9 +51,6 @@ traverse_module_state(module_state *state, visitproc visit, void *arg)
7351
/* heap types */
7452
Py_VISIT(state->ExceptionSnapshotType);
7553

76-
/* exceptions */
77-
Py_VISIT(state->RunFailedError);
78-
7954
return 0;
8055
}
8156

@@ -85,9 +60,6 @@ clear_module_state(module_state *state)
8560
/* heap types */
8661
Py_CLEAR(state->ExceptionSnapshotType);
8762

88-
/* exceptions */
89-
Py_CLEAR(state->RunFailedError);
90-
9163
return 0;
9264
}
9365

@@ -186,30 +158,6 @@ get_code_str(PyObject *arg, Py_ssize_t *len_p, PyObject **bytes_p, int *flags_p)
186158

187159
/* interpreter-specific code ************************************************/
188160

189-
static int
190-
exceptions_init(PyObject *mod)
191-
{
192-
module_state *state = get_module_state(mod);
193-
if (state == NULL) {
194-
return -1;
195-
}
196-
197-
#define ADD(NAME, BASE) \
198-
do { \
199-
assert(state->NAME == NULL); \
200-
state->NAME = ADD_NEW_EXCEPTION(mod, NAME, BASE); \
201-
if (state->NAME == NULL) { \
202-
return -1; \
203-
} \
204-
} while (0)
205-
206-
// An uncaught exception came out of interp_run_string().
207-
ADD(RunFailedError, PyExc_RuntimeError);
208-
#undef ADD
209-
210-
return 0;
211-
}
212-
213161
static int
214162
_run_script(PyObject *ns, const char *codestr, Py_ssize_t codestrlen, int flags)
215163
{
@@ -772,11 +720,6 @@ module_exec(PyObject *mod)
772720
goto error;
773721
}
774722

775-
/* Add exception types */
776-
if (exceptions_init(mod) != 0) {
777-
goto error;
778-
}
779-
780723
// PyInterpreterID
781724
if (PyModule_AddType(mod, &PyInterpreterID_Type) < 0) {
782725
goto error;

0 commit comments

Comments
 (0)