| 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 |
|
| 34 |
#include "private.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
int |
| 40 |
Pycairo_is_fspath (PyObject *obj) { |
| 41 |
PyObject *real = PyOS_FSPath (obj); |
| 42 |
if (real == NULL) { |
| 43 |
PyErr_Clear (); |
| 44 |
return 0; |
| 45 |
} else { |
| 46 |
Py_DECREF (real); |
| 47 |
return 1; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#if !defined(MS_WINDOWS) |
| 52 |
|
| 53 |
static int |
| 54 |
Pycairo_PyUnicode_FSConverter(PyObject* obj, void* result) { |
| 55 |
int res; |
| 56 |
PyObject *real = NULL; |
| 57 |
real = PyOS_FSPath (obj); |
| 58 |
if (real == NULL) { |
| 59 |
PyErr_Clear (); |
| 60 |
return PyUnicode_FSConverter (obj, result); |
| 61 |
} else { |
| 62 |
res = PyUnicode_FSConverter (real, result); |
| 63 |
Py_DECREF (real); |
| 64 |
return res; |
| 65 |
} |
| 66 |
} |
| 67 |
#endif |
| 68 |
|
| 69 |
#if defined(MS_WINDOWS) |
| 70 |
|
| 71 |
static int |
| 72 |
Pycairo_PyUnicode_FSDecoder(PyObject* obj, void* result) { |
| 73 |
int res; |
| 74 |
PyObject *real = NULL; |
| 75 |
real = PyOS_FSPath (obj); |
| 76 |
if (real == NULL) { |
| 77 |
PyErr_Clear (); |
| 78 |
return PyUnicode_FSDecoder (obj, result); |
| 79 |
} else { |
| 80 |
res = PyUnicode_FSDecoder (real, result); |
| 81 |
Py_DECREF (real); |
| 82 |
return res; |
| 83 |
} |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
int |
| 92 |
Pycairo_fspath_converter (PyObject *obj, char** result) { |
| 93 |
char *internal, *buf; |
| 94 |
PyObject *bytes; |
| 95 |
|
| 96 |
#if defined(MS_WINDOWS) |
| 97 |
PyObject *uni; |
| 98 |
|
| 99 |
if (Pycairo_PyUnicode_FSDecoder (obj, &uni) == 0) |
| 100 |
return 0; |
| 101 |
|
| 102 |
if (cairo_version() >= CAIRO_VERSION_ENCODE(1, 15, 10)) { |
| 103 |
bytes = PyUnicode_AsEncodedString (uni, "utf-8", "strict"); |
| 104 |
} else { |
| 105 |
bytes = PyUnicode_AsMBCSString (uni); |
| 106 |
} |
| 107 |
|
| 108 |
Py_DECREF (uni); |
| 109 |
if (bytes == NULL) |
| 110 |
return 0; |
| 111 |
|
| 112 |
if (PyBytes_AsStringAndSize (bytes, &internal, NULL) == -1) { |
| 113 |
Py_DECREF (bytes); |
| 114 |
return 0; |
| 115 |
} |
| 116 |
#else |
| 117 |
if (Pycairo_PyUnicode_FSConverter (obj, &bytes) == 0) |
| 118 |
return 0; |
| 119 |
|
| 120 |
if (PyBytes_AsStringAndSize (bytes, &internal, NULL) == -1) { |
| 121 |
Py_DECREF (bytes); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
#endif |
| 125 |
|
| 126 |
buf = PyMem_Malloc (strlen (internal) + 1); |
| 127 |
if (buf == NULL) { |
| 128 |
Py_DECREF (bytes); |
| 129 |
PyErr_NoMemory (); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
strcpy (buf, internal); |
| 133 |
Py_DECREF (bytes); |
| 134 |
*result = buf; |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
int |
| 142 |
Pycairo_writer_converter (PyObject *obj, PyObject** file) { |
| 143 |
PyObject *res; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
res = PyObject_CallMethod (obj, "write", "(y#)", "", (Py_ssize_t) 0); |
| 148 |
if (res == NULL) |
| 149 |
return 0; |
| 150 |
Py_DECREF (res); |
| 151 |
|
| 152 |
*file = obj; |
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
int |
| 157 |
Pycairo_reader_converter (PyObject *obj, PyObject** file) { |
| 158 |
PyObject *res; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
res = PyObject_CallMethod (obj, "read", "(i)", (int) 0); |
| 163 |
if (res == NULL) |
| 164 |
return 0; |
| 165 |
|
| 166 |
if (!PyBytes_Check (res)) { |
| 167 |
Py_DECREF (res); |
| 168 |
PyErr_SetString ( |
| 169 |
PyExc_TypeError, "'read' does not return bytes"); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
Py_DECREF (res); |
| 173 |
|
| 174 |
*file = obj; |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
int |
| 179 |
Pycairo_fspath_none_converter (PyObject *obj, char** result) { |
| 180 |
if (obj == Py_None) { |
| 181 |
*result = NULL; |
| 182 |
return 1; |
| 183 |
} |
| 184 |
|
| 185 |
return Pycairo_fspath_converter (obj, result); |
| 186 |
} |
| 187 |
|
| 188 |
PyObject* |
| 189 |
Pycairo_richcompare (void* a, void *b, int op) |
| 190 |
{ |
| 191 |
PyObject *res; |
| 192 |
|
| 193 |
switch (op) { |
| 194 |
case Py_EQ: |
| 195 |
res = (a == b) ? Py_True : Py_False; |
| 196 |
break; |
| 197 |
case Py_NE: |
| 198 |
res = (a != b) ? Py_True : Py_False; |
| 199 |
break; |
| 200 |
case Py_LT: |
| 201 |
res = (a < b) ? Py_True : Py_False; |
| 202 |
break; |
| 203 |
case Py_LE: |
| 204 |
res = (a <= b) ? Py_True : Py_False; |
| 205 |
break; |
| 206 |
case Py_GT: |
| 207 |
res = (a > b) ? Py_True : Py_False; |
| 208 |
break; |
| 209 |
case Py_GE: |
| 210 |
res = (a >= b) ? Py_True : Py_False; |
| 211 |
break; |
| 212 |
default: |
| 213 |
res = Py_NotImplemented; |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
Py_INCREF (res); |
| 218 |
return res; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
static PyObject * |
| 223 |
_conv_pyobject_to_pylong (PyObject *pyobj) { |
| 224 |
if (!PyLong_Check (pyobj)) { |
| 225 |
PyErr_SetString (PyExc_TypeError, "not of type int"); |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
Py_INCREF (pyobj); |
| 229 |
return pyobj; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
int |
| 234 |
_conv_pyobject_to_ulong (PyObject *pyobj, unsigned long *result) { |
| 235 |
unsigned long temp; |
| 236 |
PyObject *pylong; |
| 237 |
|
| 238 |
pylong = _conv_pyobject_to_pylong (pyobj); |
| 239 |
if (pylong == NULL) |
| 240 |
return -1; |
| 241 |
|
| 242 |
temp = PyLong_AsUnsignedLong (pylong); |
| 243 |
if (PyErr_Occurred ()) |
| 244 |
return -1; |
| 245 |
|
| 246 |
*result = temp; |
| 247 |
return 0; |
| 248 |
} |