blob: ebe62c360ea49003bac2994f555df2bd80f84b2c [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;
R. David Murray64a1e7c2010-12-15 01:36:03 +000037 PyInterpreterState *interp = PyThreadState_Get()->interp;
38 if (interp->modules == NULL)
39 Py_FatalError("Python import machinery not initialized");
Antoine Pitrouc83ea132010-05-09 14:46:46 +000040 if (module_api_version != PYTHON_API_VERSION) {
41 char message[512];
42 PyOS_snprintf(message, sizeof(message),
43 api_version_warning, name,
44 PYTHON_API_VERSION, name,
45 module_api_version);
46 if (PyErr_Warn(PyExc_RuntimeWarning, message))
47 return NULL;
48 }
49 /* Make sure name is fully qualified.
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000050
Antoine Pitrouc83ea132010-05-09 14:46:46 +000051 This is a bit of a hack: when the shared library is loaded,
52 the module name is "package.module", but the module calls
53 Py_InitModule*() with just "module" for the name. The shared
54 library loader squirrels away the true name of the module in
55 _Py_PackageContext, and Py_InitModule*() will substitute this
56 (if the name actually matches).
57 */
58 if (_Py_PackageContext != NULL) {
59 char *p = strrchr(_Py_PackageContext, '.');
60 if (p != NULL && strcmp(name, p+1) == 0) {
61 name = _Py_PackageContext;
62 _Py_PackageContext = NULL;
63 }
64 }
65 if ((m = PyImport_AddModule(name)) == NULL)
66 return NULL;
67 d = PyModule_GetDict(m);
68 if (methods != NULL) {
69 n = PyString_FromString(name);
70 if (n == NULL)
71 return NULL;
72 for (ml = methods; ml->ml_name != NULL; ml++) {
73 if ((ml->ml_flags & METH_CLASS) ||
74 (ml->ml_flags & METH_STATIC)) {
75 PyErr_SetString(PyExc_ValueError,
76 "module functions cannot set"
77 " METH_CLASS or METH_STATIC");
78 Py_DECREF(n);
79 return NULL;
80 }
81 v = PyCFunction_NewEx(ml, passthrough, n);
82 if (v == NULL) {
83 Py_DECREF(n);
84 return NULL;
85 }
86 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
87 Py_DECREF(v);
88 Py_DECREF(n);
89 return NULL;
90 }
91 Py_DECREF(v);
92 }
93 Py_DECREF(n);
94 }
95 if (doc != NULL) {
96 v = PyString_FromString(doc);
97 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
98 Py_XDECREF(v);
99 return NULL;
100 }
101 Py_DECREF(v);
102 }
103 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104}
105
106
Guido van Rossumbf80e541993-02-08 15:49:17 +0000107/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000108
Fred Drakeceead6d2003-01-30 15:08:25 +0000109static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000110countformat(const char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000111{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000112 int count = 0;
113 int level = 0;
114 while (level > 0 || *format != endchar) {
115 switch (*format) {
116 case '\0':
117 /* Premature end */
118 PyErr_SetString(PyExc_SystemError,
119 "unmatched paren in format");
120 return -1;
121 case '(':
122 case '[':
123 case '{':
124 if (level == 0)
125 count++;
126 level++;
127 break;
128 case ')':
129 case ']':
130 case '}':
131 level--;
132 break;
133 case '#':
134 case '&':
135 case ',':
136 case ':':
137 case ' ':
138 case '\t':
139 break;
140 default:
141 if (level == 0)
142 count++;
143 }
144 format++;
145 }
146 return count;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000147}
148
149
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000150/* Generic function to create a value -- the inverse of getargs() */
151/* After an original idea and first implementation by Steven Miale */
152
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000153static PyObject *do_mktuple(const char**, va_list *, int, int, int);
154static PyObject *do_mklist(const char**, va_list *, int, int, int);
155static PyObject *do_mkdict(const char**, va_list *, int, int, int);
156static PyObject *do_mkvalue(const char**, va_list *, int);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000157
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300159static void
160do_ignore(const char **p_format, va_list *p_va, int endchar, int n, int flags)
161{
162 PyObject *v;
163 int i;
164 assert(PyErr_Occurred());
165 v = PyTuple_New(n);
166 for (i = 0; i < n; i++) {
167 PyObject *exception, *value, *tb, *w;
168 PyErr_Fetch(&exception, &value, &tb);
169 w = do_mkvalue(p_format, p_va, flags);
170 PyErr_Restore(exception, value, tb);
171 if (w != NULL) {
172 if (v != NULL) {
173 PyTuple_SET_ITEM(v, i, w);
174 }
175 else {
176 Py_DECREF(w);
177 }
178 }
179 }
180 Py_XDECREF(v);
181 if (**p_format != endchar) {
182 PyErr_SetString(PyExc_SystemError,
183 "Unmatched paren in format");
184 return;
185 }
186 if (endchar)
187 ++*p_format;
188}
189
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000191do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000192{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 PyObject *d;
194 int i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 if (n < 0)
196 return NULL;
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300197 if (n % 2) {
198 PyErr_SetString(PyExc_SystemError,
199 "Bad dict format");
200 do_ignore(p_format, p_va, endchar, n, flags);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 return NULL;
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300202 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 /* Note that we can't bail immediately on error as this will leak
204 refcounts on any 'N' arguments. */
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300205 if ((d = PyDict_New()) == NULL) {
206 do_ignore(p_format, p_va, endchar, n, flags);
207 return NULL;
208 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000209 for (i = 0; i < n; i+= 2) {
210 PyObject *k, *v;
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300211
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 k = do_mkvalue(p_format, p_va, flags);
213 if (k == NULL) {
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300214 do_ignore(p_format, p_va, endchar, n - i - 1, flags);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 Py_DECREF(d);
216 return NULL;
217 }
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300218 v = do_mkvalue(p_format, p_va, flags);
219 if (v == NULL || PyDict_SetItem(d, k, v) < 0) {
220 do_ignore(p_format, p_va, endchar, n - i - 2, flags);
221 Py_DECREF(k);
222 Py_XDECREF(v);
223 Py_DECREF(d);
224 return NULL;
225 }
226 Py_DECREF(k);
227 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000228 }
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300229 if (**p_format != endchar) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000230 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 PyErr_SetString(PyExc_SystemError,
232 "Unmatched paren in format");
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300233 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 }
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300235 if (endchar)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 ++*p_format;
237 return d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000238}
239
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000241do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 PyObject *v;
244 int i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 if (n < 0)
246 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 /* Note that we can't bail immediately on error as this will leak
248 refcounts on any 'N' arguments. */
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300249 v = PyList_New(n);
250 if (v == NULL) {
251 do_ignore(p_format, p_va, endchar, n, flags);
252 return NULL;
253 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000254 for (i = 0; i < n; i++) {
255 PyObject *w = do_mkvalue(p_format, p_va, flags);
256 if (w == NULL) {
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300257 do_ignore(p_format, p_va, endchar, n - i - 1, flags);
258 Py_DECREF(v);
259 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000260 }
261 PyList_SET_ITEM(v, i, w);
262 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000263 if (**p_format != endchar) {
264 Py_DECREF(v);
265 PyErr_SetString(PyExc_SystemError,
266 "Unmatched paren in format");
267 return NULL;
268 }
269 if (endchar)
270 ++*p_format;
271 return v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000272}
273
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000274#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000275static int
276_ustrlen(Py_UNICODE *u)
277{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000278 int i = 0;
279 Py_UNICODE *v = u;
280 while (*v != 0) { i++; v++; }
281 return i;
Fred Drake25d34472000-04-28 14:42:37 +0000282}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000283#endif
Fred Drake25d34472000-04-28 14:42:37 +0000284
Guido van Rossum79f25d91997-04-29 20:08:16 +0000285static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000286do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000287{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000288 PyObject *v;
289 int i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000290 if (n < 0)
291 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000292 /* Note that we can't bail immediately on error as this will leak
293 refcounts on any 'N' arguments. */
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300294 if ((v = PyTuple_New(n)) == NULL) {
295 do_ignore(p_format, p_va, endchar, n, flags);
296 return NULL;
297 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 for (i = 0; i < n; i++) {
299 PyObject *w = do_mkvalue(p_format, p_va, flags);
300 if (w == NULL) {
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300301 do_ignore(p_format, p_va, endchar, n - i - 1, flags);
302 Py_DECREF(v);
303 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 }
305 PyTuple_SET_ITEM(v, i, w);
306 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000307 if (**p_format != endchar) {
308 Py_DECREF(v);
309 PyErr_SetString(PyExc_SystemError,
310 "Unmatched paren in format");
311 return NULL;
312 }
313 if (endchar)
314 ++*p_format;
315 return v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000316}
317
Guido van Rossum79f25d91997-04-29 20:08:16 +0000318static PyObject *
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000319do_mkvalue(const char **p_format, va_list *p_va, int flags)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000320{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000321 for (;;) {
322 switch (*(*p_format)++) {
323 case '(':
324 return do_mktuple(p_format, p_va, ')',
325 countformat(*p_format, ')'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000326
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000327 case '[':
328 return do_mklist(p_format, p_va, ']',
329 countformat(*p_format, ']'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000330
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 case '{':
332 return do_mkdict(p_format, p_va, '}',
333 countformat(*p_format, '}'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000334
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 case 'b':
336 case 'B':
337 case 'h':
338 case 'i':
339 return PyInt_FromLong((long)va_arg(*p_va, int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000340
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 case 'H':
342 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
343
344 case 'I':
345 {
346 unsigned int n;
347 n = va_arg(*p_va, unsigned int);
348 if (n > (unsigned long)PyInt_GetMax())
349 return PyLong_FromUnsignedLong((unsigned long)n);
350 else
351 return PyInt_FromLong(n);
352 }
353
354 case 'n':
Martin v. Löwis18e16552006-02-15 17:27:45 +0000355#if SIZEOF_SIZE_T!=SIZEOF_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000356 return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000357#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
359 case 'l':
360 return PyInt_FromLong(va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000361
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 case 'k':
363 {
364 unsigned long n;
365 n = va_arg(*p_va, unsigned long);
366 if (n > (unsigned long)PyInt_GetMax())
367 return PyLong_FromUnsignedLong(n);
368 else
369 return PyInt_FromLong(n);
370 }
Jack Jansendbd65032003-04-17 22:01:10 +0000371
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000372#ifdef HAVE_LONG_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000373 case 'L':
374 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000375
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000376 case 'K':
377 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000378#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000379#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000380 case 'u':
381 {
382 PyObject *v;
383 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
384 Py_ssize_t n;
385 if (**p_format == '#') {
386 ++*p_format;
387 if (flags & FLAG_SIZE_T)
388 n = va_arg(*p_va, Py_ssize_t);
389 else
390 n = va_arg(*p_va, int);
391 }
392 else
393 n = -1;
394 if (u == NULL) {
395 v = Py_None;
396 Py_INCREF(v);
397 }
398 else {
399 if (n < 0)
400 n = _ustrlen(u);
401 v = PyUnicode_FromUnicode(u, n);
402 }
403 return v;
404 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000405#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000406 case 'f':
407 case 'd':
408 return PyFloat_FromDouble(
409 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000410
Fred Drakeaec79242001-03-12 21:03:26 +0000411#ifndef WITHOUT_COMPLEX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000412 case 'D':
413 return PyComplex_FromCComplex(
414 *((Py_complex *)va_arg(*p_va, Py_complex *)));
Fred Drakeaec79242001-03-12 21:03:26 +0000415#endif /* WITHOUT_COMPLEX */
416
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000417 case 'c':
418 {
419 char p[1];
420 p[0] = (char)va_arg(*p_va, int);
421 return PyString_FromStringAndSize(p, 1);
422 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000423
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000424 case 's':
425 case 'z':
426 {
427 PyObject *v;
428 char *str = va_arg(*p_va, char *);
429 Py_ssize_t n;
430 if (**p_format == '#') {
431 ++*p_format;
432 if (flags & FLAG_SIZE_T)
433 n = va_arg(*p_va, Py_ssize_t);
434 else
435 n = va_arg(*p_va, int);
436 }
437 else
438 n = -1;
439 if (str == NULL) {
440 v = Py_None;
441 Py_INCREF(v);
442 }
443 else {
444 if (n < 0) {
445 size_t m = strlen(str);
446 if (m > PY_SSIZE_T_MAX) {
447 PyErr_SetString(PyExc_OverflowError,
448 "string too long for Python string");
449 return NULL;
450 }
451 n = (Py_ssize_t)m;
452 }
453 v = PyString_FromStringAndSize(str, n);
454 }
455 return v;
456 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000457
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 case 'N':
459 case 'S':
460 case 'O':
461 if (**p_format == '&') {
462 typedef PyObject *(*converter)(void *);
463 converter func = va_arg(*p_va, converter);
464 void *arg = va_arg(*p_va, void *);
465 ++*p_format;
466 return (*func)(arg);
467 }
468 else {
469 PyObject *v;
470 v = va_arg(*p_va, PyObject *);
471 if (v != NULL) {
472 if (*(*p_format - 1) != 'N')
473 Py_INCREF(v);
474 }
475 else if (!PyErr_Occurred())
476 /* If a NULL was passed
477 * because a call that should
478 * have constructed a value
479 * failed, that's OK, and we
480 * pass the error on; but if
481 * no error occurred it's not
482 * clear that the caller knew
483 * what she was doing. */
484 PyErr_SetString(PyExc_SystemError,
485 "NULL object passed to Py_BuildValue");
486 return v;
487 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000489 case ':':
490 case ',':
491 case ' ':
492 case '\t':
493 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000494
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 default:
496 PyErr_SetString(PyExc_SystemError,
497 "bad format char passed to Py_BuildValue");
498 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000500 }
501 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000502}
503
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000504
Fred Drakeceead6d2003-01-30 15:08:25 +0000505PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000506Py_BuildValue(const char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000507{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508 va_list va;
509 PyObject* retval;
510 va_start(va, format);
511 retval = va_build_value(format, va, 0);
512 va_end(va);
513 return retval;
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000514}
515
516PyObject *
517_Py_BuildValue_SizeT(const char *format, ...)
518{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000519 va_list va;
520 PyObject* retval;
521 va_start(va, format);
522 retval = va_build_value(format, va, FLAG_SIZE_T);
523 va_end(va);
524 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000525}
Guido van Rossume5372401993-03-16 12:15:04 +0000526
Guido van Rossum79f25d91997-04-29 20:08:16 +0000527PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000528Py_VaBuildValue(const char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000529{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000530 return va_build_value(format, va, 0);
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000531}
532
533PyObject *
534_Py_VaBuildValue_SizeT(const char *format, va_list va)
535{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000536 return va_build_value(format, va, FLAG_SIZE_T);
Martin v. Löwis5cb69362006-04-14 09:08:42 +0000537}
538
539static PyObject *
540va_build_value(const char *format, va_list va, int flags)
541{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000542 const char *f = format;
543 int n = countformat(f, '\0');
544 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545
546#ifdef VA_LIST_IS_ARRAY
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000547 memcpy(lva, va, sizeof(va_list));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000549#ifdef __va_copy
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000550 __va_copy(lva, va);
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000551#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000552 lva = va;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000554#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 if (n < 0)
557 return NULL;
558 if (n == 0) {
559 Py_INCREF(Py_None);
560 return Py_None;
561 }
562 if (n == 1)
563 return do_mkvalue(&f, &lva, flags);
564 return do_mktuple(&f, &lva, '\0', n, flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565}
566
567
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000569PyEval_CallFunction(PyObject *obj, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 va_list vargs;
572 PyObject *args;
573 PyObject *res;
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 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000583 res = PyEval_CallObject(obj, args);
584 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000586 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000587}
588
589
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000591PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000593 va_list vargs;
594 PyObject *meth;
595 PyObject *args;
596 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000597
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 meth = PyObject_GetAttrString(obj, methodname);
599 if (meth == NULL)
600 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000604 args = Py_VaBuildValue(format, vargs);
605 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000607 if (args == NULL) {
608 Py_DECREF(meth);
609 return NULL;
610 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000612 res = PyEval_CallObject(meth, args);
613 Py_DECREF(meth);
614 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000616 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000617}
Fred Drake9e285152000-09-23 03:24:27 +0000618
619int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000620PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
Fred Drake9e285152000-09-23 03:24:27 +0000621{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 PyObject *dict;
623 if (!PyModule_Check(m)) {
624 PyErr_SetString(PyExc_TypeError,
625 "PyModule_AddObject() needs module as first arg");
626 return -1;
627 }
628 if (!o) {
629 if (!PyErr_Occurred())
630 PyErr_SetString(PyExc_TypeError,
631 "PyModule_AddObject() needs non-NULL value");
632 return -1;
633 }
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000634
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000635 dict = PyModule_GetDict(m);
636 if (dict == NULL) {
637 /* Internal error -- modules must have a dict! */
638 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
639 PyModule_GetName(m));
640 return -1;
641 }
642 if (PyDict_SetItemString(dict, name, o))
643 return -1;
644 Py_DECREF(o);
645 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000646}
647
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000648int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000649PyModule_AddIntConstant(PyObject *m, const char *name, long value)
Fred Drake9e285152000-09-23 03:24:27 +0000650{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000651 PyObject *o = PyInt_FromLong(value);
652 if (!o)
653 return -1;
654 if (PyModule_AddObject(m, name, o) == 0)
655 return 0;
656 Py_DECREF(o);
657 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000658}
659
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000660int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000661PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
Fred Drake9e285152000-09-23 03:24:27 +0000662{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000663 PyObject *o = PyString_FromString(value);
664 if (!o)
665 return -1;
666 if (PyModule_AddObject(m, name, o) == 0)
667 return 0;
668 Py_DECREF(o);
669 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000670}