blob: 12f3f17b487011561c13568f2aaa3e23eda89ca7 [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
Guido van Rossum1d5735e1994-08-30 08:27:36 +00006#ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
7typedef extended va_double;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00008#else
Guido van Rossum1d5735e1994-08-30 08:27:36 +00009typedef double va_double;
10#endif
11
Guido van Rossum2e58ff31997-11-19 18:53:33 +000012/* Package context -- the full module name for package imports */
13char *_Py_PackageContext = NULL;
14
Guido van Rossum40b33c61997-08-02 03:07:46 +000015/* Py_InitModule4() parameters:
Guido van Rossum970a0a21995-01-09 17:47:20 +000016 - name is the module name
17 - methods is the list of top-level functions
18 - doc is the documentation string
19 - passthrough is passed as self to functions defined in the module
20 - api_version is the value of PYTHON_API_VERSION at the time the
21 module was compiled
Guido van Rossum40b33c61997-08-02 03:07:46 +000022
23 Return value is a borrowed reference to the module object; or NULL
24 if an error occurred (in Python 1.4 and before, errors were fatal).
25 Errors may still leak memory.
Guido van Rossum50620fa1995-01-07 12:43:18 +000026*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +000027
Guido van Rossum970a0a21995-01-09 17:47:20 +000028static char api_version_warning[] =
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000029"Python C API version mismatch for module %.100s:\
30 This Python has API version %d, module %.100s has version %d.";
Guido van Rossum970a0a21995-01-09 17:47:20 +000031
Guido van Rossum79f25d91997-04-29 20:08:16 +000032PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000033Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
34 PyObject *passthrough, int module_api_version)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000036 PyObject *m, *d, *v, *n;
Guido van Rossum79f25d91997-04-29 20:08:16 +000037 PyMethodDef *ml;
Guido van Rossum413407f2000-08-04 14:00:14 +000038 if (!Py_IsInitialized())
39 Py_FatalError("Interpreter not initialized (version mismatch?)");
Marc-André Lemburge5006eb2001-07-31 13:24:44 +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 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000049 /* Make sure name is fully qualified.
50
51 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 */
Guido van Rossum2e58ff31997-11-19 18:53:33 +000058 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 }
Guido van Rossum40b33c61997-08-02 03:07:46 +000065 if ((m = PyImport_AddModule(name)) == NULL)
66 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000067 d = PyModule_GetDict(m);
Fred Drake233cc592002-08-14 20:57:56 +000068 if (methods != NULL) {
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000069 n = PyString_FromString(name);
70 if (n == NULL)
71 return NULL;
Fred Drake233cc592002-08-14 20:57:56 +000072 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 return NULL;
79 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000080 v = PyCFunction_NewEx(ml, passthrough, n);
Fred Drake233cc592002-08-14 20:57:56 +000081 if (v == NULL)
82 return NULL;
83 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
84 Py_DECREF(v);
85 return NULL;
86 }
Fred Drake289898c2001-08-04 03:11:25 +000087 Py_DECREF(v);
Fred Drake289898c2001-08-04 03:11:25 +000088 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089 }
Guido van Rossum50620fa1995-01-07 12:43:18 +000090 if (doc != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +000091 v = PyString_FromString(doc);
Fred Drake289898c2001-08-04 03:11:25 +000092 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
Just van Rossumbbfd8592002-12-15 13:45:32 +000093 Py_XDECREF(v);
Guido van Rossum40b33c61997-08-02 03:07:46 +000094 return NULL;
Fred Drake289898c2001-08-04 03:11:25 +000095 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000096 Py_DECREF(v);
Guido van Rossum50620fa1995-01-07 12:43:18 +000097 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000098 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099}
100
101
Guido van Rossumbf80e541993-02-08 15:49:17 +0000102/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000103
Fred Drakeceead6d2003-01-30 15:08:25 +0000104static int
105countformat(char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000106{
107 int count = 0;
108 int level = 0;
109 while (level > 0 || *format != endchar) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000110 switch (*format) {
111 case '\0':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000112 /* Premature end */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000113 PyErr_SetString(PyExc_SystemError,
114 "unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000115 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000116 case '(':
117 case '[':
118 case '{':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000119 if (level == 0)
120 count++;
121 level++;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000122 break;
123 case ')':
124 case ']':
125 case '}':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000126 level--;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000127 break;
128 case '#':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000129 case '&':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000130 case ',':
131 case ':':
132 case ' ':
133 case '\t':
134 break;
135 default:
136 if (level == 0)
137 count++;
138 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000139 format++;
140 }
141 return count;
142}
143
144
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000145/* Generic function to create a value -- the inverse of getargs() */
146/* After an original idea and first implementation by Steven Miale */
147
Tim Petersdbd9ba62000-07-09 03:09:57 +0000148static PyObject *do_mktuple(char**, va_list *, int, int);
149static PyObject *do_mklist(char**, va_list *, int, int);
150static PyObject *do_mkdict(char**, va_list *, int, int);
151static PyObject *do_mkvalue(char**, va_list *);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000152
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000153
Guido van Rossum79f25d91997-04-29 20:08:16 +0000154static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000156{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000157 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158 int i;
159 if (n < 0)
160 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161 if ((d = PyDict_New()) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000162 return NULL;
163 for (i = 0; i < n; i+= 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyObject *k, *v;
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000165 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000166 k = do_mkvalue(p_format, p_va);
167 if (k == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000169 return NULL;
170 }
171 v = do_mkvalue(p_format, p_va);
172 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 Py_DECREF(k);
174 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000175 return NULL;
176 }
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000177 err = PyDict_SetItem(d, k, v);
178 Py_DECREF(k);
179 Py_DECREF(v);
180 if (err < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000181 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000182 return NULL;
183 }
184 }
185 if (d != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000186 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000187 d = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 PyErr_SetString(PyExc_SystemError,
189 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000190 }
191 else if (endchar)
192 ++*p_format;
193 return d;
194}
195
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000197do_mklist(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000198{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000199 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000200 int i;
201 if (n < 0)
202 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000203 if ((v = PyList_New(n)) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000204 return NULL;
205 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000207 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000209 return NULL;
210 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211 PyList_SetItem(v, i, w);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000212 }
213 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000215 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000216 PyErr_SetString(PyExc_SystemError,
217 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000218 }
219 else if (endchar)
220 ++*p_format;
221 return v;
222}
223
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000224#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000225static int
226_ustrlen(Py_UNICODE *u)
227{
228 int i = 0;
229 Py_UNICODE *v = u;
230 while (*v != 0) { i++; v++; }
231 return i;
232}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000233#endif
Fred Drake25d34472000-04-28 14:42:37 +0000234
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000237{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000238 PyObject *v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000239 int i;
240 if (n < 0)
241 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 if ((v = PyTuple_New(n)) == NULL)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000243 return NULL;
244 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000246 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000247 Py_DECREF(v);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000248 return NULL;
249 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000250 PyTuple_SetItem(v, i, w);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000251 }
252 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253 Py_DECREF(v);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000254 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000255 PyErr_SetString(PyExc_SystemError,
256 "Unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000257 }
258 else if (endchar)
259 ++*p_format;
260 return v;
261}
262
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264do_mkvalue(char **p_format, va_list *p_va)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000265{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000266 for (;;) {
267 switch (*(*p_format)++) {
268 case '(':
269 return do_mktuple(p_format, p_va, ')',
270 countformat(*p_format, ')'));
271
272 case '[':
273 return do_mklist(p_format, p_va, ']',
274 countformat(*p_format, ']'));
275
276 case '{':
277 return do_mkdict(p_format, p_va, '}',
278 countformat(*p_format, '}'));
279
280 case 'b':
Jack Jansenb763b9d2000-09-15 12:51:01 +0000281 case 'B':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000282 case 'h':
283 case 'i':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284 return PyInt_FromLong((long)va_arg(*p_va, int));
Jack Jansend50338f2000-07-06 12:22:00 +0000285
286 case 'H':
287 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000288
289 case 'l':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 return PyInt_FromLong((long)va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000291
Jack Jansendbd65032003-04-17 22:01:10 +0000292 case 'k':
293 return PyInt_FromLong((long)va_arg(*p_va, unsigned long));
294
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000295#ifdef HAVE_LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000296 case 'L':
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000297 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000298
299 case 'K':
300 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000301#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000302#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000303 case 'u':
304 {
305 PyObject *v;
306 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
307 int n;
308 if (**p_format == '#') {
309 ++*p_format;
310 n = va_arg(*p_va, int);
311 }
312 else
313 n = -1;
314 if (u == NULL) {
315 v = Py_None;
316 Py_INCREF(v);
317 }
318 else {
319 if (n < 0)
320 n = _ustrlen(u);
321 v = PyUnicode_FromUnicode(u, n);
322 }
323 return v;
324 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000325#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000326 case 'f':
327 case 'd':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000328 return PyFloat_FromDouble(
329 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000330
Fred Drakeaec79242001-03-12 21:03:26 +0000331#ifndef WITHOUT_COMPLEX
332 case 'D':
333 return PyComplex_FromCComplex(
334 *((Py_complex *)va_arg(*p_va, Py_complex *)));
335#endif /* WITHOUT_COMPLEX */
336
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000337 case 'c':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000338 {
339 char p[1];
340 p[0] = va_arg(*p_va, int);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000341 return PyString_FromStringAndSize(p, 1);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000342 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000343
344 case 's':
345 case 'z':
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000346 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000347 PyObject *v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000348 char *str = va_arg(*p_va, char *);
349 int n;
350 if (**p_format == '#') {
351 ++*p_format;
352 n = va_arg(*p_va, int);
353 }
354 else
355 n = -1;
356 if (str == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000357 v = Py_None;
358 Py_INCREF(v);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000359 }
360 else {
Guido van Rossum582acec2000-06-28 22:07:35 +0000361 if (n < 0) {
362 size_t m = strlen(str);
363 if (m > INT_MAX) {
364 PyErr_SetString(PyExc_OverflowError,
365 "string too long for Python string");
366 return NULL;
367 }
368 n = (int)m;
369 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000370 v = PyString_FromStringAndSize(str, n);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000371 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000372 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000373 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000374
Guido van Rossumd3415001998-12-23 05:01:38 +0000375 case 'N':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000376 case 'S':
377 case 'O':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000378 if (**p_format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000379 typedef PyObject *(*converter)(void *);
Guido van Rossumd1b93931995-01-20 16:56:02 +0000380 converter func = va_arg(*p_va, converter);
381 void *arg = va_arg(*p_va, void *);
382 ++*p_format;
383 return (*func)(arg);
384 }
385 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386 PyObject *v;
387 v = va_arg(*p_va, PyObject *);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000388 if (v != NULL) {
Guido van Rossumd3415001998-12-23 05:01:38 +0000389 if (*(*p_format - 1) != 'N')
390 Py_INCREF(v);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000391 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 else if (!PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000393 /* If a NULL was passed
394 * because a call that should
395 * have constructed a value
396 * failed, that's OK, and we
397 * pass the error on; but if
398 * no error occurred it's not
399 * clear that the caller knew
400 * what she was doing. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000401 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000402 "NULL object passed to Py_BuildValue");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000403 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000404 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000405
406 case ':':
407 case ',':
408 case ' ':
409 case '\t':
410 break;
411
412 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000413 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000414 "bad format char passed to Py_BuildValue");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000415 return NULL;
416
417 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000418 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000419}
420
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000421
Fred Drakeceead6d2003-01-30 15:08:25 +0000422PyObject *
423Py_BuildValue(char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000424{
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000425 va_list va;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 PyObject* retval;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000427 va_start(va, format);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 retval = Py_VaBuildValue(format, va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000429 va_end(va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000430 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000431}
Guido van Rossume5372401993-03-16 12:15:04 +0000432
Guido van Rossum79f25d91997-04-29 20:08:16 +0000433PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000434Py_VaBuildValue(char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000435{
436 char *f = format;
437 int n = countformat(f, '\0');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000438 va_list lva;
439
440#ifdef VA_LIST_IS_ARRAY
441 memcpy(lva, va, sizeof(va_list));
442#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000443#ifdef __va_copy
444 __va_copy(lva, va);
445#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000446 lva = va;
447#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000448#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000449
Guido van Rossume5372401993-03-16 12:15:04 +0000450 if (n < 0)
451 return NULL;
452 if (n == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453 Py_INCREF(Py_None);
454 return Py_None;
Guido van Rossume5372401993-03-16 12:15:04 +0000455 }
456 if (n == 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000457 return do_mkvalue(&f, &lva);
458 return do_mktuple(&f, &lva, '\0', n);
459}
460
461
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462PyObject *
463PyEval_CallFunction(PyObject *obj, char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464{
465 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000466 PyObject *args;
467 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000468
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000469 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470
Guido van Rossum79f25d91997-04-29 20:08:16 +0000471 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472 va_end(vargs);
473
474 if (args == NULL)
475 return NULL;
476
Guido van Rossum79f25d91997-04-29 20:08:16 +0000477 res = PyEval_CallObject(obj, args);
478 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000479
480 return res;
481}
482
483
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484PyObject *
Guido van Rossumc4099e61998-08-08 20:51:26 +0000485PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486{
487 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000488 PyObject *meth;
489 PyObject *args;
490 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000491
Guido van Rossumc4099e61998-08-08 20:51:26 +0000492 meth = PyObject_GetAttrString(obj, methodname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000493 if (meth == NULL)
494 return NULL;
495
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000497
Guido van Rossum79f25d91997-04-29 20:08:16 +0000498 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499 va_end(vargs);
500
501 if (args == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000502 Py_DECREF(meth);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000503 return NULL;
504 }
505
Guido van Rossum79f25d91997-04-29 20:08:16 +0000506 res = PyEval_CallObject(meth, args);
507 Py_DECREF(meth);
508 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000509
510 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000511}
Fred Drake9e285152000-09-23 03:24:27 +0000512
513int
514PyModule_AddObject(PyObject *m, char *name, PyObject *o)
515{
516 PyObject *dict;
Fred Drake83115182002-06-17 17:16:57 +0000517 if (!PyModule_Check(m) || o == NULL) {
518 PyErr_SetString(PyExc_TypeError,
519 "PyModule_AddObject() needs module as first arg");
Fred Drake9e285152000-09-23 03:24:27 +0000520 return -1;
Fred Drake83115182002-06-17 17:16:57 +0000521 }
522 dict = PyModule_GetDict(m);
523 if (dict == NULL) {
524 /* Internal error -- modules must have a dict! */
525 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
526 PyModule_GetName(m));
527 return -1;
528 }
529 if (PyDict_SetItemString(dict, name, o))
530 return -1;
531 Py_DECREF(o);
532 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000533}
534
535int
536PyModule_AddIntConstant(PyObject *m, char *name, long value)
537{
538 return PyModule_AddObject(m, name, PyInt_FromLong(value));
539}
540
541int
542PyModule_AddStringConstant(PyObject *m, char *name, char *value)
543{
544 return PyModule_AddObject(m, name, PyString_FromString(value));
545}