-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.hh
204 lines (183 loc) · 5.33 KB
/
exceptions.hh
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
#ifndef CAPY_EXCEPTIONS_HH
#define CAPY_EXCEPTIONS_HH
#include <exception>
#include <new>
// This header reproduces part of the Python exception hierarchy for
// use by C++ code. Throwing one of these exceptions will result in
// the corresponding Python exception being raised in the Python
// interpreter.
namespace Capy
{
// Exception occured in Python API function. We don't need any
// further information as we can rely on Python's exception
// information.
class ExceptionInPythonAPI : public std::exception {};
inline PyObject *check_error(PyObject *obj)
{
if (!obj)
throw ExceptionInPythonAPI();
return obj;
}
inline bool check_error(bool value)
{
if (value == -1)
throw ExceptionInPythonAPI();
return value;
}
inline int check_error(int value)
{
if (value == -1)
throw ExceptionInPythonAPI();
return value;
}
#if __SIZEOF_SIZE_T__ != __SIZEOF_INT__
inline ssize_t check_error(ssize_t value)
{
if (value == -1)
throw ExceptionInPythonAPI();
return value;
}
#endif
inline long check_error(long value)
{
if (value == -1 && PyErr_Occurred())
throw ExceptionInPythonAPI();
return value;
}
inline double check_error(double value)
{
if (value == -1 && PyErr_Occurred())
throw ExceptionInPythonAPI();
return value;
}
inline const char *check_error(const char *value)
{
if (!value)
throw ExceptionInPythonAPI();
return value;
}
class Exception : public std::exception
{
public:
Exception(const char *msg_, PyObject *pyexc_ = PyExc_Exception)
: msg(msg_), pyexc(pyexc_) {}
const char *what() const throw()
{
return msg;
}
void raise() const throw()
{
PyErr_SetString(pyexc, msg);
}
private:
const char *msg;
PyObject *pyexc;
};
class StandardError : public Exception
{
public:
StandardError(const char *msg_, PyObject *pyexc_ = PyExc_StandardError)
: Exception(msg_, pyexc_) {}
};
class ArithmeticError : public StandardError
{
public:
ArithmeticError(const char *msg_, PyObject *pyexc_ = PyExc_ArithmeticError)
: StandardError(msg_, pyexc_) {}
};
class FloatingPointError : public ArithmeticError
{
public:
FloatingPointError(const char *msg_, PyObject *pyexc_ = PyExc_FloatingPointError)
: ArithmeticError(msg_, pyexc_) {}
};
class OverflowError : public ArithmeticError
{
public:
OverflowError(const char *msg_, PyObject *pyexc_ = PyExc_OverflowError)
: ArithmeticError(msg_, pyexc_) {}
};
class ZeroDivisionError : public ArithmeticError
{
public:
ZeroDivisionError(const char *msg_, PyObject *pyexc_ = PyExc_ZeroDivisionError)
: ArithmeticError(msg_, pyexc_) {}
};
class AssertionError : public StandardError
{
public:
AssertionError(const char *msg_, PyObject *pyexc_ = PyExc_AssertionError)
: StandardError(msg_, pyexc_) {}
};
class EnvironmentError : public StandardError
{
public:
EnvironmentError(const char *msg_, PyObject *pyexc_ = PyExc_EnvironmentError)
: StandardError(msg_, pyexc_) {}
};
class IOError : public EnvironmentError
{
public:
IOError(const char *msg_, PyObject *pyexc_ = PyExc_IOError)
: EnvironmentError(msg_, pyexc_) {}
};
class OSError : public EnvironmentError
{
public:
OSError(const char *msg_, PyObject *pyexc_ = PyExc_OSError)
: EnvironmentError(msg_, pyexc_) {}
};
class MemoryError : public StandardError
{
public:
MemoryError(const char *msg_, PyObject *pyexc_ = PyExc_MemoryError)
: StandardError(msg_, pyexc_) {}
};
class RuntimeError : public StandardError
{
public:
RuntimeError(const char *msg_, PyObject *pyexc_ = PyExc_RuntimeError)
: StandardError(msg_, pyexc_) {}
};
class NotImplementedError : public RuntimeError
{
public:
NotImplementedError(const char *msg_, PyObject *pyexc_ = PyExc_NotImplementedError)
: RuntimeError(msg_, pyexc_) {}
};
class TypeError : public StandardError
{
public:
TypeError(const char *msg_, PyObject *pyexc_ = PyExc_TypeError)
: StandardError(msg_, pyexc_) {}
};
class ValueError : public StandardError
{
public:
ValueError(const char *msg_, PyObject *pyexc_ = PyExc_ValueError)
: StandardError(msg_, pyexc_) {}
};
template <PyCFunction f>
static PyObject *
check_call(PyObject *self, PyObject *args)
{
try {
return f(self, args);
}
catch (ExceptionInPythonAPI &e) {}
catch (Exception &e) {
e.raise();
}
catch (std::bad_alloc &e) {
MemoryError("Failed memory allocation in C++ code").raise();
}
catch (std::exception &e) {
RuntimeError(e.what()).raise();
}
catch (...) {
RuntimeError("Unknown C++ exception occurred").raise();
}
return 0;
}
}
#endif