10#include "nuitka/prelude.h"
14 char const *format,
char const *arg) {
15 PyObject *exception_value = Nuitka_String_FromFormat(format, arg);
16 CHECK_OBJECT(exception_value);
18 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_VALUE1(tstate, exception_state, PyExc_TypeError, exception_value);
22 char const *format,
char const *arg1,
char const *arg2) {
23 PyObject *exception_value = Nuitka_String_FromFormat(format, arg1, arg2);
24 CHECK_OBJECT(exception_value);
26 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_VALUE1(tstate, exception_state, PyExc_TypeError, exception_value);
29#if PYTHON_VERSION < 0x266
30#define WRONG_EXCEPTION_TYPE_ERROR_MESSAGE "exceptions must be classes or instances, not %s"
31#elif PYTHON_VERSION < 0x300
32#define WRONG_EXCEPTION_TYPE_ERROR_MESSAGE "exceptions must be old-style classes or derived from BaseException, not %s"
34#define WRONG_EXCEPTION_TYPE_ERROR_MESSAGE "exceptions must derive from BaseException"
38#if PYTHON_VERSION < 0x3c0
40 while (unlikely(PyTuple_Check(exception_state->exception_type)) &&
41 PyTuple_GET_SIZE(exception_state->exception_type) > 0) {
42 PyObject *tmp = exception_state->exception_type;
43 exception_state->exception_type = PyTuple_GET_ITEM(exception_state->exception_type, 0);
44 Py_INCREF(exception_state->exception_type);
50#if PYTHON_VERSION < 0x3c0
52 exception_state->exception_value = NULL;
53 exception_state->exception_tb = NULL;
55#if PYTHON_VERSION < 0x300
57 UNPACK_TUPLE_EXCEPTION_TYPE(exception_state);
60 if (PyExceptionClass_Check(exception_state->exception_type)) {
61 NORMALIZE_EXCEPTION(tstate, &exception_state->exception_type, &exception_state->exception_value,
62 &exception_state->exception_tb);
64#if PYTHON_VERSION >= 0x300
65 CHAIN_EXCEPTION(tstate, exception_state->exception_value);
68 }
else if (PyExceptionInstance_Check(exception_state->exception_type)) {
69 exception_state->exception_value = exception_state->exception_type;
70 exception_state->exception_type = PyExceptionInstance_Class(exception_state->exception_type);
71 Py_INCREF(exception_state->exception_type);
73#if PYTHON_VERSION >= 0x300
74 CHAIN_EXCEPTION(tstate, exception_state->exception_value);
77 assert(exception_state->exception_tb == NULL);
78 exception_state->exception_tb = GET_EXCEPTION_TRACEBACK(exception_state->exception_value);
79 Py_XINCREF(exception_state->exception_tb);
84 PyObject *old_exception_type = exception_state->exception_type;
86 FORMAT_TYPE_ERROR1(tstate, exception_state, WRONG_EXCEPTION_TYPE_ERROR_MESSAGE,
87 Py_TYPE(exception_state->exception_type)->tp_name);
89 Py_DECREF(old_exception_type);
95void RAISE_EXCEPTION_WITH_TYPE_AND_VALUE(PyThreadState *tstate,
97 CHECK_EXCEPTION_STATE(exception_state);
99 exception_state->exception_tb = NULL;
102 UNPACK_TUPLE_EXCEPTION_TYPE(exception_state);
104 if (PyExceptionClass_Check(exception_state->exception_type)) {
105 NORMALIZE_EXCEPTION_STATE(tstate, exception_state);
106#if PYTHON_VERSION >= 0x270
107 if (unlikely(!PyExceptionInstance_Check(exception_state->exception_value))) {
108 char const *exception_type_type = Py_TYPE(exception_state->exception_type)->tp_name;
109 char const *exception_value_type = Py_TYPE(exception_state->exception_value)->tp_name;
111 RELEASE_ERROR_OCCURRED_STATE(exception_state);
113 FORMAT_TYPE_ERROR2(tstate, exception_state,
114 "calling %s() should have returned an instance of BaseException, not '%s'",
115 exception_type_type, exception_value_type);
120 }
else if (PyExceptionInstance_Check(exception_state->exception_type)) {
121 if (unlikely(exception_state->exception_value != NULL && exception_state->exception_value != Py_None)) {
122 RELEASE_ERROR_OCCURRED_STATE(exception_state);
123 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_STR(tstate, exception_state, PyExc_TypeError,
124 "instance exception may not have a separate value");
130 exception_state->exception_value = exception_state->exception_type;
131 exception_state->exception_type = PyExceptionInstance_Class(exception_state->exception_type);
132 Py_INCREF(exception_state->exception_type);
136 char const *exception_type_type = Py_TYPE(exception_state->exception_type)->tp_name;
138 RELEASE_ERROR_OCCURRED_STATE(exception_state);
140 FORMAT_TYPE_ERROR1(tstate, exception_state, WRONG_EXCEPTION_TYPE_ERROR_MESSAGE, exception_type_type);
149 ASSERT_NORMALIZED_EXCEPTION_VALUE(exception_state->exception_value);
151 CHAIN_EXCEPTION(tstate, exception_state->exception_value);
155#if PYTHON_VERSION >= 0x300
157 PyObject *exception_cause) {
158 CHECK_EXCEPTION_STATE(exception_state);
159 CHECK_OBJECT(exception_cause);
161#if PYTHON_VERSION < 0x3c0
162 exception_state->exception_tb = NULL;
166 if (exception_cause == Py_None) {
167 Py_DECREF_IMMORTAL(exception_cause);
168 exception_cause = NULL;
169 }
else if (PyExceptionClass_Check(exception_cause)) {
170 PyObject *old_exception_cause = exception_cause;
171 exception_cause = CALL_FUNCTION_NO_ARGS(tstate, exception_cause);
172 Py_DECREF(old_exception_cause);
174 if (unlikely(exception_cause == NULL)) {
175 RELEASE_ERROR_OCCURRED_STATE(exception_state);
176 FETCH_ERROR_OCCURRED_STATE(tstate, exception_state);
182 if (unlikely(exception_cause != NULL && !PyExceptionInstance_Check(exception_cause))) {
183 RELEASE_ERROR_OCCURRED_STATE(exception_state);
185#ifdef _NUITKA_FULL_COMPAT
186 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_STR(tstate, exception_state, PyExc_TypeError,
187 "exception causes must derive from BaseException");
189 FORMAT_TYPE_ERROR1(tstate, exception_state,
"exception causes must derive from BaseException (%s does not)",
190 Py_TYPE(exception_cause)->tp_name);
196#if PYTHON_VERSION < 0x3c0
197 if (PyExceptionClass_Check(exception_state->exception_type)) {
198 char const *exception_type_type = Py_TYPE(exception_state->exception_type)->tp_name;
200 NORMALIZE_EXCEPTION_STATE(tstate, exception_state);
202 if (unlikely(!PyExceptionInstance_Check(exception_state->exception_value))) {
203 Py_XDECREF(exception_cause);
205 char const *exception_value_type = Py_TYPE(exception_state->exception_value)->tp_name;
207 RELEASE_ERROR_OCCURRED_STATE(exception_state);
209 FORMAT_TYPE_ERROR2(tstate, exception_state,
210 "calling %s() should have returned an instance of BaseException, not '%s'",
211 exception_type_type, exception_value_type);
216 Nuitka_Exception_SetCause(exception_state->exception_value, exception_cause);
217 CHAIN_EXCEPTION(tstate, exception_state->exception_value);
218 }
else if (PyExceptionInstance_Check(exception_state->exception_type)) {
219 exception_state->exception_value = exception_state->exception_type;
220 exception_state->exception_type = PyExceptionInstance_Class(exception_state->exception_type);
221 Py_INCREF(exception_state->exception_type);
223 Nuitka_Exception_SetCause(exception_state->exception_value, exception_cause);
224 CHAIN_EXCEPTION(tstate, exception_state->exception_value);
226 Py_XDECREF(exception_cause);
228 char const *exception_type_type = Py_TYPE(exception_state->exception_type)->tp_name;
230 RELEASE_ERROR_OCCURRED_STATE(exception_state);
232 FORMAT_TYPE_ERROR1(tstate, exception_state, WRONG_EXCEPTION_TYPE_ERROR_MESSAGE, exception_type_type);
235 ASSERT_NORMALIZED_EXCEPTION_VALUE(exception_state->exception_value);
237 Nuitka_Exception_SetCause(exception_state->exception_value, exception_cause);
238 CHAIN_EXCEPTION(tstate, exception_state->exception_value);
243#if PYTHON_VERSION < 0x300
245 if (exception_state->exception_tb == (PyTracebackObject *)Py_None) {
246 Py_DECREF_IMMORTAL(exception_state->exception_tb);
247 exception_state->exception_tb = NULL;
251 UNPACK_TUPLE_EXCEPTION_TYPE(exception_state);
253 if (PyExceptionClass_Check(exception_state->exception_type)) {
254 NORMALIZE_EXCEPTION_STATE(tstate, exception_state);
255#if PYTHON_VERSION >= 0x270
256 if (unlikely(!PyExceptionInstance_Check(exception_state->exception_value))) {
257 char const *exception_type_type = Py_TYPE(exception_state->exception_type)->tp_name;
258 char const *exception_value_type = Py_TYPE(exception_state->exception_value)->tp_name;
260 RELEASE_ERROR_OCCURRED_STATE(exception_state);
264 FORMAT_TYPE_ERROR2(tstate, exception_state,
265 "calling %s() should have returned an instance of BaseException, not '%s'",
266 exception_type_type, exception_value_type);
271 }
else if (PyExceptionInstance_Check(exception_state->exception_type)) {
272 if (unlikely(exception_state->exception_value != NULL && exception_state->exception_value != Py_None)) {
273 RELEASE_ERROR_OCCURRED_STATE(exception_state);
274 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_STR(tstate, exception_state, PyExc_TypeError,
275 "instance exception may not have a separate value");
281 exception_state->exception_value = exception_state->exception_type;
282 exception_state->exception_type = PyExceptionInstance_Class(exception_state->exception_type);
283 Py_INCREF(exception_state->exception_type);
287 FORMAT_TYPE_ERROR1(tstate, exception_state, WRONG_EXCEPTION_TYPE_ERROR_MESSAGE,
288 Py_TYPE(exception_state->exception_type)->tp_name);
296#if PYTHON_VERSION < 0x3b0
297 exception_state->exception_type = EXC_TYPE(tstate) != NULL ? EXC_TYPE(tstate) : Py_None;
298 Py_INCREF(exception_state->exception_type);
299 exception_state->exception_value = EXC_VALUE(tstate);
300 Py_XINCREF(exception_state->exception_value);
301 exception_state->exception_tb = (PyTracebackObject *)EXC_TRACEBACK(tstate);
302 Py_XINCREF(exception_state->exception_tb);
304 if (unlikely(exception_state->exception_type == Py_None)) {
305#if PYTHON_VERSION >= 0x300
306 RELEASE_ERROR_OCCURRED_STATE(exception_state);
308 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_STR(tstate, exception_state, PyExc_RuntimeError,
309 "No active exception to reraise");
311 char const *exception_type_type = Py_TYPE(exception_state->exception_type)->tp_name;
313 RELEASE_ERROR_OCCURRED_STATE(exception_state);
315 FORMAT_TYPE_ERROR1(tstate, exception_state, WRONG_EXCEPTION_TYPE_ERROR_MESSAGE, exception_type_type);
322 exception_state->exception_value = EXC_VALUE(tstate);
324 if (exception_state->exception_value == Py_None || exception_state->exception_value == NULL) {
325 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_STR(tstate, exception_state, PyExc_RuntimeError,
326 "No active exception to reraise");
330 Py_INCREF(exception_state->exception_value);
332#if PYTHON_VERSION < 0x3c0
333 exception_state->exception_type = PyExceptionInstance_Class(exception_state->exception_value);
334 Py_INCREF(exception_state->exception_type);
335 exception_state->exception_tb = GET_EXCEPTION_TRACEBACK(exception_state->exception_value);
336 Py_XINCREF(exception_state->exception_tb);
342 CHECK_EXCEPTION_STATE(exception_state);
343 CHECK_OBJECT(exception_state->exception_value);
350 PyObject *variable_name) {
351#if PYTHON_VERSION < 0x300
352 PyObject *exception_value_str =
353 Nuitka_String_FromFormat(
"name '%s' is not defined", Nuitka_String_AsString_Unchecked(variable_name));
355 PyObject *exception_value_str = Nuitka_String_FromFormat(
"name '%U' is not defined", variable_name);
358 PyObject *exception_value = MAKE_EXCEPTION_FROM_TYPE_ARG0(tstate, PyExc_NameError, exception_value_str);
359 Py_DECREF(exception_value_str);
361#if PYTHON_VERSION >= 0x300
362 CHAIN_EXCEPTION(tstate, exception_value);
365 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_VALUE1_NORMALIZED(tstate, exception_state, PyExc_NameError,
369#if PYTHON_VERSION < 0x300
370void RAISE_CURRENT_EXCEPTION_GLOBAL_NAME_ERROR(PyThreadState *tstate,
372 PyObject *variable_name) {
373 PyObject *exception_value_str =
374 Nuitka_String_FromFormat(
"global name '%s' is not defined", Nuitka_String_AsString_Unchecked(variable_name));
375 PyObject *exception_value = MAKE_EXCEPTION_FROM_TYPE_ARG0(tstate, PyExc_NameError, exception_value_str);
376 Py_DECREF(exception_value_str);
378 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0_VALUE1_NORMALIZED(tstate, exception_state, PyExc_NameError,
383PyObject *NORMALIZE_EXCEPTION_VALUE_FOR_RAISE(PyThreadState *tstate, PyObject *exception_type) {
385 CHECK_OBJECT(exception_type);
387 if (PyExceptionInstance_Check(exception_type)) {
388 return Py_NewRef(exception_type);
390 if (unlikely(!PyExceptionClass_Check(exception_type))) {
392 FORMAT_TYPE_ERROR1(tstate, &exception_state, WRONG_EXCEPTION_TYPE_ERROR_MESSAGE,
393 Py_TYPE(exception_type)->tp_name);
394 RESTORE_ERROR_OCCURRED_STATE(tstate, &exception_state);
398 PyObject *exception_value = CALL_FUNCTION_NO_ARGS(tstate, exception_type);
400 if (unlikely(exception_value == NULL)) {
404 if (unlikely(!PyExceptionInstance_Check(exception_value))) {
406 FORMAT_TYPE_ERROR2(tstate, &exception_state,
407 "calling %R should have returned an instance of BaseException, not %R",
408 (
char const *)exception_type, (
char const *)Py_TYPE(exception_value));
409 RESTORE_ERROR_OCCURRED_STATE(tstate, &exception_state);
411 Py_DECREF(exception_value);
415 return exception_value;
419#if PYTHON_VERSION >= 0x300
420PyObject *MAKE_STOP_ITERATION_EMPTY(
void) {
422 return Nuitka_CreateStopIteration(NULL, NULL);
425PyObject *MAKE_BASE_EXCEPTION_DERIVED_EMPTY(PyObject *exception_type) {
428 PyBaseExceptionObject *result = Nuitka_BaseExceptionSingleArg_new(NULL, (PyTypeObject *)exception_type, NULL);
430 return (PyObject *)result;
Definition exceptions.h:712