| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
r |
| 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 |
#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 |
|
| 40 |
|
| 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 |
|
| 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 |
|
| 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", |
| 229 |
sizeof(PycairoErrorObject), |
| 230 |
0, |
| 231 |
0, |
| 232 |
0, |
| 233 |
0, |
| 234 |
0, |
| 235 |
0, |
| 236 |
0, |
| 237 |
0, |
| 238 |
0, |
| 239 |
0, |
| 240 |
0, |
| 241 |
0, |
| 242 |
(reprfunc)error_str, |
| 243 |
0, |
| 244 |
0, |
| 245 |
0, |
| 246 |
Py_TPFLAGS_DEFAULT | |
| 247 |
Py_TPFLAGS_BASETYPE, |
| 248 |
0, |
| 249 |
0, |
| 250 |
0, |
| 251 |
0, |
| 252 |
0, |
| 253 |
0, |
| 254 |
0, |
| 255 |
error_methods, |
| 256 |
0, |
| 257 |
error_getset, |
| 258 |
0, |
| 259 |
0, |
| 260 |
0, |
| 261 |
0, |
| 262 |
0, |
| 263 |
(initproc)error_init, |
| 264 |
0, |
| 265 |
0, |
| 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 |
} |