blob: f405bae9d2fc4dd36c493dc4a4775ffb7d73447d [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 Pitrou7f14f0d2010-05-09 16:14:21 +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
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000067do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000069 PyObject *d;
70 int i;
71 int itemfailed = 0;
72 if (n < 0)
73 return NULL;
74 if ((d = PyDict_New()) == NULL)
75 return NULL;
76 /* Note that we can't bail immediately on error as this will leak
77 refcounts on any 'N' arguments. */
78 for (i = 0; i < n; i+= 2) {
79 PyObject *k, *v;
80 int err;
81 k = do_mkvalue(p_format, p_va, flags);
82 if (k == NULL) {
83 itemfailed = 1;
84 Py_INCREF(Py_None);
85 k = Py_None;
86 }
87 v = do_mkvalue(p_format, p_va, flags);
88 if (v == NULL) {
89 itemfailed = 1;
90 Py_INCREF(Py_None);
91 v = Py_None;
92 }
93 err = PyDict_SetItem(d, k, v);
94 Py_DECREF(k);
95 Py_DECREF(v);
96 if (err < 0 || itemfailed) {
97 Py_DECREF(d);
98 return NULL;
99 }
100 }
101 if (d != NULL && **p_format != endchar) {
102 Py_DECREF(d);
103 d = NULL;
104 PyErr_SetString(PyExc_SystemError,
105 "Unmatched paren in format");
106 }
107 else if (endchar)
108 ++*p_format;
109 return d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000110}
111
Guido van Rossum79f25d91997-04-29 20:08:16 +0000112static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000114{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000115 PyObject *v;
116 int i;
117 int itemfailed = 0;
118 if (n < 0)
119 return NULL;
120 v = PyList_New(n);
121 if (v == NULL)
122 return NULL;
123 /* Note that we can't bail immediately on error as this will leak
124 refcounts on any 'N' arguments. */
125 for (i = 0; i < n; i++) {
126 PyObject *w = do_mkvalue(p_format, p_va, flags);
127 if (w == NULL) {
128 itemfailed = 1;
129 Py_INCREF(Py_None);
130 w = Py_None;
131 }
132 PyList_SET_ITEM(v, i, w);
133 }
Neal Norwitz3e90fa52006-03-06 23:07:34 +0000134
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000135 if (itemfailed) {
136 /* do_mkvalue() should have already set an error */
137 Py_DECREF(v);
138 return NULL;
139 }
140 if (**p_format != endchar) {
141 Py_DECREF(v);
142 PyErr_SetString(PyExc_SystemError,
143 "Unmatched paren in format");
144 return NULL;
145 }
146 if (endchar)
147 ++*p_format;
148 return v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149}
150
Fred Drake25d34472000-04-28 14:42:37 +0000151static int
152_ustrlen(Py_UNICODE *u)
153{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000154 int i = 0;
155 Py_UNICODE *v = u;
156 while (*v != 0) { i++; v++; }
157 return i;
Fred Drake25d34472000-04-28 14:42:37 +0000158}
159
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000162{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 PyObject *v;
164 int i;
165 int itemfailed = 0;
166 if (n < 0)
167 return NULL;
168 if ((v = PyTuple_New(n)) == NULL)
169 return NULL;
170 /* Note that we can't bail immediately on error as this will leak
171 refcounts on any 'N' arguments. */
172 for (i = 0; i < n; i++) {
173 PyObject *w = do_mkvalue(p_format, p_va, flags);
174 if (w == NULL) {
175 itemfailed = 1;
176 Py_INCREF(Py_None);
177 w = Py_None;
178 }
179 PyTuple_SET_ITEM(v, i, w);
180 }
181 if (itemfailed) {
182 /* do_mkvalue() should have already set an error */
183 Py_DECREF(v);
184 return NULL;
185 }
186 if (**p_format != endchar) {
187 Py_DECREF(v);
188 PyErr_SetString(PyExc_SystemError,
189 "Unmatched paren in format");
190 return NULL;
191 }
192 if (endchar)
193 ++*p_format;
194 return v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000195}
196
Guido van Rossum79f25d91997-04-29 20:08:16 +0000197static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198do_mkvalue(const char **p_format, va_list *p_va, int flags)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000199{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000200 for (;;) {
201 switch (*(*p_format)++) {
202 case '(':
203 return do_mktuple(p_format, p_va, ')',
204 countformat(*p_format, ')'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000205
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000206 case '[':
207 return do_mklist(p_format, p_va, ']',
208 countformat(*p_format, ']'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000209
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000210 case '{':
211 return do_mkdict(p_format, p_va, '}',
212 countformat(*p_format, '}'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000213
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 case 'b':
215 case 'B':
216 case 'h':
217 case 'i':
218 return PyLong_FromLong((long)va_arg(*p_va, int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000219
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 case 'H':
221 return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
222
223 case 'I':
224 {
225 unsigned int n;
226 n = va_arg(*p_va, unsigned int);
227 return PyLong_FromUnsignedLong(n);
228 }
229
230 case 'n':
Martin v. Löwis18e16552006-02-15 17:27:45 +0000231#if SIZEOF_SIZE_T!=SIZEOF_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232 return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000233#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
235 case 'l':
236 return PyLong_FromLong(va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000237
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238 case 'k':
239 {
240 unsigned long n;
241 n = va_arg(*p_va, unsigned long);
242 return PyLong_FromUnsignedLong(n);
243 }
Jack Jansendbd65032003-04-17 22:01:10 +0000244
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000245#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000246 case 'L':
247 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000248
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000249 case 'K':
250 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000251#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252 case 'u':
253 {
254 PyObject *v;
255 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
256 Py_ssize_t n;
257 if (**p_format == '#') {
258 ++*p_format;
259 if (flags & FLAG_SIZE_T)
260 n = va_arg(*p_va, Py_ssize_t);
261 else
262 n = va_arg(*p_va, int);
263 }
264 else
265 n = -1;
266 if (u == NULL) {
267 v = Py_None;
268 Py_INCREF(v);
269 }
270 else {
271 if (n < 0)
272 n = _ustrlen(u);
273 v = PyUnicode_FromUnicode(u, n);
274 }
275 return v;
276 }
277 case 'f':
278 case 'd':
279 return PyFloat_FromDouble(
280 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000281
Fred Drakeaec79242001-03-12 21:03:26 +0000282#ifndef WITHOUT_COMPLEX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000283 case 'D':
284 return PyComplex_FromCComplex(
285 *((Py_complex *)va_arg(*p_va, Py_complex *)));
Fred Drakeaec79242001-03-12 21:03:26 +0000286#endif /* WITHOUT_COMPLEX */
287
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000288 case 'c':
289 {
290 char p[1];
291 p[0] = (char)va_arg(*p_va, int);
292 return PyBytes_FromStringAndSize(p, 1);
293 }
294 case 'C':
295 {
296 int i = va_arg(*p_va, int);
297 if (i < 0 || i > PyUnicode_GetMax()) {
298 PyErr_SetString(PyExc_OverflowError,
299 "%c arg not in range(0x110000)");
300 return NULL;
301 }
302 return PyUnicode_FromOrdinal(i);
303 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000304
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000305 case 's':
306 case 'z':
307 {
308 PyObject *v;
309 char *str = va_arg(*p_va, char *);
310 Py_ssize_t n;
311 if (**p_format == '#') {
312 ++*p_format;
313 if (flags & FLAG_SIZE_T)
314 n = va_arg(*p_va, Py_ssize_t);
315 else
316 n = va_arg(*p_va, int);
317 }
318 else
319 n = -1;
320 if (str == NULL) {
321 v = Py_None;
322 Py_INCREF(v);
323 }
324 else {
325 if (n < 0) {
326 size_t m = strlen(str);
327 if (m > PY_SSIZE_T_MAX) {
328 PyErr_SetString(PyExc_OverflowError,
329 "string too long for Python string");
330 return NULL;
331 }
332 n = (Py_ssize_t)m;
333 }
334 v = PyUnicode_FromStringAndSize(str, n);
335 }
336 return v;
337 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000338
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000339 case 'U':
340 {
341 PyObject *v;
342 char *str = va_arg(*p_va, char *);
343 Py_ssize_t n;
344 if (**p_format == '#') {
345 ++*p_format;
346 if (flags & FLAG_SIZE_T)
347 n = va_arg(*p_va, Py_ssize_t);
348 else
349 n = va_arg(*p_va, int);
350 }
351 else
352 n = -1;
353 if (str == NULL) {
354 v = Py_None;
355 Py_INCREF(v);
356 }
357 else {
358 if (n < 0) {
359 size_t m = strlen(str);
360 if (m > PY_SSIZE_T_MAX) {
361 PyErr_SetString(PyExc_OverflowError,
362 "string too long for Python string");
363 return NULL;
364 }
365 n = (Py_ssize_t)m;
366 }
367 v = PyUnicode_FromStringAndSize(str, n);
368 }
369 return v;
370 }
Walter Dörwaldd2034312007-05-18 16:29:38 +0000371
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000372 case 'y':
373 {
374 PyObject *v;
375 char *str = va_arg(*p_va, char *);
376 Py_ssize_t n;
377 if (**p_format == '#') {
378 ++*p_format;
379 if (flags & FLAG_SIZE_T)
380 n = va_arg(*p_va, Py_ssize_t);
381 else
382 n = va_arg(*p_va, int);
383 }
384 else
385 n = -1;
386 if (str == NULL) {
387 v = Py_None;
388 Py_INCREF(v);
389 }
390 else {
391 if (n < 0) {
392 size_t m = strlen(str);
393 if (m > PY_SSIZE_T_MAX) {
394 PyErr_SetString(PyExc_OverflowError,
395 "string too long for Python bytes");
396 return NULL;
397 }
398 n = (Py_ssize_t)m;
399 }
400 v = PyBytes_FromStringAndSize(str, n);
401 }
402 return v;
403 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000404
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000405 case 'N':
406 case 'S':
407 case 'O':
408 if (**p_format == '&') {
409 typedef PyObject *(*converter)(void *);
410 converter func = va_arg(*p_va, converter);
411 void *arg = va_arg(*p_va, void *);
412 ++*p_format;
413 return (*func)(arg);
414 }
415 else {
416 PyObject *v;
417 v = va_arg(*p_va, PyObject *);
418 if (v != NULL) {
419 if (*(*p_format - 1) != 'N')
420 Py_INCREF(v);
421 }
422 else if (!PyErr_Occurred())
423 /* If a NULL was passed
424 * because a call that should
425 * have constructed a value
426 * failed, that's OK, and we
427 * pass the error on; but if
428 * no error occurred it's not
429 * clear that the caller knew
430 * what she was doing. */
431 PyErr_SetString(PyExc_SystemError,
432 "NULL object passed to Py_BuildValue");
433 return v;
434 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000435
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 case ':':
437 case ',':
438 case ' ':
439 case '\t':
440 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000441
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000442 default:
443 PyErr_SetString(PyExc_SystemError,
444 "bad format char passed to Py_BuildValue");
445 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000446
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000447 }
448 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000449}
450
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451
Fred Drakeceead6d2003-01-30 15:08:25 +0000452PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000453Py_BuildValue(const char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000454{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 va_list va;
456 PyObject* retval;
457 va_start(va, format);
458 retval = va_build_value(format, va, 0);
459 va_end(va);
460 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461}
462
463PyObject *
464_Py_BuildValue_SizeT(const char *format, ...)
465{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000466 va_list va;
467 PyObject* retval;
468 va_start(va, format);
469 retval = va_build_value(format, va, FLAG_SIZE_T);
470 va_end(va);
471 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000472}
Guido van Rossume5372401993-03-16 12:15:04 +0000473
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000475Py_VaBuildValue(const char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000476{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000477 return va_build_value(format, va, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478}
479
480PyObject *
481_Py_VaBuildValue_SizeT(const char *format, va_list va)
482{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000483 return va_build_value(format, va, FLAG_SIZE_T);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000484}
485
486static PyObject *
487va_build_value(const char *format, va_list va, int flags)
488{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000489 const char *f = format;
490 int n = countformat(f, '\0');
491 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000492
493#ifdef VA_LIST_IS_ARRAY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000494 memcpy(lva, va, sizeof(va_list));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000495#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000496#ifdef __va_copy
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000497 __va_copy(lva, va);
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000498#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 lva = va;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000500#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000501#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000502
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000503 if (n < 0)
504 return NULL;
505 if (n == 0) {
506 Py_INCREF(Py_None);
507 return Py_None;
508 }
509 if (n == 1)
510 return do_mkvalue(&f, &lva, flags);
511 return do_mktuple(&f, &lva, '\0', n, flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000512}
513
514
Guido van Rossum79f25d91997-04-29 20:08:16 +0000515PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000516PyEval_CallFunction(PyObject *obj, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000518 va_list vargs;
519 PyObject *args;
520 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000521
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000522 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000524 args = Py_VaBuildValue(format, vargs);
525 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000526
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000527 if (args == NULL)
528 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000530 res = PyEval_CallObject(obj, args);
531 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000533 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534}
535
536
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000538PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000540 va_list vargs;
541 PyObject *meth;
542 PyObject *args;
543 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000544
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000545 meth = PyObject_GetAttrString(obj, methodname);
546 if (meth == NULL)
547 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000549 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000551 args = Py_VaBuildValue(format, vargs);
552 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 if (args == NULL) {
555 Py_DECREF(meth);
556 return NULL;
557 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000559 res = PyEval_CallObject(meth, args);
560 Py_DECREF(meth);
561 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000562
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000563 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000564}
Fred Drake9e285152000-09-23 03:24:27 +0000565
566int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000567PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
Fred Drake9e285152000-09-23 03:24:27 +0000568{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000569 PyObject *dict;
570 if (!PyModule_Check(m)) {
571 PyErr_SetString(PyExc_TypeError,
572 "PyModule_AddObject() needs module as first arg");
573 return -1;
574 }
575 if (!o) {
576 if (!PyErr_Occurred())
577 PyErr_SetString(PyExc_TypeError,
578 "PyModule_AddObject() needs non-NULL value");
579 return -1;
580 }
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000581
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000582 dict = PyModule_GetDict(m);
583 if (dict == NULL) {
584 /* Internal error -- modules must have a dict! */
585 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
586 PyModule_GetName(m));
587 return -1;
588 }
589 if (PyDict_SetItemString(dict, name, o))
590 return -1;
591 Py_DECREF(o);
592 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000593}
594
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000596PyModule_AddIntConstant(PyObject *m, const char *name, long value)
Fred Drake9e285152000-09-23 03:24:27 +0000597{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000598 PyObject *o = PyLong_FromLong(value);
599 if (!o)
600 return -1;
601 if (PyModule_AddObject(m, name, o) == 0)
602 return 0;
603 Py_DECREF(o);
604 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000605}
606
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000608PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
Fred Drake9e285152000-09-23 03:24:27 +0000609{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 PyObject *o = PyUnicode_FromString(value);
611 if (!o)
612 return -1;
613 if (PyModule_AddObject(m, name, o) == 0)
614 return 0;
615 Py_DECREF(o);
616 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000617}