Debian Codegraph @

libdrgn/python/util.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <inttypes.h>
5 #include <stdarg.h>
6
7 #include "drgnpy.h"
8 #include "../vector.h"
9
10 int append_string(PyObject *parts, const char *s)
11 {
12 _cleanup_pydecref_ PyObject *str = PyUnicode_FromString(s);
13 if (!str)
14 return -1;
15 return PyList_Append(parts, str);
16 }
17
18 int append_u64_hex(PyObject *parts, uint64_t value)
19 {
20 char buf[19];
21 snprintf(buf, sizeof(buf), "0x%" PRIx64, value);
22 return append_string(parts, buf);
23 }
24
25 static int append_formatv(PyObject *parts, const char *format, va_list ap)
26 {
27 _cleanup_pydecref_ PyObject *str = PyUnicode_FromFormatV(format, ap);
28 if (!str)
29 return -1;
30 return PyList_Append(parts, str);
31 }
32
33 int append_format(PyObject *parts, const char *format, ...)
34 {
35 va_list ap;
36 int ret;
37
38 va_start(ap, format);
39 ret = append_formatv(parts, format, ap);
40 va_end(ap);
41 return ret;
42 }
43
44 int append_attr_repr(PyObject *parts, PyObject *obj, const char *attr_name)
45 {
46 _cleanup_pydecref_ PyObject *attr =
47 PyObject_GetAttrString(obj, attr_name);
48 if (!attr)
49 return -1;
50 _cleanup_pydecref_ PyObject *str = PyObject_Repr(attr);
51 if (!str)
52 return -1;
53 return PyList_Append(parts, str);
54 }
55
56 int append_attr_str(PyObject *parts, PyObject *obj, const char *attr_name)
57 {
58 _cleanup_pydecref_ PyObject *attr =
59 PyObject_GetAttrString(obj, attr_name);
60 if (!attr)
61 return -1;
62 _cleanup_pydecref_ PyObject *str = PyObject_Str(attr);
63 if (!str)
64 return -1;
65 return PyList_Append(parts, str);
66 }
67
68 PyObject *join_strings(PyObject *parts)
69 {
70 _cleanup_pydecref_ PyObject *sep = PyUnicode_New(0, 0);
71 if (!sep)
72 return NULL;
73 return PyUnicode_Join(sep, parts);
74 }
75
76 PyObject *repr_pretty_from_str(PyObject *self, PyObject *args, PyObject *kwds)
77 {
78 static char *keywords[] = {"p", "cycle", NULL};
79 PyObject *p;
80 int cycle;
81 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Op:_repr_pretty_",
82 keywords, &p, &cycle))
83 return NULL;
84
85 if (cycle)
86 return PyObject_CallMethod(p, "text", "s", "...");
87
88 _cleanup_pydecref_ PyObject *str_obj = PyObject_Str(self);
89 if (!str_obj)
90 return NULL;
91 return PyObject_CallMethod(p, "text", "O", str_obj);
92 }
93
94 int index_converter(PyObject *o, void *p)
95 {
96 struct index_arg *arg = p;
97
98 arg->is_none = o == Py_None;
99 if (arg->allow_none && arg->is_none)
100 return 1;
101
102 _cleanup_pydecref_ PyObject *index_obj = PyNumber_Index(o);
103 if (!index_obj)
104 return 0;
105 if (arg->is_signed) {
106 arg->svalue = PyLong_AsLongLong(index_obj);
107 return (arg->svalue != -1LL || !PyErr_Occurred());
108 } else {
109 arg->uvalue = PyLong_AsUnsignedLongLong(index_obj);
110 return (arg->uvalue != -1ULL || !PyErr_Occurred());
111 }
112 }
113
114 int u64_converter(PyObject *o, void *p)
115 {
116 return PyLong_AsUInt64(o, p) == 0;
117 }
118
119 int path_converter(PyObject *o, void *p)
120 {
121 if (o == NULL) {
122 path_cleanup(p);
123 return 1;
124 }
125
126 struct path_arg *path = p;
127 path->fd = -1;
128 path->path = NULL;
129 path->length = 0;
130 path->bytes = NULL;
131 if (path->allow_fd && PyIndex_Check(o)) {
132 _cleanup_pydecref_ PyObject *fd_obj = PyNumber_Index(o);
133 if (!fd_obj)
134 return 0;
135 int overflow;
136 long fd = PyLong_AsLongAndOverflow(fd_obj, &overflow);
137 if (fd == -1 && PyErr_Occurred())
138 return 0;
139 if (overflow > 0 || fd > INT_MAX) {
140 PyErr_SetString(PyExc_OverflowError,
141 "fd is greater than maximum");
142 return 0;
143 }
144 if (fd < 0) {
145 PyErr_SetString(PyExc_ValueError, "fd is negative");
146 return 0;
147 }
148 path->fd = fd;
149 } else if (path->allow_none && o == Py_None) {
150 path->path = NULL;
151 path->length = 0;
152 path->bytes = NULL;
153 } else {
154 if (!PyUnicode_FSConverter(o, &path->bytes)) {
155 path->object = path->bytes = NULL;
156 return 0;
157 }
158 path->path = PyBytes_AS_STRING(path->bytes);
159 path->length = PyBytes_GET_SIZE(path->bytes);
160 }
161 Py_INCREF(o);
162 path->object = o;
163 return Py_CLEANUP_SUPPORTED;
164 }
165
166 void path_cleanup(struct path_arg *path)
167 {
168 Py_CLEAR(path->bytes);
169 Py_CLEAR(path->object);
170 }
171
172 DEFINE_VECTOR_FUNCTIONS(path_arg_vector);
173
174 int path_sequence_converter(PyObject *o, void *p)
175 {
176 if (o == NULL) {
177 path_sequence_cleanup(p);
178 return 1;
179 }
180
181 struct path_sequence_arg *paths = p;
182
183 if (paths->allow_none && o == Py_None)
184 return 1;
185
186 _cleanup_pydecref_ PyObject *it = PyObject_GetIter(o);
187 if (!it)
188 return 0;
189
190 Py_ssize_t length_hint = PyObject_LengthHint(o, 1);
191 if (length_hint == -1)
192 return 0;
193 if (!path_arg_vector_reserve(&paths->args, length_hint)) {
194 PyErr_NoMemory();
195 return 0;
196 }
197
198 for (;;) {
199 _cleanup_pydecref_ PyObject *item = PyIter_Next(it);
200 if (!item)
201 break;
202
203 struct path_arg *path_arg =
204 path_arg_vector_append_entry(&paths->args);
205 if (!path_arg) {
206 PyErr_NoMemory();
207 return 0;
208 }
209 memset(path_arg, 0, sizeof(*path_arg));
210 if (!path_converter(item, path_arg)) {
211 path_arg_vector_pop(&paths->args);
212 return 0;
213 }
214 }
215 if (PyErr_Occurred())
216 return 0;
217
218 size_t n = path_arg_vector_size(&paths->args);
219 if (paths->null_terminate) {
220 if (n == SIZE_MAX) {
221 PyErr_NoMemory();
222 return 0;
223 }
224 n++;
225 }
226 paths->paths = malloc_array(n, sizeof(paths->paths[0]));
227 if (!paths->paths) {
228 PyErr_NoMemory();
229 return 0;
230 }
231
232 for (size_t i = 0; i < path_arg_vector_size(&paths->args); i++)
233 paths->paths[i] = path_arg_vector_at(&paths->args, i)->path;
234 if (paths->null_terminate)
235 paths->paths[path_arg_vector_size(&paths->args)] = NULL;
236
237 return Py_CLEANUP_SUPPORTED;
238 }
239
240 void path_sequence_cleanup(struct path_sequence_arg *paths)
241 {
242 free(paths->paths);
243 paths->paths = NULL;
244 vector_for_each(path_arg_vector, path_arg, &paths->args)
245 path_cleanup(path_arg);
246 path_arg_vector_deinit(&paths->args);
247 path_arg_vector_init(&paths->args);
248 }
249
250 size_t path_sequence_size(struct path_sequence_arg *paths)
251 {
252 return path_arg_vector_size(&paths->args);
253 }
254
255 int enum_converter(PyObject *o, void *p)
256 {
257 struct enum_arg *arg = p;
258
259 if (arg->allow_none && o == Py_None)
260 return 1;
261
262 if (!PyObject_TypeCheck(o, (PyTypeObject *)arg->type)) {
263 PyErr_Format(PyExc_TypeError,
264 "expected %s%s, not %s",
265 ((PyTypeObject *)arg->type)->tp_name,
266 arg->allow_none ? " or None" : "",
267 Py_TYPE(o)->tp_name);
268 return 0;
269 }
270
271 _cleanup_pydecref_ PyObject *value = PyObject_GetAttrString(o, "value");
272 if (!value)
273 return 0;
274 arg->value = PyLong_AsUnsignedLong(value);
275 if (arg->value == -1 && PyErr_Occurred())
276 return 0;
277 return 1;
278 }
279
280 #if PY_VERSION_HEX < 0x030e00a2
281 int PyLong_IsNegative(PyObject *obj)
282 {
283 if (!PyLong_Check(obj)) {
284 PyErr_SetString(PyExc_TypeError, "expected int");
285 return -1;
286 }
287 return _PyLong_Sign(obj) < 0;
288 }
289 #endif
290
291 #if PY_VERSION_HEX < 0x030e00a1
292 // Note that PyLong_AsLong{,Long}() automatically call __index__(), but
293 // PyLong_AsUnsignedLong{,Long}() don't.
294 int PyLong_AsInt64(PyObject *obj, int64_t *value)
295 {
296 long long v = PyLong_AsLongLong(obj);
297 if (v == -1 && PyErr_Occurred())
298 return -1;
299 if (v < INT64_MIN || v > INT64_MAX) {
300 PyErr_SetString(PyExc_OverflowError,
301 "Python int too large to convert to C int64_t");
302 return -1;
303 }
304 *value = v;
305 return 0;
306 }
307
308 int PyLong_AsUInt32(PyObject *obj, uint32_t *value)
309 {
310 _cleanup_pydecref_ PyObject *index = PyNumber_Index(obj);
311 if (!index)
312 return -1;
313 unsigned long v = PyLong_AsUnsignedLong(index);
314 if (v == (unsigned long)-1 && PyErr_Occurred())
315 return -1;
316 if (v > UINT32_MAX) {
317 PyErr_SetString(PyExc_OverflowError,
318 "Python int too large to convert to C uint32_t");
319 return -1;
320 }
321 *value = v;
322 return 0;
323 }
324
325 int PyLong_AsUInt64(PyObject *obj, uint64_t *value)
326 {
327 _cleanup_pydecref_ PyObject *index = PyNumber_Index(obj);
328 if (!index)
329 return -1;
330 unsigned long long v = PyLong_AsUnsignedLongLong(index);
331 if (v == (unsigned long long)-1 && PyErr_Occurred())
332 return -1;
333 if (v > UINT64_MAX) {
334 PyErr_SetString(PyExc_OverflowError,
335 "Python int too large to convert to C uint64_t");
336 return -1;
337 }
338 *value = v;
339 return 0;
340 }
341 #endif
342
343 int PyLong_AsUInt16(PyObject *obj, uint16_t *value)
344 {
345 _cleanup_pydecref_ PyObject *index = PyNumber_Index(obj);
346 if (!index)
347 return -1;
348 unsigned long v = PyLong_AsUnsignedLong(index);
349 if (v == (unsigned long)-1 && PyErr_Occurred())
350 return -1;
351 if (v > UINT16_MAX) {
352 PyErr_SetString(PyExc_OverflowError,
353 "Python int too large to convert to C uint16_t");
354 return -1;
355 }
356 *value = v;
357 return 0;
358 }