Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
76a7d7b
Make Interpreter() idempotent.
ericsnowcurrently Nov 28, 2023
57a3c19
Interpreters are always isolated.
ericsnowcurrently Nov 28, 2023
747e542
interp.id is always int.
ericsnowcurrently Nov 28, 2023
0ed9fb0
Add InterpreterNotFoundError.
ericsnowcurrently Dec 1, 2023
98f9cf3
Stop using InterpreterID in _interpreters.
ericsnowcurrently Dec 1, 2023
0277878
Fix Interpreter.__repr__().
ericsnowcurrently Nov 28, 2023
59c8227
.run() -> .exec_sync()
ericsnowcurrently Nov 28, 2023
fa132a2
RunFailedError -> ExecFailure
ericsnowcurrently Nov 28, 2023
644efa5
Add Interpreter.run().
ericsnowcurrently Nov 28, 2023
abf2aa8
Make the interpreters module a package.
ericsnowcurrently Dec 1, 2023
8ca303f
Add interpreters.Queue.
ericsnowcurrently Nov 28, 2023
8b97373
Add memoryview XID with _xxsubinterpreters import.
ericsnowcurrently Dec 11, 2023
cc20f1f
Update CODEOWNERS.
ericsnowcurrently Dec 11, 2023
cb0a605
Ignore static builtin exception types.
ericsnowcurrently Dec 11, 2023
62a9bac
Make CODEOWNERS more specific.
ericsnowcurrently Dec 11, 2023
6c71018
Fix submodule names.
ericsnowcurrently Dec 11, 2023
13a44e3
Use interpreters.__getattr__() for submodule aliases.
ericsnowcurrently Dec 7, 2023
c9d15d2
Fix InterpreterIDTests.
ericsnowcurrently Dec 11, 2023
1c76c4a
LONG_LONG_MAX -> LLONG_MAX
ericsnowcurrently Dec 11, 2023
cfae15f
Fix Interpreter.run().
ericsnowcurrently Dec 11, 2023
3f98ce1
Use exec_sync() in test_sys.
ericsnowcurrently Dec 11, 2023
f8b1685
Fix test_capi.
ericsnowcurrently Dec 11, 2023
0b34a83
Fix test_importlib.
ericsnowcurrently Dec 12, 2023
68faf1a
Fix test_import.
ericsnowcurrently Dec 12, 2023
d161f76
Fix test_threading.
ericsnowcurrently Dec 12, 2023
778276f
Fix TestInterpreterRun.
ericsnowcurrently Dec 12, 2023
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
Add InterpreterNotFoundError.
  • Loading branch information
