• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* InterpreterError extends Exception */
3 
4 static PyTypeObject _PyExc_InterpreterError = {
5     PyVarObject_HEAD_INIT(NULL, 0)
6     .tp_name = "interpreters.InterpreterError",
7     .tp_doc = PyDoc_STR("A cross-interpreter operation failed"),
8     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
9     //.tp_traverse = ((PyTypeObject *)PyExc_Exception)->tp_traverse,
10     //.tp_clear = ((PyTypeObject *)PyExc_Exception)->tp_clear,
11     //.tp_base = (PyTypeObject *)PyExc_Exception,
12 };
13 PyObject *PyExc_InterpreterError = (PyObject *)&_PyExc_InterpreterError;
14 
15 /* InterpreterNotFoundError extends InterpreterError */
16 
17 static PyTypeObject _PyExc_InterpreterNotFoundError = {
18     PyVarObject_HEAD_INIT(NULL, 0)
19     .tp_name = "interpreters.InterpreterNotFoundError",
20     .tp_doc = PyDoc_STR("An interpreter was not found"),
21     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
22     //.tp_traverse = ((PyTypeObject *)PyExc_Exception)->tp_traverse,
23     //.tp_clear = ((PyTypeObject *)PyExc_Exception)->tp_clear,
24     .tp_base = &_PyExc_InterpreterError,
25 };
26 PyObject *PyExc_InterpreterNotFoundError = (PyObject *)&_PyExc_InterpreterNotFoundError;
27 
28 /* NotShareableError extends ValueError */
29 
30 static int
_init_not_shareable_error_type(PyInterpreterState * interp)31 _init_not_shareable_error_type(PyInterpreterState *interp)
32 {
33     const char *name = "interpreters.NotShareableError";
34     PyObject *base = PyExc_ValueError;
35     PyObject *ns = NULL;
36     PyObject *exctype = PyErr_NewException(name, base, ns);
37     if (exctype == NULL) {
38         return -1;
39     }
40 
41     _PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError = exctype;
42     return 0;
43 }
44 
45 static void
_fini_not_shareable_error_type(PyInterpreterState * interp)46 _fini_not_shareable_error_type(PyInterpreterState *interp)
47 {
48     Py_CLEAR(_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError);
49 }
50 
51 static PyObject *
_get_not_shareable_error_type(PyInterpreterState * interp)52 _get_not_shareable_error_type(PyInterpreterState *interp)
53 {
54     assert(_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError != NULL);
55     return _PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError;
56 }
57 
58 
59 /* lifecycle */
60 
61 static int
init_exceptions(PyInterpreterState * interp)62 init_exceptions(PyInterpreterState *interp)
63 {
64     PyTypeObject *base = (PyTypeObject *)PyExc_Exception;
65 
66     // builtin static types
67 
68     _PyExc_InterpreterError.tp_base = base;
69     _PyExc_InterpreterError.tp_traverse = base->tp_traverse;
70     _PyExc_InterpreterError.tp_clear = base->tp_clear;
71     if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterError) < 0) {
72         return -1;
73     }
74 
75     _PyExc_InterpreterNotFoundError.tp_traverse = base->tp_traverse;
76     _PyExc_InterpreterNotFoundError.tp_clear = base->tp_clear;
77     if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterNotFoundError) < 0) {
78         return -1;
79     }
80 
81     // heap types
82 
83     // We would  call _init_not_shareable_error_type() here too,
84     // but that leads to ref leaks
85 
86     return 0;
87 }
88 
89 static void
fini_exceptions(PyInterpreterState * interp)90 fini_exceptions(PyInterpreterState *interp)
91 {
92     // Likewise with _fini_not_shareable_error_type().
93     _PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterNotFoundError);
94     _PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterError);
95 }
96