Skip to content

gh-76785: Add Interpreter.bind() #111575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
_interpreters.RunFailedError -> interpreters.RunFailedError
  • Loading branch information
ericsnowcurrently committed Nov 6, 2023
commit 4056c55bc5e4e0492778c00fc6ed9b2a29b2be06
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy