Nuitka
The Python compiler
Loading...
Searching...
No Matches
raising.h
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_HELPER_RAISING_H__
4#define __NUITKA_HELPER_RAISING_H__
5
6#if PYTHON_VERSION >= 0x300
7NUITKA_MAY_BE_UNUSED static void CHAIN_EXCEPTION(PyThreadState *tstate, PyObject *exception_value) {
8 // Implicit chain of exception already existing.
9
10 // Normalize existing published exception first.
11#if PYTHON_VERSION < 0x3b0
12 {
13 // TODO: Revert to using NORMALIZE_EXCEPTION
14 struct Nuitka_ExceptionPreservationItem *exception_state =
15 (struct Nuitka_ExceptionPreservationItem *)&EXC_TYPE(tstate);
16 NORMALIZE_EXCEPTION_STATE(tstate, exception_state);
17 }
18#endif
19
20 PyObject *old_exc_value = EXC_VALUE(tstate);
21
22 if (old_exc_value != NULL && old_exc_value != Py_None && old_exc_value != exception_value) {
23 PyObject *current = old_exc_value;
24 while (true) {
25 PyObject *context = Nuitka_Exception_GetContext(current);
26 if (context == NULL) {
27 break;
28 }
29
30 CHECK_OBJECT(context);
31
32 if (context == exception_value) {
33 Nuitka_Exception_DeleteContext(current);
34 break;
35 }
36
37 current = context;
38 }
39
40 CHECK_OBJECT(old_exc_value);
41 Nuitka_Exception_SetContext(exception_value, old_exc_value);
42
43#if PYTHON_VERSION < 0x3b0
44 CHECK_OBJECT(EXC_TRACEBACK(tstate));
45 ATTACH_TRACEBACK_TO_EXCEPTION_VALUE(old_exc_value, (PyTracebackObject *)EXC_TRACEBACK(tstate));
46#endif
47 }
48}
49#endif
50
51#if PYTHON_VERSION < 0x3c0
52extern void RAISE_EXCEPTION_WITH_TYPE(PyThreadState *tstate, struct Nuitka_ExceptionPreservationItem *exception_state);
53extern void RAISE_EXCEPTION_WITH_TYPE_AND_VALUE(PyThreadState *tstate,
54 struct Nuitka_ExceptionPreservationItem *exception_state);
55#else
56extern void RAISE_EXCEPTION_WITH_VALUE(PyThreadState *tstate, struct Nuitka_ExceptionPreservationItem *exception_state);
57#endif
58
59#if PYTHON_VERSION < 0x300
60extern void RAISE_EXCEPTION_WITH_TRACEBACK(PyThreadState *tstate,
61 struct Nuitka_ExceptionPreservationItem *exception_state);
62#else
63extern void RAISE_EXCEPTION_WITH_CAUSE(PyThreadState *tstate, struct Nuitka_ExceptionPreservationItem *exception_state,
64 PyObject *exception_cause);
65#endif
66
67extern bool RERAISE_EXCEPTION(PyThreadState *tstate, struct Nuitka_ExceptionPreservationItem *exception_state);
68
69extern void RAISE_CURRENT_EXCEPTION_NAME_ERROR(PyThreadState *tstate,
70 struct Nuitka_ExceptionPreservationItem *exception_state,
71 PyObject *variable_name);
72
73#if PYTHON_VERSION < 0x300
74extern void RAISE_CURRENT_EXCEPTION_GLOBAL_NAME_ERROR(PyThreadState *tstate,
75 struct Nuitka_ExceptionPreservationItem *exception_state,
76 PyObject *variable_name);
77#endif
78
79extern PyObject *NORMALIZE_EXCEPTION_VALUE_FOR_RAISE(PyThreadState *tstate, PyObject *exception_type);
80
81#if PYTHON_VERSION >= 0x300
82extern PyObject *MAKE_STOP_ITERATION_EMPTY(void);
83extern PyObject *MAKE_BASE_EXCEPTION_DERIVED_EMPTY(PyObject *exception_type);
84#endif
85
86NUITKA_MAY_BE_UNUSED static inline void
87SET_EXCEPTION_PRESERVATION_STATE_STOP_ITERATION_EMPTY(PyThreadState *tstate,
88 struct Nuitka_ExceptionPreservationItem *exception_state) {
89#if PYTHON_VERSION < 0x3c0
90 SET_EXCEPTION_PRESERVATION_STATE_FROM_TYPE0(tstate, exception_state, PyExc_StopIteration);
91#else
92 exception_state->exception_value = MAKE_STOP_ITERATION_EMPTY();
93#endif
94}
95
96// Create an exception value object from type and value input.
97extern PyObject *MAKE_EXCEPTION_WITH_VALUE(PyThreadState *tstate, PyObject *exception_type, PyObject *value);
98
99#endif
100
101// Part of "Nuitka", an optimizing Python compiler that is compatible and
102// integrates with CPython, but also works on its own.
103//
104// Licensed under the Apache License, Version 2.0 (the "License");
105// you may not use this file except in compliance with the License.
106// You may obtain a copy of the License at
107//
108// http://www.apache.org/licenses/LICENSE-2.0
109//
110// Unless required by applicable law or agreed to in writing, software
111// distributed under the License is distributed on an "AS IS" BASIS,
112// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
113// See the License for the specific language governing permissions and
114// limitations under the License.
Definition exceptions.h:712