blob: 6ee48f3f1e44c5f39899a681599c04891f6a95dc [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
Martin v. Löwis5cb69362006-04-14 09:08:42 +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
Martin v. Löwis5cb69362006-04-14 09:08:42 +00009static PyObject *va_build_value(const char *, va_list, int);
Martin v. Löwis5cb69362006-04-14 09:08:42 +000010
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 Rossum40b33c61997-08-02 03:07:46 +000014/* Py_InitModule4() parameters:
Guido van Rossum970a0a21995-01-09 17:47:20 +000015 - name is the module name
16 - methods is the list of top-level functions
17 - doc is the documentation string
18 - passthrough is passed as self to functions defined in the module
19 - api_version is the value of PYTHON_API_VERSION at the time the
20 module was compiled
Guido van Rossum40b33c61997-08-02 03:07:46 +000021
22 Return value is a borrowed reference to the module object; or NULL
23 if an error occurred (in Python 1.4 and before, errors were fatal).
24 Errors may still leak memory.
Guido van Rossum50620fa1995-01-07 12:43:18 +000025*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +000026
Guido van Rossum970a0a21995-01-09 17:47:20 +000027static char api_version_warning[] =
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000028"Python C API version mismatch for module %.100s:\
29 This Python has API version %d, module %.100s has version %d.";
Guido van Rossum970a0a21995-01-09 17:47:20 +000030
Guido van Rossum79f25d91997-04-29 20:08:16 +000031PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000032Py_InitModule4(const char *name, PyMethodDef *methods, const char *doc,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000033 PyObject *passthrough, int module_api_version)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000035 PyObject *m, *d, *v, *n;
36 PyMethodDef *ml;
37 if (!Py_IsInitialized())
38 Py_FatalError("Interpreter not initialized (version mismatch?)");
39 if (module_api_version != PYTHON_API_VERSION) {
40 char message[512];
41 PyOS_snprintf(message, sizeof(message),
42 api_version_warning, name,
43 PYTHON_API_VERSION, name,
44 module_api_version);
45 if (PyErr_Warn(PyExc_RuntimeWarning, message))
46 return NULL;
47 }
48 /* Make sure name is fully qualified.
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000049
Antoine Pitrouc83ea132010-05-09 14:46:46 +000050 This is a bit of a hack: when the shared library is loaded,
51 the module name is "package.module", but the module calls
52 Py_InitModule*() with just "module" for the name. The shared
53 library loader squirrels away the true name of the module in
54 _Py_PackageContext, and Py_InitModule*() will substitute this
55 (if the name actually matches).
56 */
57 if (_Py_PackageContext != NULL) {
58 char *p = strrchr(_Py_PackageContext, '.');
59 if (p != NULL && strcmp(name, p+1) == 0) {
60 name = _Py_PackageContext;
61 _Py_PackageContext = NULL;
62 }
63 }
64 if ((m = PyImport_AddModule(name)) == NULL)
65 return NULL;
66 d = PyModule_GetDict(m);
67 if (methods != NULL) {
68 n = PyString_FromString(name);
69 if (n == NULL)
70 return NULL;
71 for (ml = methods; ml->ml_name != NULL; ml++) {
72 if ((ml->ml_flags & METH_CLASS) ||
73 (ml->ml_flags & METH_STATIC)) {
74 PyErr_SetString(PyExc_ValueError,
75 "module functions cannot set"
76 " METH_CLASS or METH_STATIC");
77 Py_DECREF(n);
78 return NULL;
79 }
80 v = PyCFunction_NewEx(ml, passthrough, n);
81 if (v == NULL) {
82 Py_DECREF(n);
83 return NULL;
84 }
85 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
86 Py_DECREF(v);
87 Py_DECREF(n);
88 return NULL;
89 }
90 Py_DECREF(v);
91 }
92 Py_DECREF(n);
93 }
94 if (doc != NULL) {
95 v = PyString_FromString(doc);
96 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
97 Py_XDECREF(v);
98 return NULL;
99 }
100 Py_DECREF(v);
101 }
102 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103}
104
105
Guido van Rossumbf80e541993-02-08 15:49:17 +0000106/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000107
Fred Drakeceead6d2003-01-30 15:08:25 +0000108static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000109countformat(const char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000110{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 int count = 0;
112 int level = 0;
113 while (level > 0 || *format != endchar) {
114 switch (*format) {
115 case '\0':
116 /* Premature end */
117 PyErr_SetString(PyExc_SystemError,
118 "unmatched paren in format");
119 return -1;
120 case '(':
121 case '[':
122 case '{':
123 if (level == 0)
124 count++;
125 level++;
126 break;
127 case ')':
128 case ']':
129 case '}':
130 level--;
131 break;
132 case '#':
133 case '&':
134 case ',':
135 case ':':
136 case ' ':
137 case '\t':
138 break;
139 default:
140 if (level == 0)
141 count++;
142 }
143 format++;
144 }
145 return count;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000146}
147
148
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000149/* Generic function to create a value -- the inverse of getargs() */
150/* After an original idea and first implementation by Steven Miale */
151
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000152static PyObject *do_mktuple(const char**, va_list *, int, int, int);
153static PyObject *do_mklist(const char**, va_list *, int, int, int);
154static PyObject *do_mkdict(const char**, va_list *, int, int, int);
155static PyObject *do_mkvalue(const char**, va_list *, int);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000156
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000157
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000159do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000160{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 PyObject *d;
162 int i;
163 int itemfailed = 0;
164 if (n < 0)
165 return NULL;
166 if ((d = PyDict_New()) == NULL)
167 return NULL;
168 /* Note that we can't bail immediately on error as this will leak
169 refcounts on any 'N' arguments. */
170 for (i = 0; i < n; i+= 2) {
171 PyObject *k, *v;
172 int err;
173 k = do_mkvalue(p_format, p_va, flags);
174 if (k == NULL) {
175 itemfailed = 1;
176 Py_INCREF(Py_None);
177 k = Py_None;
178 }
179 v = do_mkvalue(p_format, p_va, flags);
180 if (v == NULL) {
181 itemfailed = 1;
182 Py_INCREF(Py_None);
183 v = Py_None;
184 }
185 err = PyDict_SetItem(d, k, v);
186 Py_DECREF(k);
187 Py_DECREF(v);
188 if (err < 0 || itemfailed) {
189 Py_DECREF(d);
190 return NULL;
191 }
192 }
193 if (d != NULL && **p_format != endchar) {
194 Py_DECREF(d);
195 d = NULL;
196 PyErr_SetString(PyExc_SystemError,
197 "Unmatched paren in format");
198 }
199 else if (endchar)
200 ++*p_format;
201 return d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000202}
203
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000205do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000206{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000207 PyObject *v;
208 int i;
209 int itemfailed = 0;
210 if (n < 0)
211 return NULL;
212 v = PyList_New(n);
213 if (v == NULL)
214 return NULL;
215 /* Note that we can't bail immediately on error as this will leak
216 refcounts on any 'N' arguments. */
217 for (i = 0; i < n; i++) {
218 PyObject *w = do_mkvalue(p_format, p_va, flags);
219 if (w == NULL) {
220 itemfailed = 1;
221 Py_INCREF(Py_None);
222 w = Py_None;
223 }
224 PyList_SET_ITEM(v, i, w);
225 }
Neal Norwitz3e90fa52006-03-06 23:07:34 +0000226
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 if (itemfailed) {
228 /* do_mkvalue() should have already set an error */
229 Py_DECREF(v);
230 return NULL;
231 }
232 if (**p_format != endchar) {
233 Py_DECREF(v);
234 PyErr_SetString(PyExc_SystemError,
235 "Unmatched paren in format");
236 return NULL;
237 }
238 if (endchar)
239 ++*p_format;
240 return v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000241}
242
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000243#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000244static int
245_ustrlen(Py_UNICODE *u)
246{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 int i = 0;
248 Py_UNICODE *v = u;
249 while (*v != 0) { i++; v++; }
250 return i;
Fred Drake25d34472000-04-28 14:42:37 +0000251}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000252#endif
Fred Drake25d34472000-04-28 14:42:37 +0000253
Guido van Rossum79f25d91997-04-29 20:08:16 +0000254static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000255do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000256{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000257 PyObject *v;
258 int i;
259 int itemfailed = 0;
260 if (n < 0)
261 return NULL;
262 if ((v = PyTuple_New(n)) == NULL)
263 return NULL;
264 /* Note that we can't bail immediately on error as this will leak
265 refcounts on any 'N' arguments. */
266 for (i = 0; i < n; i++) {
267 PyObject *w = do_mkvalue(p_format, p_va, flags);
268 if (w == NULL) {
269 itemfailed = 1;
270 Py_INCREF(Py_None);
271 w = Py_None;
272 }
273 PyTuple_SET_ITEM(v, i, w);
274 }
275 if (itemfailed) {
276 /* do_mkvalue() should have already set an error */
277 Py_DECREF(v);
278 return NULL;
279 }
280 if (**p_format != endchar) {
281 Py_DECREF(v);
282 PyErr_SetString(PyExc_SystemError,
283 "Unmatched paren in format");
284 return NULL;
285 }
286 if (endchar)
287 ++*p_format;
288 return v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000289}
290
Guido van Rossum79f25d91997-04-29 20:08:16 +0000291static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000292do_mkvalue(const char **p_format, va_list *p_va, int flags)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000293{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 for (;;) {
295 switch (*(*p_format)++) {
296 case '(':
297 return do_mktuple(p_format, p_va, ')',
298 countformat(*p_format, ')'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000299
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000300 case '[':
301 return do_mklist(p_format, p_va, ']',
302 countformat(*p_format, ']'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 case '{':
305 return do_mkdict(p_format, p_va, '}',
306 countformat(*p_format, '}'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000307
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000308 case 'b':
309 case 'B':
310 case 'h':
311 case 'i':
312 return PyInt_FromLong((long)va_arg(*p_va, int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000313
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000314 case 'H':
315 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
316
317 case 'I':
318 {
319 unsigned int n;
320 n = va_arg(*p_va, unsigned int);
321 if (n > (unsigned long)PyInt_GetMax())
322 return PyLong_FromUnsignedLong((unsigned long)n);
323 else
324 return PyInt_FromLong(n);
325 }
326
327 case 'n':
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328#if SIZEOF_SIZE_T!=SIZEOF_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000329 return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000330#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
332 case 'l':
333 return PyInt_FromLong(va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000334
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 case 'k':
336 {
337 unsigned long n;
338 n = va_arg(*p_va, unsigned long);
339 if (n > (unsigned long)PyInt_GetMax())
340 return PyLong_FromUnsignedLong(n);
341 else
342 return PyInt_FromLong(n);
343 }
Jack Jansendbd65032003-04-17 22:01:10 +0000344
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000345#ifdef HAVE_LONG_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 case 'L':
347 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000348
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 case 'K':
350 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000351#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000352#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 case 'u':
354 {
355 PyObject *v;
356 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
357 Py_ssize_t n;
358 if (**p_format == '#') {
359 ++*p_format;
360 if (flags & FLAG_SIZE_T)
361 n = va_arg(*p_va, Py_ssize_t);
362 else
363 n = va_arg(*p_va, int);
364 }
365 else
366 n = -1;
367 if (u == NULL) {
368 v = Py_None;
369 Py_INCREF(v);
370 }
371 else {
372 if (n < 0)
373 n = _ustrlen(u);
374 v = PyUnicode_FromUnicode(u, n);
375 }
376 return v;
377 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000378#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000379 case 'f':
380 case 'd':
381 return PyFloat_FromDouble(
382 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Fred Drakeaec79242001-03-12 21:03:26 +0000384#ifndef WITHOUT_COMPLEX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000385 case 'D':
386 return PyComplex_FromCComplex(
387 *((Py_complex *)va_arg(*p_va, Py_complex *)));
Fred Drakeaec79242001-03-12 21:03:26 +0000388#endif /* WITHOUT_COMPLEX */
389
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000390 case 'c':
391 {
392 char p[1];
393 p[0] = (char)va_arg(*p_va, int);
394 return PyString_FromStringAndSize(p, 1);
395 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000396
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 case 's':
398 case 'z':
399 {
400 PyObject *v;
401 char *str = va_arg(*p_va, char *);
402 Py_ssize_t n;
403 if (**p_format == '#') {
404 ++*p_format;
405 if (flags & FLAG_SIZE_T)
406 n = va_arg(*p_va, Py_ssize_t);
407 else
408 n = va_arg(*p_va, int);
409 }
410 else
411 n = -1;
412 if (str == NULL) {
413 v = Py_None;
414 Py_INCREF(v);
415 }
416 else {
417 if (n < 0) {
418 size_t m = strlen(str);
419 if (m > PY_SSIZE_T_MAX) {
420 PyErr_SetString(PyExc_OverflowError,
421 "string too long for Python string");
422 return NULL;
423 }
424 n = (Py_ssize_t)m;
425 }
426 v = PyString_FromStringAndSize(str, n);
427 }
428 return v;
429 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000430
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000431 case 'N':
432 case 'S':
433 case 'O':
434 if (**p_format == '&') {
435 typedef PyObject *(*converter)(void *);
436 converter func = va_arg(*p_va, converter);
437 void *arg = va_arg(*p_va, void *);
438 ++*p_format;
439 return (*func)(arg);
440 }
441 else {
442 PyObject *v;
443 v = va_arg(*p_va, PyObject *);
444 if (v != NULL) {
445 if (*(*p_format - 1) != 'N')
446 Py_INCREF(v);
447 }
448 else if (!PyErr_Occurred())
449 /* If a NULL was passed
450 * because a call that should
451 * have constructed a value
452 * failed, that's OK, and we
453 * pass the error on; but if
454 * no error occurred it's not
455 * clear that the caller knew
456 * what she was doing. */
457 PyErr_SetString(PyExc_SystemError,
458 "NULL object passed to Py_BuildValue");
459 return v;
460 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000461
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000462 case ':':
463 case ',':
464 case ' ':
465 case '\t':
466 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 default:
469 PyErr_SetString(PyExc_SystemError,
470 "bad format char passed to Py_BuildValue");
471 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 }
474 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000475}
476
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000477
Fred Drakeceead6d2003-01-30 15:08:25 +0000478PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000479Py_BuildValue(const char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000480{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000481 va_list va;
482 PyObject* retval;
483 va_start(va, format);
484 retval = va_build_value(format, va, 0);
485 va_end(va);
486 return retval;
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000487}
488
489PyObject *
490_Py_BuildValue_SizeT(const char *format, ...)
491{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 va_list va;
493 PyObject* retval;
494 va_start(va, format);
495 retval = va_build_value(format, va, FLAG_SIZE_T);
496 va_end(va);
497 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000498}
Guido van Rossume5372401993-03-16 12:15:04 +0000499
Guido van Rossum79f25d91997-04-29 20:08:16 +0000500PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000501Py_VaBuildValue(const char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000502{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 return va_build_value(format, va, 0);
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000504}
505
506PyObject *
507_Py_VaBuildValue_SizeT(const char *format, va_list va)
508{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000509 return va_build_value(format, va, FLAG_SIZE_T);
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000510}
511
512static PyObject *
513va_build_value(const char *format, va_list va, int flags)
514{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 const char *f = format;
516 int n = countformat(f, '\0');
517 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000518
519#ifdef VA_LIST_IS_ARRAY
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 memcpy(lva, va, sizeof(va_list));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000521#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000522#ifdef __va_copy
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523 __va_copy(lva, va);
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000524#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000525 lva = va;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000526#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000527#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000528
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529 if (n < 0)
530 return NULL;
531 if (n == 0) {
532 Py_INCREF(Py_None);
533 return Py_None;
534 }
535 if (n == 1)
536 return do_mkvalue(&f, &lva, flags);
537 return do_mktuple(&f, &lva, '\0', n, flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538}
539
540
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000542PyEval_CallFunction(PyObject *obj, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000544 va_list vargs;
545 PyObject *args;
546 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000550 args = Py_VaBuildValue(format, vargs);
551 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 if (args == NULL)
554 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 res = PyEval_CallObject(obj, args);
557 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000559 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560}
561
562
Guido van Rossum79f25d91997-04-29 20:08:16 +0000563PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000564PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 va_list vargs;
567 PyObject *meth;
568 PyObject *args;
569 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 meth = PyObject_GetAttrString(obj, methodname);
572 if (meth == NULL)
573 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000575 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000577 args = Py_VaBuildValue(format, vargs);
578 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000579
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000580 if (args == NULL) {
581 Py_DECREF(meth);
582 return NULL;
583 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000584
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 res = PyEval_CallObject(meth, args);
586 Py_DECREF(meth);
587 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000588
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000589 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000590}
Fred Drake9e285152000-09-23 03:24:27 +0000591
592int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000593PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
Fred Drake9e285152000-09-23 03:24:27 +0000594{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000595 PyObject *dict;
596 if (!PyModule_Check(m)) {
597 PyErr_SetString(PyExc_TypeError,
598 "PyModule_AddObject() needs module as first arg");
599 return -1;
600 }
601 if (!o) {
602 if (!PyErr_Occurred())
603 PyErr_SetString(PyExc_TypeError,
604 "PyModule_AddObject() needs non-NULL value");
605 return -1;
606 }
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000607
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 dict = PyModule_GetDict(m);
609 if (dict == NULL) {
610 /* Internal error -- modules must have a dict! */
611 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
612 PyModule_GetName(m));
613 return -1;
614 }
615 if (PyDict_SetItemString(dict, name, o))
616 return -1;
617 Py_DECREF(o);
618 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000619}
620
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000621int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000622PyModule_AddIntConstant(PyObject *m, const char *name, long value)
Fred Drake9e285152000-09-23 03:24:27 +0000623{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 PyObject *o = PyInt_FromLong(value);
625 if (!o)
626 return -1;
627 if (PyModule_AddObject(m, name, o) == 0)
628 return 0;
629 Py_DECREF(o);
630 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000631}
632
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000634PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
Fred Drake9e285152000-09-23 03:24:27 +0000635{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000636 PyObject *o = PyString_FromString(value);
637 if (!o)
638 return -1;
639 if (PyModule_AddObject(m, name, o) == 0)
640 return 0;
641 Py_DECREF(o);
642 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000643}