blob: 0d093711f5f0965cf1b9329c737134fb18fa5b5c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module support implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006#define FLAG_SIZE_T 1
Guido van Rossum1d5735e1994-08-30 08:27:36 +00007typedef double va_double;
Guido van Rossum1d5735e1994-08-30 08:27:36 +00008
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009static PyObject *va_build_value(const char *, va_list, int);
10
Guido van Rossum2e58ff31997-11-19 18:53:33 +000011/* Package context -- the full module name for package imports */
12char *_Py_PackageContext = NULL;
13
Guido van Rossumbf80e541993-02-08 15:49:17 +000014/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +000015
Fred Drakeceead6d2003-01-30 15:08:25 +000016static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000017countformat(const char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +000018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 int count = 0;
20 int level = 0;
21 while (level > 0 || *format != endchar) {
22 switch (*format) {
23 case '\0':
24 /* Premature end */
25 PyErr_SetString(PyExc_SystemError,
26 "unmatched paren in format");
27 return -1;
28 case '(':
29 case '[':
30 case '{':
31 if (level == 0)
32 count++;
33 level++;
34 break;
35 case ')':
36 case ']':
37 case '}':
38 level--;
39 break;
40 case '#':
41 case '&':
42 case ',':
43 case ':':
44 case ' ':
45 case '\t':
46 break;
47 default:
48 if (level == 0)
49 count++;
50 }
51 format++;
52 }
53 return count;
Guido van Rossumfc61adb1992-04-13 15:53:41 +000054}
55
56
Guido van Rossumfc61adb1992-04-13 15:53:41 +000057/* Generic function to create a value -- the inverse of getargs() */
58/* After an original idea and first implementation by Steven Miale */
59
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060static PyObject *do_mktuple(const char**, va_list *, int, int, int);
61static PyObject *do_mklist(const char**, va_list *, int, int, int);
62static PyObject *do_mkdict(const char**, va_list *, int, int, int);
63static PyObject *do_mkvalue(const char**, va_list *, int);
Guido van Rossumfc61adb1992-04-13 15:53:41 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Serhiy Storchaka13e602e2016-05-20 22:31:14 +030066static void
67do_ignore(const char **p_format, va_list *p_va, int endchar, int n, int flags)
68{
69 PyObject *v;
70 int i;
71 assert(PyErr_Occurred());
72 v = PyTuple_New(n);
73 for (i = 0; i < n; i++) {
74 PyObject *exception, *value, *tb, *w;
75
76 PyErr_Fetch(&exception, &value, &tb);
77 w = do_mkvalue(p_format, p_va, flags);
78 PyErr_Restore(exception, value, tb);
79 if (w != NULL) {
80 if (v != NULL) {
81 PyTuple_SET_ITEM(v, i, w);
82 }
83 else {
84 Py_DECREF(w);
85 }
86 }
87 }
88 Py_XDECREF(v);
89 if (**p_format != endchar) {
90 PyErr_SetString(PyExc_SystemError,
91 "Unmatched paren in format");
92 return;
93 }
94 if (endchar)
95 ++*p_format;
96}
97
Guido van Rossum79f25d91997-04-29 20:08:16 +000098static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000099do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 PyObject *d;
102 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (n < 0)
104 return NULL;
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300105 if (n % 2) {
106 PyErr_SetString(PyExc_SystemError,
107 "Bad dict format");
108 do_ignore(p_format, p_va, endchar, n, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 return NULL;
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300110 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 /* Note that we can't bail immediately on error as this will leak
112 refcounts on any 'N' arguments. */
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300113 if ((d = PyDict_New()) == NULL) {
114 do_ignore(p_format, p_va, endchar, n, flags);
115 return NULL;
116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 for (i = 0; i < n; i+= 2) {
118 PyObject *k, *v;
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 k = do_mkvalue(p_format, p_va, flags);
121 if (k == NULL) {
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300122 do_ignore(p_format, p_va, endchar, n - i - 1, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 Py_DECREF(d);
124 return NULL;
125 }
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300126 v = do_mkvalue(p_format, p_va, flags);
127 if (v == NULL || PyDict_SetItem(d, k, v) < 0) {
128 do_ignore(p_format, p_va, endchar, n - i - 2, flags);
129 Py_DECREF(k);
130 Py_XDECREF(v);
131 Py_DECREF(d);
132 return NULL;
133 }
134 Py_DECREF(k);
135 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 }
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300137 if (**p_format != endchar) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 PyErr_SetString(PyExc_SystemError,
140 "Unmatched paren in format");
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300141 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 }
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300143 if (endchar)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 ++*p_format;
145 return d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146}
147
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyObject *v;
152 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (n < 0)
154 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 /* Note that we can't bail immediately on error as this will leak
156 refcounts on any 'N' arguments. */
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300157 v = PyList_New(n);
158 if (v == NULL) {
159 do_ignore(p_format, p_va, endchar, n, flags);
160 return NULL;
161 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 for (i = 0; i < n; i++) {
163 PyObject *w = do_mkvalue(p_format, p_va, flags);
164 if (w == NULL) {
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300165 do_ignore(p_format, p_va, endchar, n - i - 1, flags);
166 Py_DECREF(v);
167 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 }
169 PyList_SET_ITEM(v, i, w);
170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (**p_format != endchar) {
172 Py_DECREF(v);
173 PyErr_SetString(PyExc_SystemError,
174 "Unmatched paren in format");
175 return NULL;
176 }
177 if (endchar)
178 ++*p_format;
179 return v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000180}
181
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyObject *v;
186 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (n < 0)
188 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* Note that we can't bail immediately on error as this will leak
190 refcounts on any 'N' arguments. */
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300191 if ((v = PyTuple_New(n)) == NULL) {
192 do_ignore(p_format, p_va, endchar, n, flags);
193 return NULL;
194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 for (i = 0; i < n; i++) {
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300196 PyObject *w = do_mkvalue(p_format, p_va, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (w == NULL) {
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300198 do_ignore(p_format, p_va, endchar, n - i - 1, flags);
199 Py_DECREF(v);
200 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 }
202 PyTuple_SET_ITEM(v, i, w);
203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (**p_format != endchar) {
205 Py_DECREF(v);
206 PyErr_SetString(PyExc_SystemError,
207 "Unmatched paren in format");
208 return NULL;
209 }
210 if (endchar)
211 ++*p_format;
212 return v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000213}
214
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216do_mkvalue(const char **p_format, va_list *p_va, int flags)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 for (;;) {
219 switch (*(*p_format)++) {
220 case '(':
221 return do_mktuple(p_format, p_va, ')',
222 countformat(*p_format, ')'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 case '[':
225 return do_mklist(p_format, p_va, ']',
226 countformat(*p_format, ']'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 case '{':
229 return do_mkdict(p_format, p_va, '}',
230 countformat(*p_format, '}'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 case 'b':
233 case 'B':
234 case 'h':
235 case 'i':
236 return PyLong_FromLong((long)va_arg(*p_va, int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 case 'H':
239 return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
240
241 case 'I':
242 {
243 unsigned int n;
244 n = va_arg(*p_va, unsigned int);
245 return PyLong_FromUnsignedLong(n);
246 }
247
248 case 'n':
Martin v. Löwis18e16552006-02-15 17:27:45 +0000249#if SIZEOF_SIZE_T!=SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
253 case 'l':
254 return PyLong_FromLong(va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 case 'k':
257 {
258 unsigned long n;
259 n = va_arg(*p_va, unsigned long);
260 return PyLong_FromUnsignedLong(n);
261 }
Jack Jansendbd65032003-04-17 22:01:10 +0000262
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000263#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 case 'L':
265 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 case 'K':
268 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 case 'u':
271 {
272 PyObject *v;
273 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
274 Py_ssize_t n;
275 if (**p_format == '#') {
276 ++*p_format;
277 if (flags & FLAG_SIZE_T)
278 n = va_arg(*p_va, Py_ssize_t);
279 else
280 n = va_arg(*p_va, int);
281 }
282 else
283 n = -1;
284 if (u == NULL) {
285 v = Py_None;
286 Py_INCREF(v);
287 }
288 else {
289 if (n < 0)
Victor Stinnerd9c563e2011-09-29 04:01:43 +0200290 n = Py_UNICODE_strlen(u);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 v = PyUnicode_FromUnicode(u, n);
292 }
293 return v;
294 }
295 case 'f':
296 case 'd':
297 return PyFloat_FromDouble(
298 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 case 'D':
301 return PyComplex_FromCComplex(
302 *((Py_complex *)va_arg(*p_va, Py_complex *)));
Fred Drakeaec79242001-03-12 21:03:26 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 case 'c':
305 {
306 char p[1];
307 p[0] = (char)va_arg(*p_va, int);
308 return PyBytes_FromStringAndSize(p, 1);
309 }
310 case 'C':
311 {
312 int i = va_arg(*p_va, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 return PyUnicode_FromOrdinal(i);
314 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 case 's':
317 case 'z':
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000318 case 'U': /* XXX deprecated alias */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 {
320 PyObject *v;
321 char *str = va_arg(*p_va, char *);
322 Py_ssize_t n;
323 if (**p_format == '#') {
324 ++*p_format;
325 if (flags & FLAG_SIZE_T)
326 n = va_arg(*p_va, Py_ssize_t);
327 else
328 n = va_arg(*p_va, int);
329 }
330 else
331 n = -1;
332 if (str == NULL) {
333 v = Py_None;
334 Py_INCREF(v);
335 }
336 else {
337 if (n < 0) {
338 size_t m = strlen(str);
339 if (m > PY_SSIZE_T_MAX) {
340 PyErr_SetString(PyExc_OverflowError,
341 "string too long for Python string");
342 return NULL;
343 }
344 n = (Py_ssize_t)m;
345 }
346 v = PyUnicode_FromStringAndSize(str, n);
347 }
348 return v;
349 }
Walter Dörwaldd2034312007-05-18 16:29:38 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 case 'y':
352 {
353 PyObject *v;
354 char *str = va_arg(*p_va, char *);
355 Py_ssize_t n;
356 if (**p_format == '#') {
357 ++*p_format;
358 if (flags & FLAG_SIZE_T)
359 n = va_arg(*p_va, Py_ssize_t);
360 else
361 n = va_arg(*p_va, int);
362 }
363 else
364 n = -1;
365 if (str == NULL) {
366 v = Py_None;
367 Py_INCREF(v);
368 }
369 else {
370 if (n < 0) {
371 size_t m = strlen(str);
372 if (m > PY_SSIZE_T_MAX) {
373 PyErr_SetString(PyExc_OverflowError,
374 "string too long for Python bytes");
375 return NULL;
376 }
377 n = (Py_ssize_t)m;
378 }
379 v = PyBytes_FromStringAndSize(str, n);
380 }
381 return v;
382 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 case 'N':
385 case 'S':
386 case 'O':
387 if (**p_format == '&') {
388 typedef PyObject *(*converter)(void *);
389 converter func = va_arg(*p_va, converter);
390 void *arg = va_arg(*p_va, void *);
391 ++*p_format;
392 return (*func)(arg);
393 }
394 else {
395 PyObject *v;
396 v = va_arg(*p_va, PyObject *);
397 if (v != NULL) {
398 if (*(*p_format - 1) != 'N')
399 Py_INCREF(v);
400 }
401 else if (!PyErr_Occurred())
402 /* If a NULL was passed
403 * because a call that should
404 * have constructed a value
405 * failed, that's OK, and we
406 * pass the error on; but if
407 * no error occurred it's not
408 * clear that the caller knew
409 * what she was doing. */
410 PyErr_SetString(PyExc_SystemError,
411 "NULL object passed to Py_BuildValue");
412 return v;
413 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 case ':':
416 case ',':
417 case ' ':
418 case '\t':
419 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 default:
422 PyErr_SetString(PyExc_SystemError,
423 "bad format char passed to Py_BuildValue");
424 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 }
427 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000428}
429
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000430
Fred Drakeceead6d2003-01-30 15:08:25 +0000431PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000432Py_BuildValue(const char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 va_list va;
435 PyObject* retval;
436 va_start(va, format);
437 retval = va_build_value(format, va, 0);
438 va_end(va);
439 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440}
441
442PyObject *
443_Py_BuildValue_SizeT(const char *format, ...)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 va_list va;
446 PyObject* retval;
447 va_start(va, format);
448 retval = va_build_value(format, va, FLAG_SIZE_T);
449 va_end(va);
450 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000451}
Guido van Rossume5372401993-03-16 12:15:04 +0000452
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000454Py_VaBuildValue(const char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 return va_build_value(format, va, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457}
458
459PyObject *
460_Py_VaBuildValue_SizeT(const char *format, va_list va)
461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 return va_build_value(format, va, FLAG_SIZE_T);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463}
464
465static PyObject *
466va_build_value(const char *format, va_list va, int flags)
467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 const char *f = format;
469 int n = countformat(f, '\0');
470 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000471
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000472 Py_VA_COPY(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (n < 0)
475 return NULL;
476 if (n == 0) {
477 Py_INCREF(Py_None);
478 return Py_None;
479 }
480 if (n == 1)
481 return do_mkvalue(&f, &lva, flags);
482 return do_mktuple(&f, &lva, '\0', n, flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483}
484
485
Guido van Rossum79f25d91997-04-29 20:08:16 +0000486PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000487PyEval_CallFunction(PyObject *obj, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 va_list vargs;
490 PyObject *args;
491 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 args = Py_VaBuildValue(format, vargs);
496 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (args == NULL)
499 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 res = PyEval_CallObject(obj, args);
502 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000505}
506
507
Guido van Rossum79f25d91997-04-29 20:08:16 +0000508PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000509PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 va_list vargs;
512 PyObject *meth;
513 PyObject *args;
514 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 meth = PyObject_GetAttrString(obj, methodname);
517 if (meth == NULL)
518 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 args = Py_VaBuildValue(format, vargs);
523 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (args == NULL) {
526 Py_DECREF(meth);
527 return NULL;
528 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 res = PyEval_CallObject(meth, args);
531 Py_DECREF(meth);
532 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000535}
Fred Drake9e285152000-09-23 03:24:27 +0000536
537int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000538PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
Fred Drake9e285152000-09-23 03:24:27 +0000539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 PyObject *dict;
541 if (!PyModule_Check(m)) {
542 PyErr_SetString(PyExc_TypeError,
543 "PyModule_AddObject() needs module as first arg");
544 return -1;
545 }
546 if (!o) {
547 if (!PyErr_Occurred())
548 PyErr_SetString(PyExc_TypeError,
549 "PyModule_AddObject() needs non-NULL value");
550 return -1;
551 }
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 dict = PyModule_GetDict(m);
554 if (dict == NULL) {
555 /* Internal error -- modules must have a dict! */
556 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
557 PyModule_GetName(m));
558 return -1;
559 }
560 if (PyDict_SetItemString(dict, name, o))
561 return -1;
562 Py_DECREF(o);
563 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000564}
565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000567PyModule_AddIntConstant(PyObject *m, const char *name, long value)
Fred Drake9e285152000-09-23 03:24:27 +0000568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 PyObject *o = PyLong_FromLong(value);
570 if (!o)
571 return -1;
572 if (PyModule_AddObject(m, name, o) == 0)
573 return 0;
574 Py_DECREF(o);
575 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000576}
577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000579PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
Fred Drake9e285152000-09-23 03:24:27 +0000580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyObject *o = PyUnicode_FromString(value);
582 if (!o)
583 return -1;
584 if (PyModule_AddObject(m, name, o) == 0)
585 return 0;
586 Py_DECREF(o);
587 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000588}