Debian Codegraph @

cairo/error.c

raw symbols SCIP
1 /* -*- mode: C; c-basic-offset: 2 -*-
2 *
3 * Pycairo - Python bindings for cairo
4 *
5 * Copyright © 2017 Christoph Reiter
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
14 *
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
20 *
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
25 *
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
29 */
30
31 #define PY_SSIZE_T_CLEAN
32 #include <Python.h>
33 #include "private.h"
34
35 static PyTypeObject PycairoError_Type;
36 static PyObject *error_get_type_combined (
37 PyObject *error, PyObject *other, char *name);
38
39 /* Like cairo_status_to_string(), but translates some C function names to
40 * Python function names.
41 */
42 static const char*
43 status_to_string(cairo_status_t status)
44 {
45 if (status == CAIRO_STATUS_INVALID_RESTORE)
46 return "Context.restore() without matching Context.save()";
47 else if (status == CAIRO_STATUS_INVALID_POP_GROUP)
48 return "Context.pop_group() without matching Context.push_group()";
49 else
50 return cairo_status_to_string(status);
51 }
52
53 /* Sets an exception based on a cairo_status_t */
54 static void
55 set_error (PyObject *error_type, cairo_status_t status)
56 {
57 PyObject *args, *v, *int_enum;
58
59 int_enum = CREATE_INT_ENUM(Status, status);
60 if (int_enum == NULL)
61 return;
62 args = Py_BuildValue("(sO)", status_to_string(status), int_enum);
63 Py_DECREF (int_enum);
64 v = PyObject_Call(error_type, args, NULL);
65 Py_DECREF(args);
66 if (v != NULL) {
67 PyErr_SetObject((PyObject *)Py_TYPE(v), v);
68 Py_DECREF(v);
69 }
70 }
71
72 int
73 Pycairo_Check_Status (cairo_status_t status) {
74 PyObject *module, *error, *suberror;
75
76 if (PyErr_Occurred() != NULL)
77 return 1;
78
79 if (status == CAIRO_STATUS_SUCCESS)
80 return 0;
81
82 module = PyImport_ImportModule ("cairo");
83 if (module == NULL)
84 return 1;
85 error = PyObject_GetAttrString (module, "Error");
86 Py_DECREF (module);
87 if (error == NULL)
88 return 1;
89
90 switch (status) {
91 case CAIRO_STATUS_NO_MEMORY:
92 suberror = error_get_type_combined (
93 error, PyExc_MemoryError, "cairo.MemoryError");
94 set_error (suberror, status);
95 Py_DECREF (suberror);
96 break;
97 case CAIRO_STATUS_READ_ERROR:
98 case CAIRO_STATUS_WRITE_ERROR:
99 suberror = error_get_type_combined (
100 error, PyExc_IOError, "cairo.IOError");
101 set_error (suberror, status);
102 Py_DECREF (suberror);
103 break;
104 default:
105 set_error (error, status);
106 }
107
108 Py_DECREF (error);
109
110 return 1;
111 }
112
113 typedef struct {
114 PyBaseExceptionObject base;
115 } PycairoErrorObject;
116
117 static PyObject *
118 error_get_args(PycairoErrorObject *self) {
119 PyObject *args;
120
121 args = PyObject_GetAttrString((PyObject *)self, "args");
122 if (args == NULL)
123 return NULL;
124
125 if (!PyTuple_Check(args)) {
126 PyErr_SetString(PyExc_TypeError, ".args not a tuple");
127 Py_DECREF(args);
128 return NULL;
129 }
130
131 return args;
132 }
133
134 static int
135 error_init(PycairoErrorObject *self, PyObject *args, PyObject *kwds)
136 {
137 PyObject *status_obj, *error_args;
138
139 if (PycairoError_Type.tp_base->tp_init((PyObject *)self, args, kwds) < 0)
140 return -1;
141
142 error_args = error_get_args(self);
143 if (error_args == NULL)
144 return -1;
145
146 if(PyTuple_GET_SIZE(error_args) >= 2) {
147 status_obj = PyTuple_GET_ITEM(error_args, 1);
148 } else {
149 status_obj = Py_None;
150 }
151
152 Py_DECREF(error_args);
153
154 if (PyObject_SetAttrString ((PyObject *)self, "__status", status_obj) < 0)
155 return -1;
156
157 return 0;
158 }
159
160 static PyObject *
161 error_get_status(PycairoErrorObject *self, void *closure)
162 {
163 return PyObject_GetAttrString ((PyObject *)self, "__status");
164 }
165
166 static int
167 error_set_status(PycairoErrorObject *self, PyObject *value, void *closure)
168 {
169 if (value == NULL) {
170 PyErr_SetString(PyExc_TypeError, "Cannot delete attribute");
171 return -1;
172 }
173
174 if (PyObject_SetAttrString ((PyObject *)self, "__status", value) < 0)
175 return -1;
176
177 return 0;
178 }
179
180 static PyGetSetDef error_getset[] = {
181 {"status", (getter)error_get_status, (setter)error_set_status,},
182 {NULL,},
183 };
184
185 static PyObject *
186 error_str(PycairoErrorObject *self)
187 {
188 PyObject *result, *error_args;
189
190 error_args = error_get_args(self);
191 if (error_args == NULL)
192 return NULL;
193
194 /* Default to printing just the message */
195 if (PyTuple_GET_SIZE(error_args) >= 1) {
196 result = PyObject_Str(PyTuple_GET_ITEM(error_args, 0));
197 } else {
198 result = PycairoError_Type.tp_base->tp_str((PyObject*)self);
199 }
200
201 Py_DECREF(error_args);
202 return result;
203 }
204
205 static PyObject *
206 error_check_status (PyTypeObject *type, PyObject *args) {
207 cairo_status_t status;
208 int status_arg;
209
210 if (!PyArg_ParseTuple (args, "i:Error._check_status", &status_arg))
211 return NULL;
212
213 status = (cairo_status_t)status_arg;
214
215 if (Pycairo_Check_Status (status))
216 return NULL;
217
218 Py_RETURN_NONE;
219 }
220
221 static PyMethodDef error_methods[] = {
222 {"_check_status", (PyCFunction)error_check_status, METH_VARARGS | METH_CLASS},
223 {NULL, NULL, 0, NULL},
224 };
225
226 static PyTypeObject PycairoError_Type = {
227 PyVarObject_HEAD_INIT(NULL, 0)
228 "cairo.Error", /* tp_name */
229 sizeof(PycairoErrorObject),/* tp_basicsize */
230 0, /* tp_itemsize */
231 0, /* tp_dealloc */
232 0, /* tp_print */
233 0, /* tp_getattr */
234 0, /* tp_setattr */
235 0, /* tp_reserved */
236 0, /* tp_repr */
237 0, /* tp_as_number */
238 0, /* tp_as_sequence */
239 0, /* tp_as_mapping */
240 0, /* tp_hash */
241 0, /* tp_call */
242 (reprfunc)error_str, /* tp_str */
243 0, /* tp_getattro */
244 0, /* tp_setattro */
245 0, /* tp_as_buffer */
246 Py_TPFLAGS_DEFAULT |
247 Py_TPFLAGS_BASETYPE, /* tp_flags */
248 0, /* tp_doc */
249 0, /* tp_traverse */
250 0, /* tp_clear */
251 0, /* tp_richcompare */
252 0, /* tp_weaklistoffset */
253 0, /* tp_iter */
254 0, /* tp_iternext */
255 error_methods, /* tp_methods */
256 0, /* tp_members */
257 error_getset, /* tp_getset */
258 0, /* tp_base */
259 0, /* tp_dict */
260 0, /* tp_descr_get */
261 0, /* tp_descr_set */
262 0, /* tp_dictoffset */
263 (initproc)error_init, /* tp_init */
264 0, /* tp_alloc */
265 0, /* tp_new */
266 };
267
268 int
269 init_error (PyObject *module) {
270 PyObject *error;
271
272 PycairoError_Type.tp_base = (PyTypeObject*)PyExc_Exception;
273 if (PyType_Ready(&PycairoError_Type) < 0)
274 return -1;
275
276 error = (PyObject*)&PycairoError_Type;
277
278 Py_INCREF(error);
279 if (PyModule_AddObject(module, "Error", error) < 0) {
280 Py_DECREF (error);
281 return -1;
282 }
283
284 Py_INCREF(error);
285 if (PyModule_AddObject(module, "CairoError", error) < 0) {
286 Py_DECREF (error);
287 return -1;
288 }
289
290 return 0;
291 }
292
293 static PyObject *
294 error_get_type_combined (PyObject *error, PyObject *other, char* name) {
295 PyObject *class_dict, *new_type_args;
296 PyObject *new_type;
297
298 class_dict = PyDict_New ();
299 if (class_dict == NULL)
300 return NULL;
301
302 new_type_args = Py_BuildValue ("s(OO)O", name,
303 error, other, class_dict);
304 Py_DECREF (class_dict);
305 if (new_type_args == NULL)
306 return NULL;
307
308 new_type = PyType_Type.tp_new (&PyType_Type, new_type_args, NULL);
309 return new_type;
310 }