Skip to content

Commit

Permalink
_interpreters.RunFailedError -> interpreters.RunFailedError
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Nov 2, 2023
1 parent 74191dd commit a62ea5c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 67 deletions.
23 changes: 13 additions & 10 deletions Lib/test/support/interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@

__all__ = [
'Interpreter', 'get_current', 'get_main', 'create', 'list_all',
'RunFailedError',
'SendChannel', 'RecvChannel',
'create_channel', 'list_all_channels', 'is_shareable',
'ChannelError', 'ChannelNotFoundError',
'ChannelEmptyError',
]


class RunFailedError(RuntimeError):

def __init__(self, snapshot):
if snapshot.type and snapshot.msg:
msg = f'{snapshot.type}: {snapshot.msg}'
else:
msg = snapshot.type or snapshot.msg
super().__init__(msg)
self.snapshot = snapshot


def create(*, isolated=True):
"""Return a new (idle) Python interpreter."""
id = _interpreters.create(isolated=isolated)
Expand Down Expand Up @@ -118,16 +130,7 @@ def run(self, src_str, /, *, init=None):
"""
err = _interpreters.exec(self._id, src_str, init)
if err is not None:
if err.name is not None:
if err.msg is not None:
msg = f'{err.name}: {err.msg}'
else:
msg = err.name
elif err.msg is not None:
msg = err.msg
else:
msg = None
raise RunFailedError(msg)
raise RunFailedError(err)


def create_channel():
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ def test_success(self):

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

def test_failure(self):
interp = interpreters.create()
with self.assertRaises(interpreters.RunFailedError):
interp.run('raise Exception')

def test_in_thread(self):
interp = interpreters.create()
script, file = _captured_script('print("it worked!", end="")')
Expand Down
57 changes: 0 additions & 57 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,12 @@ _get_current_interp(void)
return PyInterpreterState_Get();
}

static PyObject *
add_new_exception(PyObject *mod, const char *name, PyObject *base)
{
assert(!PyObject_HasAttrStringWithError(mod, name));
PyObject *exctype = PyErr_NewException(name, base, NULL);
if (exctype == NULL) {
return NULL;
}
int res = PyModule_AddType(mod, (PyTypeObject *)exctype);
if (res < 0) {
Py_DECREF(exctype);
return NULL;
}
return exctype;
}

#define ADD_NEW_EXCEPTION(MOD, NAME, BASE) \
add_new_exception(MOD, MODULE_NAME "." Py_STRINGIFY(NAME), BASE)


/* module state *************************************************************/

typedef struct {
/* heap types */
PyTypeObject *ExceptionSnapshotType;

/* exceptions */
PyObject *RunFailedError;
} module_state;

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

/* exceptions */
Py_VISIT(state->RunFailedError);

return 0;
}

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

/* exceptions */
Py_CLEAR(state->RunFailedError);

return 0;
}

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

/* interpreter-specific code ************************************************/

static int
exceptions_init(PyObject *mod)
{
module_state *state = get_module_state(mod);
if (state == NULL) {
return -1;
}

#define ADD(NAME, BASE) \
do { \
assert(state->NAME == NULL); \
state->NAME = ADD_NEW_EXCEPTION(mod, NAME, BASE); \
if (state->NAME == NULL) { \
return -1; \
} \
} while (0)

// An uncaught exception came out of interp_run_string().
ADD(RunFailedError, PyExc_RuntimeError);
#undef ADD

return 0;
}

static int
_run_script(PyObject *ns, const char *codestr, Py_ssize_t codestrlen, int flags)
{
Expand Down Expand Up @@ -772,11 +720,6 @@ module_exec(PyObject *mod)
goto error;
}

/* Add exception types */
if (exceptions_init(mod) != 0) {
goto error;
}

// PyInterpreterID
if (PyModule_AddType(mod, &PyInterpreterID_Type) < 0) {
goto error;
Expand Down

0 comments on commit a62ea5c

Please sign in to comment.