ericsnowcurrently committed Dec 1, 2023
commit 0ed9fb0684cc39465aa871b9c391ba0d550fd2a9
10 changes: 10 additions & 0 deletions Include/internal/pycore_crossinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ extern "C" {

#include "pycore_pyerrors.h"

/**************/
/* exceptions */
/**************/

PyAPI_DATA(PyObject *) PyExc_InterpreterError;
PyAPI_DATA(PyObject *) PyExc_InterpreterNotFoundError;


/***************************/
/* cross-interpreter calls */
Expand Down Expand Up @@ -159,6 +166,9 @@ struct _xi_state {
extern PyStatus _PyXI_Init(PyInterpreterState *interp);
extern void _PyXI_Fini(PyInterpreterState *interp);

extern PyStatus _PyXI_InitTypes(PyInterpreterState *interp);
extern void _PyXI_FiniTypes(PyInterpreterState *interp);


/***************************/
/* short-term data sharing */
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/support/interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import _xxinterpchannels as _channels

# aliases:
from _xxsubinterpreters import is_shareable
from _xxsubinterpreters import (
InterpreterError, InterpreterNotFoundError,
is_shareable,
)
from _xxinterpchannels import (
ChannelError, ChannelNotFoundError, ChannelClosedError,
ChannelEmptyError, ChannelNotEmptyError,
Expand All @@ -15,7 +18,7 @@

__all__ = [
'Interpreter', 'get_current', 'get_main', 'create', 'list_all',
'RunFailedError',
'InterpreterError', 'InterpreterNotFoundError', 'RunFailedError',
'SendChannel', 'RecvChannel',
'create_channel', 'list_all_channels', 'is_shareable',
'ChannelError', 'ChannelNotFoundError',
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
_interpreters = import_helper.import_module('_xxsubinterpreters')
_channels = import_helper.import_module('_xxinterpchannels')
from test.support import interpreters
from test.support.interpreters import InterpreterNotFoundError


def _captured_script(script):
Expand Down Expand Up @@ -282,7 +283,7 @@ def test_idempotent(self):
self.assertIs(interp, main)

def test_init_does_not_exist(self):
with self.assertRaises(RuntimeError):
with self.assertRaises(InterpreterNotFoundError):
interpreters.Interpreter(1_000_000)

def test_init_bad_id(self):
Expand Down Expand Up @@ -348,7 +349,7 @@ def test_from_subinterpreter(self):
def test_already_destroyed(self):
interp = interpreters.create()
interp.close()
with self.assertRaises(RuntimeError):
with self.assertRaises(InterpreterNotFoundError):
interp.is_running()

def test_with_only_background_threads(self):
Expand Down Expand Up @@ -417,7 +418,7 @@ def f():
def test_already_destroyed(self):
interp = interpreters.create()
interp.close()
with self.assertRaises(RuntimeError):
with self.assertRaises(InterpreterNotFoundError):
interp.close()

def test_from_current(self):
Expand Down
8 changes: 8 additions & 0 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,14 @@ module_exec(PyObject *mod)
goto error;
}

// exceptions
if (PyModule_AddType(mod, (PyTypeObject *)PyExc_InterpreterError) < 0) {
goto error;
}
if (PyModule_AddType(mod, (PyTypeObject *)PyExc_InterpreterNotFoundError) < 0) {
goto error;
}

return 0;

error:
Expand Down
62 changes: 62 additions & 0 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,53 @@
#include "pycore_weakref.h" // _PyWeakref_GET_REF()


/**************/
/* exceptions */
/**************/

/* InterpreterError extends Exception */

static PyTypeObject _PyExc_InterpreterError = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "InterpreterError",
.tp_doc = PyDoc_STR("An interpreter was not found."),
//.tp_base = (PyTypeObject *)PyExc_BaseException,
};
PyObject *PyExc_InterpreterError = (PyObject *)&_PyExc_InterpreterError;

/* InterpreterNotFoundError extends InterpreterError */

static PyTypeObject _PyExc_InterpreterNotFoundError = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "InterpreterNotFoundError",
.tp_doc = PyDoc_STR("An interpreter was not found."),
.tp_base = &_PyExc_InterpreterError,
};
PyObject *PyExc_InterpreterNotFoundError = (PyObject *)&_PyExc_InterpreterNotFoundError;

/* lifecycle */

static int
init_exceptions(PyInterpreterState *interp)
{
_PyExc_InterpreterError.tp_base = (PyTypeObject *)PyExc_BaseException;
if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterError) < 0) {
return -1;
}
if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterNotFoundError) < 0) {
return -1;
}
return 0;
}

static void
fini_exceptions(PyInterpreterState *interp)
{
_PyStaticType_Dealloc(interp, &_PyExc_InterpreterNotFoundError);
_PyStaticType_Dealloc(interp, &_PyExc_InterpreterError);
}


/***************************/
/* cross-interpreter calls */
/***************************/
Expand Down Expand Up @@ -2118,3 +2165,18 @@ _PyXI_Fini(PyInterpreterState *interp)
_xidregistry_fini(_get_global_xidregistry(interp->runtime));
}
}

PyStatus
_PyXI_InitTypes(PyInterpreterState *interp)
{
if (init_exceptions(interp) < 0) {
return _PyStatus_ERR("failed to initialize an exception type");
}
return _PyStatus_OK();
}

void
_PyXI_FiniTypes(PyInterpreterState *interp)
{
fini_exceptions(interp);
}
6 changes: 6 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,11 @@ pycore_init_types(PyInterpreterState *interp)
return status;
}

status = _PyXI_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

return _PyStatus_OK();
}

Expand Down Expand Up @@ -1742,6 +1747,7 @@ finalize_interp_types(PyInterpreterState *interp)
{
_PyUnicode_FiniTypes(interp);
_PySys_FiniTypes(interp);
_PyXI_FiniTypes(interp);
_PyExc_Fini(interp);
_PyAsyncGen_Fini(interp);
_PyContext_Fini(interp);
Expand Down
2 changes: 1 addition & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ _PyInterpreterState_LookUpID(int64_t requested_id)
HEAD_UNLOCK(runtime);
}
if (interp == NULL && !PyErr_Occurred()) {
PyErr_Format(PyExc_RuntimeError,
PyErr_Format(PyExc_InterpreterNotFoundError,
"unrecognized interpreter ID %lld", requested_id);
}
return interp;
Expand Down