Debian Codegraph @

cairo/misc.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
34 #include "private.h"
35
36 /* Returns 1 if the object has the correct file type for a filesystem path.
37 * Parsing it with Pycairo_fspath_converter() might still fail.
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 /* Broken in PyPy: https://foss.heptapod.net/pypy/pypy/-/issues/3168 */
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 /* Broken in PyPy: https://foss.heptapod.net/pypy/pypy/-/issues/3168 */
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 /* Converts a Python object to a cairo path. The result needs to be freed with
89 * PyMem_Free().
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 /* Verifies that the object has a callable write() method.
139 * Gives a borrowed reference.
140 */
141 int
142 Pycairo_writer_converter (PyObject *obj, PyObject** file) {
143 PyObject *res;
144
145 /* Check that the file is opened in binary mode by writing a zero length
146 * bytes object to it */
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 /* Check that the file is opened in binary mode by reading a zero length
161 * bytes object from it */
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 /* NULL on error */
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 /* -1 on error */
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 }