1 #ifndef PYTHONIC_PYTHON_EXCEPTION_HANDLER_HPP
2 #define PYTHONIC_PYTHON_EXCEPTION_HANDLER_HPP
3 
4 #ifdef ENABLE_PYTHON_MODULE
5 
6 #include "Python.h"
7 #include <utility>
8 
9 PYTHONIC_NS_BEGIN
10 
11 // This function have to be include after every others exceptions to have
12 // correct exception macro defined.
13 template <class F>
handle_python_exception(F && f)14 PyObject *handle_python_exception(F &&f)
15 {
16   try {
17     return f();
18   }
19 #ifdef PYTHONIC_BUILTIN_SYNTAXWARNING_HPP
20   catch (pythonic::types::SyntaxWarning &e) {
21     PyErr_SetString(PyExc_SyntaxWarning,
22                     pythonic::builtins::functor::str{}(e.args).c_str());
23   }
24 #endif
25 #ifdef PYTHONIC_BUILTIN_RUNTIMEWARNING_HPP
26   catch (pythonic::types::RuntimeWarning &e) {
27     PyErr_SetString(PyExc_RuntimeWarning,
28                     pythonic::builtins::functor::str{}(e.args).c_str());
29   }
30 #endif
31 #ifdef PYTHONIC_BUILTIN_DEPRECATIONWARNING_HPP
32   catch (pythonic::types::DeprecationWarning &e) {
33     PyErr_SetString(PyExc_DeprecationWarning,
34                     pythonic::builtins::functor::str{}(e.args).c_str());
35   }
36 #endif
37 #ifdef PYTHONIC_BUILTIN_IMPORTWARNING_HPP
38   catch (pythonic::types::ImportWarning &e) {
39     PyErr_SetString(PyExc_ImportWarning,
40                     pythonic::builtins::functor::str{}(e.args).c_str());
41   }
42 #endif
43 #ifdef PYTHONIC_BUILTIN_UNICODEWARNING_HPP
44   catch (pythonic::types::UnicodeWarning &e) {
45     PyErr_SetString(PyExc_UnicodeWarning,
46                     pythonic::builtins::functor::str{}(e.args).c_str());
47   }
48 #endif
49 #ifdef PYTHONIC_BUILTIN_BYTESWARNING_HPP
50   catch (pythonic::types::BytesWarning &e) {
51     PyErr_SetString(PyExc_BytesWarning,
52                     pythonic::builtins::functor::str{}(e.args).c_str());
53   }
54 #endif
55 #ifdef PYTHONIC_BUILTIN_USERWARNING_HPP
56   catch (pythonic::types::UserWarning &e) {
57     PyErr_SetString(PyExc_UserWarning,
58                     pythonic::builtins::functor::str{}(e.args).c_str());
59   }
60 #endif
61 #ifdef PYTHONIC_BUILTIN_FUTUREWARNING_HPP
62   catch (pythonic::types::FutureWarning &e) {
63     PyErr_SetString(PyExc_FutureWarning,
64                     pythonic::builtins::functor::str{}(e.args).c_str());
65   }
66 #endif
67 #ifdef PYTHONIC_BUILTIN_PENDINGDEPRECATIONWARNING_HPP
68   catch (pythonic::types::PendingDeprecationWarning &e) {
69     PyErr_SetString(PyExc_PendingDeprecationWarning,
70                     pythonic::builtins::functor::str{}(e.args).c_str());
71   }
72 #endif
73 #ifdef PYTHONIC_BUILTIN_WARNING_HPP
74   catch (pythonic::types::Warning &e) {
75     PyErr_SetString(PyExc_Warning,
76                     pythonic::builtins::functor::str{}(e.args).c_str());
77   }
78 #endif
79 #ifdef PYTHONIC_BUILTIN_UNICODEERROR_HPP
80   catch (pythonic::types::UnicodeError &e) {
81     PyErr_SetString(PyExc_UnicodeError,
82                     pythonic::builtins::functor::str{}(e.args).c_str());
83   }
84 #endif
85 #ifdef PYTHONIC_BUILTIN_VALUEERROR_HPP
86   catch (pythonic::types::ValueError &e) {
87     PyErr_SetString(PyExc_ValueError,
88                     pythonic::builtins::functor::str{}(e.args).c_str());
89   }
90 #endif
91 #ifdef PYTHONIC_BUILTIN_TYPEERROR_HPP
92   catch (pythonic::types::TypeError &e) {
93     PyErr_SetString(PyExc_TypeError,
94                     pythonic::builtins::functor::str{}(e.args).c_str());
95   }
96 #endif
97 #ifdef PYTHONIC_BUILTIN_SYSTEMERROR_HPP
98   catch (pythonic::types::SystemError &e) {
99     PyErr_SetString(PyExc_SystemError,
100                     pythonic::builtins::functor::str{}(e.args).c_str());
101   }
102 #endif
103 #ifdef PYTHONIC_BUILTIN_TABERROR_HPP
104   catch (pythonic::types::TabError &e) {
105     PyErr_SetString(PyExc_TabError,
106                     pythonic::builtins::functor::str{}(e.args).c_str());
107   }
108 #endif
109 #ifdef PYTHONIC_BUILTIN_INDENTATIONERROR_HPP
110   catch (pythonic::types::IndentationError &e) {
111     PyErr_SetString(PyExc_IndentationError,
112                     pythonic::builtins::functor::str{}(e.args).c_str());
113   }
114 #endif
115 #ifdef PYTHONIC_BUILTIN_SYNTAXERROR_HPP
116   catch (pythonic::types::SyntaxError &e) {
117     PyErr_SetString(PyExc_SyntaxError,
118                     pythonic::builtins::functor::str{}(e.args).c_str());
119   }
120 #endif
121 #ifdef PYTHONIC_BUILTIN_NOTIMPLEMENTEDERROR_HPP
122   catch (pythonic::types::NotImplementedError &e) {
123     PyErr_SetString(PyExc_NotImplementedError,
124                     pythonic::builtins::functor::str{}(e.args).c_str());
125   }
126 #endif
127 #ifdef PYTHONIC_BUILTIN_RUNTIMEERROR_HPP
128   catch (pythonic::types::RuntimeError &e) {
129     PyErr_SetString(PyExc_RuntimeError,
130                     pythonic::builtins::functor::str{}(e.args).c_str());
131   }
132 #endif
133 #ifdef PYTHONIC_BUILTIN_REFERENCEERROR_HPP
134   catch (pythonic::types::ReferenceError &e) {
135     PyErr_SetString(PyExc_ReferenceError,
136                     pythonic::builtins::functor::str{}(e.args).c_str());
137   }
138 #endif
139 #ifdef PYTHONIC_BUILTIN_UNBOUNDLOCALERROR_HPP
140   catch (pythonic::types::UnboundLocalError &e) {
141     PyErr_SetString(PyExc_UnboundLocalError,
142                     pythonic::builtins::functor::str{}(e.args).c_str());
143   }
144 #endif
145 #ifdef PYTHONIC_BUILTIN_NAMEERROR_HPP
146   catch (pythonic::types::NameError &e) {
147     PyErr_SetString(PyExc_NameError,
148                     pythonic::builtins::functor::str{}(e.args).c_str());
149   }
150 #endif
151 #ifdef PYTHONIC_BUILTIN_MEMORYERROR_HPP
152   catch (pythonic::types::MemoryError &e) {
153     PyErr_SetString(PyExc_MemoryError,
154                     pythonic::builtins::functor::str{}(e.args).c_str());
155   }
156 #endif
157 #ifdef PYTHONIC_BUILTIN_KEYERROR_HPP
158   catch (pythonic::types::KeyError &e) {
159     PyErr_SetString(PyExc_KeyError,
160                     pythonic::builtins::functor::str{}(e.args).c_str());
161   }
162 #endif
163 #ifdef PYTHONIC_BUILTIN_INDEXERROR_HPP
164   catch (pythonic::types::IndexError &e) {
165     PyErr_SetString(PyExc_IndexError,
166                     pythonic::builtins::functor::str{}(e.args).c_str());
167   }
168 #endif
169 #ifdef PYTHONIC_BUILTIN_LOOKUPERROR_HPP
170   catch (pythonic::types::LookupError &e) {
171     PyErr_SetString(PyExc_LookupError,
172                     pythonic::builtins::functor::str{}(e.args).c_str());
173   }
174 #endif
175 #ifdef PYTHONIC_BUILTIN_IMPORTERROR_HPP
176   catch (pythonic::types::ImportError &e) {
177     PyErr_SetString(PyExc_ImportError,
178                     pythonic::builtins::functor::str{}(e.args).c_str());
179   }
180 #endif
181 #ifdef PYTHONIC_BUILTIN_EOFERROR_HPP
182   catch (pythonic::types::EOFError &e) {
183     PyErr_SetString(PyExc_EOFError,
184                     pythonic::builtins::functor::str{}(e.args).c_str());
185   }
186 #endif
187 #ifdef PYTHONIC_BUILTIN_OSERROR_HPP
188   catch (pythonic::types::OSError &e) {
189     PyErr_SetString(PyExc_OSError,
190                     pythonic::builtins::functor::str{}(e.args).c_str());
191   }
192 #endif
193 #ifdef PYTHONIC_BUILTIN_IOERROR_HPP
194   catch (pythonic::types::IOError &e) {
195     PyErr_SetString(PyExc_IOError,
196                     pythonic::builtins::functor::str{}(e.args).c_str());
197   }
198 #endif
199 #ifdef PYTHONIC_BUILTIN_ENVIRONMENTERROR_HPP
200   catch (pythonic::types::EnvironmentError &e) {
201     PyErr_SetString(PyExc_EnvironmentError,
202                     pythonic::builtins::functor::str{}(e.args).c_str());
203   }
204 #endif
205 #ifdef PYTHONIC_BUILTIN_ATTRIBUTEERROR_HPP
206   catch (pythonic::types::AttributeError &e) {
207     PyErr_SetString(PyExc_AttributeError,
208                     pythonic::builtins::functor::str{}(e.args).c_str());
209   }
210 #endif
211 #ifdef PYTHONIC_BUILTIN_ASSERTIONERROR_HPP
212   catch (pythonic::types::AssertionError &e) {
213     PyErr_SetString(PyExc_AssertionError,
214                     pythonic::builtins::functor::str{}(e.args).c_str());
215   }
216 #endif
217 #ifdef PYTHONIC_BUILTIN_ZERODIVISIONERROR_HPP
218   catch (pythonic::types::ZeroDivisionError &e) {
219     PyErr_SetString(PyExc_ZeroDivisionError,
220                     pythonic::builtins::functor::str{}(e.args).c_str());
221   }
222 #endif
223 #ifdef PYTHONIC_BUILTIN_OVERFLOWERROR_HPP
224   catch (pythonic::types::OverflowError &e) {
225     PyErr_SetString(PyExc_OverflowError,
226                     pythonic::builtins::functor::str{}(e.args).c_str());
227   }
228 #endif
229 #ifdef PYTHONIC_BUILTIN_FLOATINGPOINTERROR_HPP
230   catch (pythonic::types::FloatingPointError &e) {
231     PyErr_SetString(PyExc_FloatingPointError,
232                     pythonic::builtins::functor::str{}(e.args).c_str());
233   }
234 #endif
235 #ifdef PYTHONIC_BUILTIN_ARITHMETICERROR_HPP
236   catch (pythonic::types::ArithmeticError &e) {
237     PyErr_SetString(PyExc_ArithmeticError,
238                     pythonic::builtins::functor::str{}(e.args).c_str());
239   }
240 #endif
241 #ifdef PYTHONIC_BUILTIN_FILENOTFOUNDERROR_HPP
242   catch (pythonic::types::FileNotFoundError &e) {
243     PyErr_SetString(PyExc_FileNotFoundError,
244                     pythonic::builtins::functor::str{}(e.args).c_str());
245   }
246 #endif
247 #ifdef PYTHONIC_BUILTIN_BUFFERERROR_HPP
248   catch (pythonic::types::BufferError &e) {
249     PyErr_SetString(PyExc_BufferError,
250                     pythonic::builtins::functor::str{}(e.args).c_str());
251   }
252 #endif
253 #ifdef PYTHONIC_BUILTIN_STANDARDERROR_HPP
254   catch (pythonic::types::StandardError &e) {
255     PyErr_SetString(PyExc_StandardError,
256                     pythonic::builtins::functor::str{}(e.args).c_str());
257   }
258 #endif
259 #ifdef PYTHONIC_BUILTIN_STOPITERATION_HPP
260   catch (pythonic::types::StopIteration &e) {
261     PyErr_SetString(PyExc_StopIteration,
262                     pythonic::builtins::functor::str{}(e.args).c_str());
263   }
264 #endif
265 #ifdef PYTHONIC_BUILTIN_EXCEPTION_HPP
266   catch (pythonic::types::Exception &e) {
267     PyErr_SetString(PyExc_Exception,
268                     pythonic::builtins::functor::str{}(e.args).c_str());
269   }
270 #endif
271 #ifdef PYTHONIC_BUILTIN_GENERATOREXIT_HPP
272   catch (pythonic::types::GeneratorExit &e) {
273     PyErr_SetString(PyExc_GeneratorExit,
274                     pythonic::builtins::functor::str{}(e.args).c_str());
275   }
276 #endif
277 #ifdef PYTHONIC_BUILTIN_KEYBOARDINTERRUPT_HPP
278   catch (pythonic::types::KeyboardInterrupt &e) {
279     PyErr_SetString(PyExc_KeyboardInterrupt,
280                     pythonic::builtins::functor::str{}(e.args).c_str());
281   }
282 #endif
283 #ifdef PYTHONIC_BUILTIN_SYSTEMEXIT_HPP
284   catch (pythonic::types::SystemExit &e) {
285     PyErr_SetString(PyExc_SystemExit,
286                     pythonic::builtins::functor::str{}(e.args).c_str());
287   }
288 #endif
289 #ifdef PYTHONIC_BUILTIN_BASEEXCEPTION_HPP
290   catch (pythonic::types::BaseException &e) {
291     PyErr_SetString(PyExc_BaseException,
292                     pythonic::builtins::functor::str{}(e.args).c_str());
293   }
294 #endif
295   catch (...) {
296     PyErr_SetString(PyExc_RuntimeError,
297                     "Something happened on the way to heaven");
298   }
299   return nullptr;
300 }
301 PYTHONIC_NS_END
302 
303 #endif
304 
305 #endif
306