1 | n/a | |
---|
2 | n/a | /* Module support implementation */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | |
---|
6 | n/a | #define FLAG_SIZE_T 1 |
---|
7 | n/a | typedef double va_double; |
---|
8 | n/a | |
---|
9 | n/a | static PyObject *va_build_value(const char *, va_list, int); |
---|
10 | n/a | static PyObject **va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len, const char *, va_list, int, Py_ssize_t*); |
---|
11 | n/a | |
---|
12 | n/a | /* Package context -- the full module name for package imports */ |
---|
13 | n/a | const char *_Py_PackageContext = NULL; |
---|
14 | n/a | |
---|
15 | n/a | /* Helper for mkvalue() to scan the length of a format */ |
---|
16 | n/a | |
---|
17 | n/a | static Py_ssize_t |
---|
18 | n/a | countformat(const char *format, char endchar) |
---|
19 | n/a | { |
---|
20 | n/a | Py_ssize_t count = 0; |
---|
21 | n/a | int level = 0; |
---|
22 | n/a | while (level > 0 || *format != endchar) { |
---|
23 | n/a | switch (*format) { |
---|
24 | n/a | case '\0': |
---|
25 | n/a | /* Premature end */ |
---|
26 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
27 | n/a | "unmatched paren in format"); |
---|
28 | n/a | return -1; |
---|
29 | n/a | case '(': |
---|
30 | n/a | case '[': |
---|
31 | n/a | case '{': |
---|
32 | n/a | if (level == 0) { |
---|
33 | n/a | count++; |
---|
34 | n/a | } |
---|
35 | n/a | level++; |
---|
36 | n/a | break; |
---|
37 | n/a | case ')': |
---|
38 | n/a | case ']': |
---|
39 | n/a | case '}': |
---|
40 | n/a | level--; |
---|
41 | n/a | break; |
---|
42 | n/a | case '#': |
---|
43 | n/a | case '&': |
---|
44 | n/a | case ',': |
---|
45 | n/a | case ':': |
---|
46 | n/a | case ' ': |
---|
47 | n/a | case '\t': |
---|
48 | n/a | break; |
---|
49 | n/a | default: |
---|
50 | n/a | if (level == 0) { |
---|
51 | n/a | count++; |
---|
52 | n/a | } |
---|
53 | n/a | } |
---|
54 | n/a | format++; |
---|
55 | n/a | } |
---|
56 | n/a | return count; |
---|
57 | n/a | } |
---|
58 | n/a | |
---|
59 | n/a | |
---|
60 | n/a | /* Generic function to create a value -- the inverse of getargs() */ |
---|
61 | n/a | /* After an original idea and first implementation by Steven Miale */ |
---|
62 | n/a | |
---|
63 | n/a | static PyObject *do_mktuple(const char**, va_list *, char, Py_ssize_t, int); |
---|
64 | n/a | static int do_mkstack(PyObject **, const char**, va_list *, char, Py_ssize_t, int); |
---|
65 | n/a | static PyObject *do_mklist(const char**, va_list *, char, Py_ssize_t, int); |
---|
66 | n/a | static PyObject *do_mkdict(const char**, va_list *, char, Py_ssize_t, int); |
---|
67 | n/a | static PyObject *do_mkvalue(const char**, va_list *, int); |
---|
68 | n/a | |
---|
69 | n/a | |
---|
70 | n/a | static void |
---|
71 | n/a | do_ignore(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags) |
---|
72 | n/a | { |
---|
73 | n/a | PyObject *v; |
---|
74 | n/a | Py_ssize_t i; |
---|
75 | n/a | assert(PyErr_Occurred()); |
---|
76 | n/a | v = PyTuple_New(n); |
---|
77 | n/a | for (i = 0; i < n; i++) { |
---|
78 | n/a | PyObject *exception, *value, *tb, *w; |
---|
79 | n/a | |
---|
80 | n/a | PyErr_Fetch(&exception, &value, &tb); |
---|
81 | n/a | w = do_mkvalue(p_format, p_va, flags); |
---|
82 | n/a | PyErr_Restore(exception, value, tb); |
---|
83 | n/a | if (w != NULL) { |
---|
84 | n/a | if (v != NULL) { |
---|
85 | n/a | PyTuple_SET_ITEM(v, i, w); |
---|
86 | n/a | } |
---|
87 | n/a | else { |
---|
88 | n/a | Py_DECREF(w); |
---|
89 | n/a | } |
---|
90 | n/a | } |
---|
91 | n/a | } |
---|
92 | n/a | Py_XDECREF(v); |
---|
93 | n/a | if (**p_format != endchar) { |
---|
94 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
95 | n/a | "Unmatched paren in format"); |
---|
96 | n/a | return; |
---|
97 | n/a | } |
---|
98 | n/a | if (endchar) { |
---|
99 | n/a | ++*p_format; |
---|
100 | n/a | } |
---|
101 | n/a | } |
---|
102 | n/a | |
---|
103 | n/a | static PyObject * |
---|
104 | n/a | do_mkdict(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags) |
---|
105 | n/a | { |
---|
106 | n/a | PyObject *d; |
---|
107 | n/a | Py_ssize_t i; |
---|
108 | n/a | if (n < 0) |
---|
109 | n/a | return NULL; |
---|
110 | n/a | if (n % 2) { |
---|
111 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
112 | n/a | "Bad dict format"); |
---|
113 | n/a | do_ignore(p_format, p_va, endchar, n, flags); |
---|
114 | n/a | return NULL; |
---|
115 | n/a | } |
---|
116 | n/a | /* Note that we can't bail immediately on error as this will leak |
---|
117 | n/a | refcounts on any 'N' arguments. */ |
---|
118 | n/a | if ((d = PyDict_New()) == NULL) { |
---|
119 | n/a | do_ignore(p_format, p_va, endchar, n, flags); |
---|
120 | n/a | return NULL; |
---|
121 | n/a | } |
---|
122 | n/a | for (i = 0; i < n; i+= 2) { |
---|
123 | n/a | PyObject *k, *v; |
---|
124 | n/a | |
---|
125 | n/a | k = do_mkvalue(p_format, p_va, flags); |
---|
126 | n/a | if (k == NULL) { |
---|
127 | n/a | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
---|
128 | n/a | Py_DECREF(d); |
---|
129 | n/a | return NULL; |
---|
130 | n/a | } |
---|
131 | n/a | v = do_mkvalue(p_format, p_va, flags); |
---|
132 | n/a | if (v == NULL || PyDict_SetItem(d, k, v) < 0) { |
---|
133 | n/a | do_ignore(p_format, p_va, endchar, n - i - 2, flags); |
---|
134 | n/a | Py_DECREF(k); |
---|
135 | n/a | Py_XDECREF(v); |
---|
136 | n/a | Py_DECREF(d); |
---|
137 | n/a | return NULL; |
---|
138 | n/a | } |
---|
139 | n/a | Py_DECREF(k); |
---|
140 | n/a | Py_DECREF(v); |
---|
141 | n/a | } |
---|
142 | n/a | if (**p_format != endchar) { |
---|
143 | n/a | Py_DECREF(d); |
---|
144 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
145 | n/a | "Unmatched paren in format"); |
---|
146 | n/a | return NULL; |
---|
147 | n/a | } |
---|
148 | n/a | if (endchar) |
---|
149 | n/a | ++*p_format; |
---|
150 | n/a | return d; |
---|
151 | n/a | } |
---|
152 | n/a | |
---|
153 | n/a | static PyObject * |
---|
154 | n/a | do_mklist(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags) |
---|
155 | n/a | { |
---|
156 | n/a | PyObject *v; |
---|
157 | n/a | Py_ssize_t i; |
---|
158 | n/a | if (n < 0) |
---|
159 | n/a | return NULL; |
---|
160 | n/a | /* Note that we can't bail immediately on error as this will leak |
---|
161 | n/a | refcounts on any 'N' arguments. */ |
---|
162 | n/a | v = PyList_New(n); |
---|
163 | n/a | if (v == NULL) { |
---|
164 | n/a | do_ignore(p_format, p_va, endchar, n, flags); |
---|
165 | n/a | return NULL; |
---|
166 | n/a | } |
---|
167 | n/a | for (i = 0; i < n; i++) { |
---|
168 | n/a | PyObject *w = do_mkvalue(p_format, p_va, flags); |
---|
169 | n/a | if (w == NULL) { |
---|
170 | n/a | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
---|
171 | n/a | Py_DECREF(v); |
---|
172 | n/a | return NULL; |
---|
173 | n/a | } |
---|
174 | n/a | PyList_SET_ITEM(v, i, w); |
---|
175 | n/a | } |
---|
176 | n/a | if (**p_format != endchar) { |
---|
177 | n/a | Py_DECREF(v); |
---|
178 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
179 | n/a | "Unmatched paren in format"); |
---|
180 | n/a | return NULL; |
---|
181 | n/a | } |
---|
182 | n/a | if (endchar) |
---|
183 | n/a | ++*p_format; |
---|
184 | n/a | return v; |
---|
185 | n/a | } |
---|
186 | n/a | |
---|
187 | n/a | static int |
---|
188 | n/a | do_mkstack(PyObject **stack, const char **p_format, va_list *p_va, |
---|
189 | n/a | char endchar, Py_ssize_t n, int flags) |
---|
190 | n/a | { |
---|
191 | n/a | Py_ssize_t i; |
---|
192 | n/a | |
---|
193 | n/a | if (n < 0) { |
---|
194 | n/a | return -1; |
---|
195 | n/a | } |
---|
196 | n/a | /* Note that we can't bail immediately on error as this will leak |
---|
197 | n/a | refcounts on any 'N' arguments. */ |
---|
198 | n/a | for (i = 0; i < n; i++) { |
---|
199 | n/a | PyObject *w = do_mkvalue(p_format, p_va, flags); |
---|
200 | n/a | if (w == NULL) { |
---|
201 | n/a | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
---|
202 | n/a | goto error; |
---|
203 | n/a | } |
---|
204 | n/a | stack[i] = w; |
---|
205 | n/a | } |
---|
206 | n/a | if (**p_format != endchar) { |
---|
207 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
208 | n/a | "Unmatched paren in format"); |
---|
209 | n/a | goto error; |
---|
210 | n/a | } |
---|
211 | n/a | if (endchar) { |
---|
212 | n/a | ++*p_format; |
---|
213 | n/a | } |
---|
214 | n/a | return 0; |
---|
215 | n/a | |
---|
216 | n/a | error: |
---|
217 | n/a | n = i; |
---|
218 | n/a | for (i=0; i < n; i++) { |
---|
219 | n/a | Py_DECREF(stack[i]); |
---|
220 | n/a | } |
---|
221 | n/a | return -1; |
---|
222 | n/a | } |
---|
223 | n/a | |
---|
224 | n/a | static PyObject * |
---|
225 | n/a | do_mktuple(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags) |
---|
226 | n/a | { |
---|
227 | n/a | PyObject *v; |
---|
228 | n/a | Py_ssize_t i; |
---|
229 | n/a | if (n < 0) |
---|
230 | n/a | return NULL; |
---|
231 | n/a | /* Note that we can't bail immediately on error as this will leak |
---|
232 | n/a | refcounts on any 'N' arguments. */ |
---|
233 | n/a | if ((v = PyTuple_New(n)) == NULL) { |
---|
234 | n/a | do_ignore(p_format, p_va, endchar, n, flags); |
---|
235 | n/a | return NULL; |
---|
236 | n/a | } |
---|
237 | n/a | for (i = 0; i < n; i++) { |
---|
238 | n/a | PyObject *w = do_mkvalue(p_format, p_va, flags); |
---|
239 | n/a | if (w == NULL) { |
---|
240 | n/a | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
---|
241 | n/a | Py_DECREF(v); |
---|
242 | n/a | return NULL; |
---|
243 | n/a | } |
---|
244 | n/a | PyTuple_SET_ITEM(v, i, w); |
---|
245 | n/a | } |
---|
246 | n/a | if (**p_format != endchar) { |
---|
247 | n/a | Py_DECREF(v); |
---|
248 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
249 | n/a | "Unmatched paren in format"); |
---|
250 | n/a | return NULL; |
---|
251 | n/a | } |
---|
252 | n/a | if (endchar) |
---|
253 | n/a | ++*p_format; |
---|
254 | n/a | return v; |
---|
255 | n/a | } |
---|
256 | n/a | |
---|
257 | n/a | static PyObject * |
---|
258 | n/a | do_mkvalue(const char **p_format, va_list *p_va, int flags) |
---|
259 | n/a | { |
---|
260 | n/a | for (;;) { |
---|
261 | n/a | switch (*(*p_format)++) { |
---|
262 | n/a | case '(': |
---|
263 | n/a | return do_mktuple(p_format, p_va, ')', |
---|
264 | n/a | countformat(*p_format, ')'), flags); |
---|
265 | n/a | |
---|
266 | n/a | case '[': |
---|
267 | n/a | return do_mklist(p_format, p_va, ']', |
---|
268 | n/a | countformat(*p_format, ']'), flags); |
---|
269 | n/a | |
---|
270 | n/a | case '{': |
---|
271 | n/a | return do_mkdict(p_format, p_va, '}', |
---|
272 | n/a | countformat(*p_format, '}'), flags); |
---|
273 | n/a | |
---|
274 | n/a | case 'b': |
---|
275 | n/a | case 'B': |
---|
276 | n/a | case 'h': |
---|
277 | n/a | case 'i': |
---|
278 | n/a | return PyLong_FromLong((long)va_arg(*p_va, int)); |
---|
279 | n/a | |
---|
280 | n/a | case 'H': |
---|
281 | n/a | return PyLong_FromLong((long)va_arg(*p_va, unsigned int)); |
---|
282 | n/a | |
---|
283 | n/a | case 'I': |
---|
284 | n/a | { |
---|
285 | n/a | unsigned int n; |
---|
286 | n/a | n = va_arg(*p_va, unsigned int); |
---|
287 | n/a | return PyLong_FromUnsignedLong(n); |
---|
288 | n/a | } |
---|
289 | n/a | |
---|
290 | n/a | case 'n': |
---|
291 | n/a | #if SIZEOF_SIZE_T!=SIZEOF_LONG |
---|
292 | n/a | return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t)); |
---|
293 | n/a | #endif |
---|
294 | n/a | /* Fall through from 'n' to 'l' if Py_ssize_t is long */ |
---|
295 | n/a | case 'l': |
---|
296 | n/a | return PyLong_FromLong(va_arg(*p_va, long)); |
---|
297 | n/a | |
---|
298 | n/a | case 'k': |
---|
299 | n/a | { |
---|
300 | n/a | unsigned long n; |
---|
301 | n/a | n = va_arg(*p_va, unsigned long); |
---|
302 | n/a | return PyLong_FromUnsignedLong(n); |
---|
303 | n/a | } |
---|
304 | n/a | |
---|
305 | n/a | case 'L': |
---|
306 | n/a | return PyLong_FromLongLong((long long)va_arg(*p_va, long long)); |
---|
307 | n/a | |
---|
308 | n/a | case 'K': |
---|
309 | n/a | return PyLong_FromUnsignedLongLong((long long)va_arg(*p_va, unsigned long long)); |
---|
310 | n/a | |
---|
311 | n/a | case 'u': |
---|
312 | n/a | { |
---|
313 | n/a | PyObject *v; |
---|
314 | n/a | Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *); |
---|
315 | n/a | Py_ssize_t n; |
---|
316 | n/a | if (**p_format == '#') { |
---|
317 | n/a | ++*p_format; |
---|
318 | n/a | if (flags & FLAG_SIZE_T) |
---|
319 | n/a | n = va_arg(*p_va, Py_ssize_t); |
---|
320 | n/a | else |
---|
321 | n/a | n = va_arg(*p_va, int); |
---|
322 | n/a | } |
---|
323 | n/a | else |
---|
324 | n/a | n = -1; |
---|
325 | n/a | if (u == NULL) { |
---|
326 | n/a | v = Py_None; |
---|
327 | n/a | Py_INCREF(v); |
---|
328 | n/a | } |
---|
329 | n/a | else { |
---|
330 | n/a | if (n < 0) |
---|
331 | n/a | n = wcslen(u); |
---|
332 | n/a | v = PyUnicode_FromWideChar(u, n); |
---|
333 | n/a | } |
---|
334 | n/a | return v; |
---|
335 | n/a | } |
---|
336 | n/a | case 'f': |
---|
337 | n/a | case 'd': |
---|
338 | n/a | return PyFloat_FromDouble( |
---|
339 | n/a | (double)va_arg(*p_va, va_double)); |
---|
340 | n/a | |
---|
341 | n/a | case 'D': |
---|
342 | n/a | return PyComplex_FromCComplex( |
---|
343 | n/a | *((Py_complex *)va_arg(*p_va, Py_complex *))); |
---|
344 | n/a | |
---|
345 | n/a | case 'c': |
---|
346 | n/a | { |
---|
347 | n/a | char p[1]; |
---|
348 | n/a | p[0] = (char)va_arg(*p_va, int); |
---|
349 | n/a | return PyBytes_FromStringAndSize(p, 1); |
---|
350 | n/a | } |
---|
351 | n/a | case 'C': |
---|
352 | n/a | { |
---|
353 | n/a | int i = va_arg(*p_va, int); |
---|
354 | n/a | return PyUnicode_FromOrdinal(i); |
---|
355 | n/a | } |
---|
356 | n/a | |
---|
357 | n/a | case 's': |
---|
358 | n/a | case 'z': |
---|
359 | n/a | case 'U': /* XXX deprecated alias */ |
---|
360 | n/a | { |
---|
361 | n/a | PyObject *v; |
---|
362 | n/a | const char *str = va_arg(*p_va, const char *); |
---|
363 | n/a | Py_ssize_t n; |
---|
364 | n/a | if (**p_format == '#') { |
---|
365 | n/a | ++*p_format; |
---|
366 | n/a | if (flags & FLAG_SIZE_T) |
---|
367 | n/a | n = va_arg(*p_va, Py_ssize_t); |
---|
368 | n/a | else |
---|
369 | n/a | n = va_arg(*p_va, int); |
---|
370 | n/a | } |
---|
371 | n/a | else |
---|
372 | n/a | n = -1; |
---|
373 | n/a | if (str == NULL) { |
---|
374 | n/a | v = Py_None; |
---|
375 | n/a | Py_INCREF(v); |
---|
376 | n/a | } |
---|
377 | n/a | else { |
---|
378 | n/a | if (n < 0) { |
---|
379 | n/a | size_t m = strlen(str); |
---|
380 | n/a | if (m > PY_SSIZE_T_MAX) { |
---|
381 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
382 | n/a | "string too long for Python string"); |
---|
383 | n/a | return NULL; |
---|
384 | n/a | } |
---|
385 | n/a | n = (Py_ssize_t)m; |
---|
386 | n/a | } |
---|
387 | n/a | v = PyUnicode_FromStringAndSize(str, n); |
---|
388 | n/a | } |
---|
389 | n/a | return v; |
---|
390 | n/a | } |
---|
391 | n/a | |
---|
392 | n/a | case 'y': |
---|
393 | n/a | { |
---|
394 | n/a | PyObject *v; |
---|
395 | n/a | const char *str = va_arg(*p_va, const char *); |
---|
396 | n/a | Py_ssize_t n; |
---|
397 | n/a | if (**p_format == '#') { |
---|
398 | n/a | ++*p_format; |
---|
399 | n/a | if (flags & FLAG_SIZE_T) |
---|
400 | n/a | n = va_arg(*p_va, Py_ssize_t); |
---|
401 | n/a | else |
---|
402 | n/a | n = va_arg(*p_va, int); |
---|
403 | n/a | } |
---|
404 | n/a | else |
---|
405 | n/a | n = -1; |
---|
406 | n/a | if (str == NULL) { |
---|
407 | n/a | v = Py_None; |
---|
408 | n/a | Py_INCREF(v); |
---|
409 | n/a | } |
---|
410 | n/a | else { |
---|
411 | n/a | if (n < 0) { |
---|
412 | n/a | size_t m = strlen(str); |
---|
413 | n/a | if (m > PY_SSIZE_T_MAX) { |
---|
414 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
415 | n/a | "string too long for Python bytes"); |
---|
416 | n/a | return NULL; |
---|
417 | n/a | } |
---|
418 | n/a | n = (Py_ssize_t)m; |
---|
419 | n/a | } |
---|
420 | n/a | v = PyBytes_FromStringAndSize(str, n); |
---|
421 | n/a | } |
---|
422 | n/a | return v; |
---|
423 | n/a | } |
---|
424 | n/a | |
---|
425 | n/a | case 'N': |
---|
426 | n/a | case 'S': |
---|
427 | n/a | case 'O': |
---|
428 | n/a | if (**p_format == '&') { |
---|
429 | n/a | typedef PyObject *(*converter)(void *); |
---|
430 | n/a | converter func = va_arg(*p_va, converter); |
---|
431 | n/a | void *arg = va_arg(*p_va, void *); |
---|
432 | n/a | ++*p_format; |
---|
433 | n/a | return (*func)(arg); |
---|
434 | n/a | } |
---|
435 | n/a | else { |
---|
436 | n/a | PyObject *v; |
---|
437 | n/a | v = va_arg(*p_va, PyObject *); |
---|
438 | n/a | if (v != NULL) { |
---|
439 | n/a | if (*(*p_format - 1) != 'N') |
---|
440 | n/a | Py_INCREF(v); |
---|
441 | n/a | } |
---|
442 | n/a | else if (!PyErr_Occurred()) |
---|
443 | n/a | /* If a NULL was passed |
---|
444 | n/a | * because a call that should |
---|
445 | n/a | * have constructed a value |
---|
446 | n/a | * failed, that's OK, and we |
---|
447 | n/a | * pass the error on; but if |
---|
448 | n/a | * no error occurred it's not |
---|
449 | n/a | * clear that the caller knew |
---|
450 | n/a | * what she was doing. */ |
---|
451 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
452 | n/a | "NULL object passed to Py_BuildValue"); |
---|
453 | n/a | return v; |
---|
454 | n/a | } |
---|
455 | n/a | |
---|
456 | n/a | case ':': |
---|
457 | n/a | case ',': |
---|
458 | n/a | case ' ': |
---|
459 | n/a | case '\t': |
---|
460 | n/a | break; |
---|
461 | n/a | |
---|
462 | n/a | default: |
---|
463 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
464 | n/a | "bad format char passed to Py_BuildValue"); |
---|
465 | n/a | return NULL; |
---|
466 | n/a | |
---|
467 | n/a | } |
---|
468 | n/a | } |
---|
469 | n/a | } |
---|
470 | n/a | |
---|
471 | n/a | |
---|
472 | n/a | PyObject * |
---|
473 | n/a | Py_BuildValue(const char *format, ...) |
---|
474 | n/a | { |
---|
475 | n/a | va_list va; |
---|
476 | n/a | PyObject* retval; |
---|
477 | n/a | va_start(va, format); |
---|
478 | n/a | retval = va_build_value(format, va, 0); |
---|
479 | n/a | va_end(va); |
---|
480 | n/a | return retval; |
---|
481 | n/a | } |
---|
482 | n/a | |
---|
483 | n/a | PyObject * |
---|
484 | n/a | _Py_BuildValue_SizeT(const char *format, ...) |
---|
485 | n/a | { |
---|
486 | n/a | va_list va; |
---|
487 | n/a | PyObject* retval; |
---|
488 | n/a | va_start(va, format); |
---|
489 | n/a | retval = va_build_value(format, va, FLAG_SIZE_T); |
---|
490 | n/a | va_end(va); |
---|
491 | n/a | return retval; |
---|
492 | n/a | } |
---|
493 | n/a | |
---|
494 | n/a | PyObject * |
---|
495 | n/a | Py_VaBuildValue(const char *format, va_list va) |
---|
496 | n/a | { |
---|
497 | n/a | return va_build_value(format, va, 0); |
---|
498 | n/a | } |
---|
499 | n/a | |
---|
500 | n/a | PyObject * |
---|
501 | n/a | _Py_VaBuildValue_SizeT(const char *format, va_list va) |
---|
502 | n/a | { |
---|
503 | n/a | return va_build_value(format, va, FLAG_SIZE_T); |
---|
504 | n/a | } |
---|
505 | n/a | |
---|
506 | n/a | static PyObject * |
---|
507 | n/a | va_build_value(const char *format, va_list va, int flags) |
---|
508 | n/a | { |
---|
509 | n/a | const char *f = format; |
---|
510 | n/a | Py_ssize_t n = countformat(f, '\0'); |
---|
511 | n/a | va_list lva; |
---|
512 | n/a | PyObject *retval; |
---|
513 | n/a | |
---|
514 | n/a | if (n < 0) |
---|
515 | n/a | return NULL; |
---|
516 | n/a | if (n == 0) { |
---|
517 | n/a | Py_RETURN_NONE; |
---|
518 | n/a | } |
---|
519 | n/a | va_copy(lva, va); |
---|
520 | n/a | if (n == 1) { |
---|
521 | n/a | retval = do_mkvalue(&f, &lva, flags); |
---|
522 | n/a | } else { |
---|
523 | n/a | retval = do_mktuple(&f, &lva, '\0', n, flags); |
---|
524 | n/a | } |
---|
525 | n/a | va_end(lva); |
---|
526 | n/a | return retval; |
---|
527 | n/a | } |
---|
528 | n/a | |
---|
529 | n/a | PyObject ** |
---|
530 | n/a | _Py_VaBuildStack(PyObject **small_stack, Py_ssize_t small_stack_len, |
---|
531 | n/a | const char *format, va_list va, Py_ssize_t *p_nargs) |
---|
532 | n/a | { |
---|
533 | n/a | return va_build_stack(small_stack, small_stack_len, format, va, 0, p_nargs); |
---|
534 | n/a | } |
---|
535 | n/a | |
---|
536 | n/a | PyObject ** |
---|
537 | n/a | _Py_VaBuildStack_SizeT(PyObject **small_stack, Py_ssize_t small_stack_len, |
---|
538 | n/a | const char *format, va_list va, Py_ssize_t *p_nargs) |
---|
539 | n/a | { |
---|
540 | n/a | return va_build_stack(small_stack, small_stack_len, format, va, FLAG_SIZE_T, p_nargs); |
---|
541 | n/a | } |
---|
542 | n/a | |
---|
543 | n/a | static PyObject ** |
---|
544 | n/a | va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len, |
---|
545 | n/a | const char *format, va_list va, int flags, Py_ssize_t *p_nargs) |
---|
546 | n/a | { |
---|
547 | n/a | const char *f; |
---|
548 | n/a | Py_ssize_t n; |
---|
549 | n/a | va_list lva; |
---|
550 | n/a | PyObject **stack; |
---|
551 | n/a | int res; |
---|
552 | n/a | |
---|
553 | n/a | n = countformat(format, '\0'); |
---|
554 | n/a | if (n < 0) { |
---|
555 | n/a | *p_nargs = 0; |
---|
556 | n/a | return NULL; |
---|
557 | n/a | } |
---|
558 | n/a | |
---|
559 | n/a | if (n == 0) { |
---|
560 | n/a | *p_nargs = 0; |
---|
561 | n/a | return small_stack; |
---|
562 | n/a | } |
---|
563 | n/a | |
---|
564 | n/a | if (n <= small_stack_len) { |
---|
565 | n/a | stack = small_stack; |
---|
566 | n/a | } |
---|
567 | n/a | else { |
---|
568 | n/a | stack = PyMem_Malloc(n * sizeof(stack[0])); |
---|
569 | n/a | if (stack == NULL) { |
---|
570 | n/a | PyErr_NoMemory(); |
---|
571 | n/a | return NULL; |
---|
572 | n/a | } |
---|
573 | n/a | } |
---|
574 | n/a | |
---|
575 | n/a | va_copy(lva, va); |
---|
576 | n/a | f = format; |
---|
577 | n/a | res = do_mkstack(stack, &f, &lva, '\0', n, flags); |
---|
578 | n/a | va_end(lva); |
---|
579 | n/a | |
---|
580 | n/a | if (res < 0) { |
---|
581 | n/a | return NULL; |
---|
582 | n/a | } |
---|
583 | n/a | |
---|
584 | n/a | *p_nargs = n; |
---|
585 | n/a | return stack; |
---|
586 | n/a | } |
---|
587 | n/a | |
---|
588 | n/a | |
---|
589 | n/a | PyObject * |
---|
590 | n/a | PyEval_CallFunction(PyObject *callable, const char *format, ...) |
---|
591 | n/a | { |
---|
592 | n/a | va_list vargs; |
---|
593 | n/a | PyObject *args; |
---|
594 | n/a | PyObject *res; |
---|
595 | n/a | |
---|
596 | n/a | va_start(vargs, format); |
---|
597 | n/a | |
---|
598 | n/a | args = Py_VaBuildValue(format, vargs); |
---|
599 | n/a | va_end(vargs); |
---|
600 | n/a | |
---|
601 | n/a | if (args == NULL) |
---|
602 | n/a | return NULL; |
---|
603 | n/a | |
---|
604 | n/a | res = PyEval_CallObject(callable, args); |
---|
605 | n/a | Py_DECREF(args); |
---|
606 | n/a | |
---|
607 | n/a | return res; |
---|
608 | n/a | } |
---|
609 | n/a | |
---|
610 | n/a | |
---|
611 | n/a | PyObject * |
---|
612 | n/a | PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...) |
---|
613 | n/a | { |
---|
614 | n/a | va_list vargs; |
---|
615 | n/a | PyObject *meth; |
---|
616 | n/a | PyObject *args; |
---|
617 | n/a | PyObject *res; |
---|
618 | n/a | |
---|
619 | n/a | meth = PyObject_GetAttrString(obj, name); |
---|
620 | n/a | if (meth == NULL) |
---|
621 | n/a | return NULL; |
---|
622 | n/a | |
---|
623 | n/a | va_start(vargs, format); |
---|
624 | n/a | |
---|
625 | n/a | args = Py_VaBuildValue(format, vargs); |
---|
626 | n/a | va_end(vargs); |
---|
627 | n/a | |
---|
628 | n/a | if (args == NULL) { |
---|
629 | n/a | Py_DECREF(meth); |
---|
630 | n/a | return NULL; |
---|
631 | n/a | } |
---|
632 | n/a | |
---|
633 | n/a | res = PyEval_CallObject(meth, args); |
---|
634 | n/a | Py_DECREF(meth); |
---|
635 | n/a | Py_DECREF(args); |
---|
636 | n/a | |
---|
637 | n/a | return res; |
---|
638 | n/a | } |
---|
639 | n/a | |
---|
640 | n/a | int |
---|
641 | n/a | PyModule_AddObject(PyObject *m, const char *name, PyObject *o) |
---|
642 | n/a | { |
---|
643 | n/a | PyObject *dict; |
---|
644 | n/a | if (!PyModule_Check(m)) { |
---|
645 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
646 | n/a | "PyModule_AddObject() needs module as first arg"); |
---|
647 | n/a | return -1; |
---|
648 | n/a | } |
---|
649 | n/a | if (!o) { |
---|
650 | n/a | if (!PyErr_Occurred()) |
---|
651 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
652 | n/a | "PyModule_AddObject() needs non-NULL value"); |
---|
653 | n/a | return -1; |
---|
654 | n/a | } |
---|
655 | n/a | |
---|
656 | n/a | dict = PyModule_GetDict(m); |
---|
657 | n/a | if (dict == NULL) { |
---|
658 | n/a | /* Internal error -- modules must have a dict! */ |
---|
659 | n/a | PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__", |
---|
660 | n/a | PyModule_GetName(m)); |
---|
661 | n/a | return -1; |
---|
662 | n/a | } |
---|
663 | n/a | if (PyDict_SetItemString(dict, name, o)) |
---|
664 | n/a | return -1; |
---|
665 | n/a | Py_DECREF(o); |
---|
666 | n/a | return 0; |
---|
667 | n/a | } |
---|
668 | n/a | |
---|
669 | n/a | int |
---|
670 | n/a | PyModule_AddIntConstant(PyObject *m, const char *name, long value) |
---|
671 | n/a | { |
---|
672 | n/a | PyObject *o = PyLong_FromLong(value); |
---|
673 | n/a | if (!o) |
---|
674 | n/a | return -1; |
---|
675 | n/a | if (PyModule_AddObject(m, name, o) == 0) |
---|
676 | n/a | return 0; |
---|
677 | n/a | Py_DECREF(o); |
---|
678 | n/a | return -1; |
---|
679 | n/a | } |
---|
680 | n/a | |
---|
681 | n/a | int |
---|
682 | n/a | PyModule_AddStringConstant(PyObject *m, const char *name, const char *value) |
---|
683 | n/a | { |
---|
684 | n/a | PyObject *o = PyUnicode_FromString(value); |
---|
685 | n/a | if (!o) |
---|
686 | n/a | return -1; |
---|
687 | n/a | if (PyModule_AddObject(m, name, o) == 0) |
---|
688 | n/a | return 0; |
---|
689 | n/a | Py_DECREF(o); |
---|
690 | n/a | return -1; |
---|
691 | n/a | } |
---|