-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathcrossinterp_exceptions.h
206 lines (181 loc) · 5.98 KB
/
crossinterp_exceptions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
static void
_ensure_current_cause(PyThreadState *tstate, PyObject *cause)
{
if (cause == NULL) {
return;
}
PyObject *exc = _PyErr_GetRaisedException(tstate);
assert(exc != NULL);
PyObject *ctx = PyException_GetContext(exc);
if (ctx == NULL) {
PyException_SetContext(exc, Py_NewRef(cause));
}
else {
Py_DECREF(ctx);
}
assert(PyException_GetCause(exc) == NULL);
PyException_SetCause(exc, Py_NewRef(cause));
_PyErr_SetRaisedException(tstate, exc);
}
/* InterpreterError extends Exception */
static PyTypeObject _PyExc_InterpreterError = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "interpreters.InterpreterError",
.tp_doc = PyDoc_STR("A cross-interpreter operation failed"),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
//.tp_traverse = ((PyTypeObject *)PyExc_Exception)->tp_traverse,
//.tp_clear = ((PyTypeObject *)PyExc_Exception)->tp_clear,
//.tp_base = (PyTypeObject *)PyExc_Exception,
};
PyObject *PyExc_InterpreterError = (PyObject *)&_PyExc_InterpreterError;
/* InterpreterNotFoundError extends InterpreterError */
static PyTypeObject _PyExc_InterpreterNotFoundError = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "interpreters.InterpreterNotFoundError",
.tp_doc = PyDoc_STR("An interpreter was not found"),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
//.tp_traverse = ((PyTypeObject *)PyExc_Exception)->tp_traverse,
//.tp_clear = ((PyTypeObject *)PyExc_Exception)->tp_clear,
.tp_base = &_PyExc_InterpreterError,
};
PyObject *PyExc_InterpreterNotFoundError = (PyObject *)&_PyExc_InterpreterNotFoundError;
/* NotShareableError extends TypeError */
static int
_init_notshareableerror(exceptions_t *state)
{
const char *name = "interpreters.NotShareableError";
PyObject *base = PyExc_TypeError;
PyObject *ns = NULL;
PyObject *exctype = PyErr_NewException(name, base, ns);
if (exctype == NULL) {
return -1;
}
state->PyExc_NotShareableError = exctype;
return 0;
}
static void
_fini_notshareableerror(exceptions_t *state)
{
Py_CLEAR(state->PyExc_NotShareableError);
}
static PyObject *
get_notshareableerror_type(PyThreadState *tstate)
{
_PyXI_state_t *local = _PyXI_GET_STATE(tstate->interp);
if (local == NULL) {
PyErr_Clear();
return NULL;
}
return local->exceptions.PyExc_NotShareableError;
}
static void
_ensure_notshareableerror(PyThreadState *tstate,
PyObject *cause, int force, PyObject *msgobj)
{
PyObject *ctx = _PyErr_GetRaisedException(tstate);
PyObject *exctype = get_notshareableerror_type(tstate);
if (exctype != NULL) {
if (!force && ctx != NULL && Py_TYPE(ctx) == (PyTypeObject *)exctype) {
// A NotShareableError instance is already set.
assert(cause == NULL);
_PyErr_SetRaisedException(tstate, ctx);
}
}
else {
exctype = PyExc_TypeError;
}
_PyErr_SetObject(tstate, exctype, msgobj);
// We have to set the context manually since _PyErr_SetObject() doesn't.
_PyErr_ChainExceptions1Tstate(tstate, ctx);
_ensure_current_cause(tstate, cause);
}
static void
set_notshareableerror(PyThreadState *tstate, PyObject *cause, int force, const char *msg)
{
PyObject *msgobj = PyUnicode_FromString(msg);
if (msgobj == NULL) {
assert(_PyErr_Occurred(tstate));
}
else {
_ensure_notshareableerror(tstate, cause, force, msgobj);
Py_DECREF(msgobj);
}
}
static void
format_notshareableerror_v(PyThreadState *tstate, PyObject *cause, int force,
const char *format, va_list vargs)
{
PyObject *msgobj = PyUnicode_FromFormatV(format, vargs);
if (msgobj == NULL) {
assert(_PyErr_Occurred(tstate));
}
else {
_ensure_notshareableerror(tstate, cause, force, msgobj);
Py_DECREF(msgobj);
}
}
static void
format_notshareableerror(PyThreadState *tstate, PyObject *cause, int force,
const char *format, ...)
{
va_list vargs;
va_start(vargs, format);
format_notshareableerror_v(tstate, cause, force, format, vargs);
va_end(vargs);
}
/* lifecycle */
static int
init_static_exctypes(exceptions_t *state, PyInterpreterState *interp)
{
assert(state == &_PyXI_GET_STATE(interp)->exceptions);
PyTypeObject *base = (PyTypeObject *)PyExc_Exception;
// PyExc_InterpreterError
_PyExc_InterpreterError.tp_base = base;
_PyExc_InterpreterError.tp_traverse = base->tp_traverse;
_PyExc_InterpreterError.tp_clear = base->tp_clear;
if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterError) < 0) {
goto error;
}
state->PyExc_InterpreterError = (PyObject *)&_PyExc_InterpreterError;
// PyExc_InterpreterNotFoundError
_PyExc_InterpreterNotFoundError.tp_traverse = base->tp_traverse;
_PyExc_InterpreterNotFoundError.tp_clear = base->tp_clear;
if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterNotFoundError) < 0) {
goto error;
}
state->PyExc_InterpreterNotFoundError =
(PyObject *)&_PyExc_InterpreterNotFoundError;
return 0;
error:
fini_static_exctypes(state, interp);
return -1;
}
static void
fini_static_exctypes(exceptions_t *state, PyInterpreterState *interp)
{
assert(state == &_PyXI_GET_STATE(interp)->exceptions);
if (state->PyExc_InterpreterNotFoundError != NULL) {
state->PyExc_InterpreterNotFoundError = NULL;
_PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterNotFoundError);
}
if (state->PyExc_InterpreterError != NULL) {
state->PyExc_InterpreterError = NULL;
_PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterError);
}
}
static int
init_heap_exctypes(exceptions_t *state)
{
if (_init_notshareableerror(state) < 0) {
goto error;
}
return 0;
error:
fini_heap_exctypes(state);
return -1;
}
static void
fini_heap_exctypes(exceptions_t *state)
{
_fini_notshareableerror(state);
}