blob: f53e4c362ee8bebaa0be3f69dc15b543b244fb6a [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 +00006typedef double va_double;
Guido van Rossum1d5735e1994-08-30 08:27:36 +00007
Guido van Rossum2e58ff31997-11-19 18:53:33 +00008/* Package context -- the full module name for package imports */
9char *_Py_PackageContext = NULL;
10
Guido van Rossum40b33c61997-08-02 03:07:46 +000011/* Py_InitModule4() parameters:
Guido van Rossum970a0a21995-01-09 17:47:20 +000012 - name is the module name
13 - methods is the list of top-level functions
14 - doc is the documentation string
15 - passthrough is passed as self to functions defined in the module
16 - api_version is the value of PYTHON_API_VERSION at the time the
17 module was compiled
Guido van Rossum40b33c61997-08-02 03:07:46 +000018
19 Return value is a borrowed reference to the module object; or NULL
20 if an error occurred (in Python 1.4 and before, errors were fatal).
21 Errors may still leak memory.
Guido van Rossum50620fa1995-01-07 12:43:18 +000022*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023
Guido van Rossum970a0a21995-01-09 17:47:20 +000024static char api_version_warning[] =
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000025"Python C API version mismatch for module %.100s:\
26 This Python has API version %d, module %.100s has version %d.";
Guido van Rossum970a0a21995-01-09 17:47:20 +000027
Guido van Rossum79f25d91997-04-29 20:08:16 +000028PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000029Py_InitModule4(const char *name, PyMethodDef *methods, const char *doc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030 PyObject *passthrough, int module_api_version)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000032 PyObject *m, *d, *v, *n;
Guido van Rossum79f25d91997-04-29 20:08:16 +000033 PyMethodDef *ml;
Guido van Rossum413407f2000-08-04 14:00:14 +000034 if (!Py_IsInitialized())
35 Py_FatalError("Interpreter not initialized (version mismatch?)");
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000036 if (module_api_version != PYTHON_API_VERSION) {
37 char message[512];
38 PyOS_snprintf(message, sizeof(message),
39 api_version_warning, name,
40 PYTHON_API_VERSION, name,
41 module_api_version);
42 if (PyErr_Warn(PyExc_RuntimeWarning, message))
43 return NULL;
44 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000045 /* Make sure name is fully qualified.
46
47 This is a bit of a hack: when the shared library is loaded,
48 the module name is "package.module", but the module calls
49 Py_InitModule*() with just "module" for the name. The shared
50 library loader squirrels away the true name of the module in
51 _Py_PackageContext, and Py_InitModule*() will substitute this
52 (if the name actually matches).
53 */
Guido van Rossum2e58ff31997-11-19 18:53:33 +000054 if (_Py_PackageContext != NULL) {
55 char *p = strrchr(_Py_PackageContext, '.');
56 if (p != NULL && strcmp(name, p+1) == 0) {
57 name = _Py_PackageContext;
58 _Py_PackageContext = NULL;
59 }
60 }
Guido van Rossum40b33c61997-08-02 03:07:46 +000061 if ((m = PyImport_AddModule(name)) == NULL)
62 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000063 d = PyModule_GetDict(m);
Fred Drake233cc592002-08-14 20:57:56 +000064 if (methods != NULL) {
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000065 n = PyString_FromString(name);
66 if (n == NULL)
67 return NULL;
Fred Drake233cc592002-08-14 20:57:56 +000068 for (ml = methods; ml->ml_name != NULL; ml++) {
69 if ((ml->ml_flags & METH_CLASS) ||
70 (ml->ml_flags & METH_STATIC)) {
71 PyErr_SetString(PyExc_ValueError,
72 "module functions cannot set"
73 " METH_CLASS or METH_STATIC");
Martin v. Löwis0b300be2006-03-01 21:33:54 +000074 Py_DECREF(n);
Fred Drake233cc592002-08-14 20:57:56 +000075 return NULL;
76 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000077 v = PyCFunction_NewEx(ml, passthrough, n);
Martin v. Löwis0b300be2006-03-01 21:33:54 +000078 if (v == NULL) {
79 Py_DECREF(n);
Fred Drake233cc592002-08-14 20:57:56 +000080 return NULL;
Martin v. Löwis0b300be2006-03-01 21:33:54 +000081 }
Fred Drake233cc592002-08-14 20:57:56 +000082 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
83 Py_DECREF(v);
Martin v. Löwis0b300be2006-03-01 21:33:54 +000084 Py_DECREF(n);
Fred Drake233cc592002-08-14 20:57:56 +000085 return NULL;
86 }
Fred Drake289898c2001-08-04 03:11:25 +000087 Py_DECREF(v);
Fred Drake289898c2001-08-04 03:11:25 +000088 }
Neal Norwitz7bcabc62005-11-20 23:58:38 +000089 Py_DECREF(n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 }
Guido van Rossum50620fa1995-01-07 12:43:18 +000091 if (doc != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +000092 v = PyString_FromString(doc);
Fred Drake289898c2001-08-04 03:11:25 +000093 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
Just van Rossumbbfd8592002-12-15 13:45:32 +000094 Py_XDECREF(v);
Guido van Rossum40b33c61997-08-02 03:07:46 +000095 return NULL;
Fred Drake289898c2001-08-04 03:11:25 +000096 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000097 Py_DECREF(v);
Guido van Rossum50620fa1995-01-07 12:43:18 +000098 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000099 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
102
Guido van Rossumbf80e541993-02-08 15:49:17 +0000103/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000104
Fred Drakeceead6d2003-01-30 15:08:25 +0000105static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000106countformat(const char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000107{
108 int count = 0;
109 int level = 0;
110 while (level > 0 || *format != endchar) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000111 switch (*format) {
112 case '\0':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000113 /* Premature end */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000114 PyErr_SetString(PyExc_SystemError,
115 "unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000116 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000117 case '(':
118 case '[':
119 case '{':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000120 if (level == 0)
121 count++;
122 level++;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000123 break;
124 case ')':
125 case ']':
126 case '}':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000127 level--;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000128 break;
129 case '#':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000130 case '&':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000131 case ',':
132 case ':':
133 case ' ':
134 case '\t':
135 break;
136 default:
137 if (level == 0)
138 count++;
139 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000140 format++;
141 }
142 return count;
143}
144
145
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000146/* Generic function to create a value -- the inverse of getargs() */
147/* After an original idea and first implementation by Steven Miale */
148
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000149static PyObject *do_mktuple(const char**, va_list *, int, int);
150static PyObject *do_mklist(const char**, va_list *, int, int);
151static PyObject *do_mkdict(const char**, va_list *, int, int);
152static PyObject *do_mkvalue(const char**, va_list *);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000153
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000154
Guido van Rossum79f25d91997-04-29 20:08:16 +0000155static PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000156do_mkdict(const char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000157{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000159 int i;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000160 int itemfailed = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000161 if (n < 0)
162 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 if ((d = PyDict_New()) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000164 return NULL;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000165 /* Note that we can't bail immediately on error as this will leak
166 refcounts on any 'N' arguments. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000167 for (i = 0; i < n; i+= 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 PyObject *k, *v;
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000169 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000170 k = do_mkvalue(p_format, p_va);
171 if (k == NULL) {
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000172 itemfailed = 1;
173 Py_INCREF(Py_None);
174 k = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000175 }
176 v = do_mkvalue(p_format, p_va);
177 if (v == NULL) {
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000178 itemfailed = 1;
179 Py_INCREF(Py_None);
180 v = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000181 }
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000182 err = PyDict_SetItem(d, k, v);
183 Py_DECREF(k);
184 Py_DECREF(v);
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000185 if (err < 0 || itemfailed) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000186 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000187 return NULL;
188 }
189 }
190 if (d != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000192 d = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000193 PyErr_SetString(PyExc_SystemError,
194 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000195 }
196 else if (endchar)
197 ++*p_format;
198 return d;
199}
200
Guido van Rossum79f25d91997-04-29 20:08:16 +0000201static PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000202do_mklist(const char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000203{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000205 int i;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000206 int itemfailed = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000207 if (n < 0)
208 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000209 if ((v = PyList_New(n)) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000210 return NULL;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000211 /* Note that we can't bail immediately on error as this will leak
212 refcounts on any 'N' arguments. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000213 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000215 if (w == NULL) {
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000216 itemfailed = 1;
217 Py_INCREF(Py_None);
218 w = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000219 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 PyList_SetItem(v, i, w);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000221 }
222 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000223 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000224 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000225 PyErr_SetString(PyExc_SystemError,
226 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000227 }
228 else if (endchar)
229 ++*p_format;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000230 if (itemfailed) {
231 Py_DECREF(v);
232 v = NULL;
233 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000234 return v;
235}
236
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000237#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000238static int
239_ustrlen(Py_UNICODE *u)
240{
241 int i = 0;
242 Py_UNICODE *v = u;
243 while (*v != 0) { i++; v++; }
244 return i;
245}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000246#endif
Fred Drake25d34472000-04-28 14:42:37 +0000247
Guido van Rossum79f25d91997-04-29 20:08:16 +0000248static PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000249do_mktuple(const char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000250{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 PyObject *v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000252 int i;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000253 int itemfailed = 0;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000254 if (n < 0)
255 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256 if ((v = PyTuple_New(n)) == NULL)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000257 return NULL;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000258 /* Note that we can't bail immediately on error as this will leak
259 refcounts on any 'N' arguments. */
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000260 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000261 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000262 if (w == NULL) {
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000263 itemfailed = 1;
264 Py_INCREF(Py_None);
265 w = Py_None;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000266 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 PyTuple_SetItem(v, i, w);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000268 }
269 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 Py_DECREF(v);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000271 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000272 PyErr_SetString(PyExc_SystemError,
273 "Unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000274 }
275 else if (endchar)
276 ++*p_format;
Michael W. Hudsonc849e632004-07-14 11:28:06 +0000277 if (itemfailed) {
278 Py_DECREF(v);
279 v = NULL;
280 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000281 return v;
282}
283
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284static PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000285do_mkvalue(const char **p_format, va_list *p_va)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000286{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000287 for (;;) {
288 switch (*(*p_format)++) {
289 case '(':
290 return do_mktuple(p_format, p_va, ')',
291 countformat(*p_format, ')'));
292
293 case '[':
294 return do_mklist(p_format, p_va, ']',
295 countformat(*p_format, ']'));
296
297 case '{':
298 return do_mkdict(p_format, p_va, '}',
299 countformat(*p_format, '}'));
300
301 case 'b':
Jack Jansenb763b9d2000-09-15 12:51:01 +0000302 case 'B':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000303 case 'h':
304 case 'i':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000305 return PyInt_FromLong((long)va_arg(*p_va, int));
Jack Jansend50338f2000-07-06 12:22:00 +0000306
307 case 'H':
308 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000309
Georg Brandlf06e30a2005-11-24 15:37:42 +0000310 case 'I':
311 {
312 unsigned int n;
313 n = va_arg(*p_va, unsigned int);
Tim Peters35c3f4f2005-12-24 06:23:41 +0000314 if (n > (unsigned long)PyInt_GetMax())
Georg Brandlf06e30a2005-11-24 15:37:42 +0000315 return PyLong_FromUnsignedLong((unsigned long)n);
316 else
317 return PyInt_FromLong(n);
318 }
319
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320 case 'n':
321#if SIZEOF_SIZE_T!=SIZEOF_LONG
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000322 return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000323#endif
324 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000325 case 'l':
Georg Brandlf06e30a2005-11-24 15:37:42 +0000326 return PyInt_FromLong(va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000327
Jack Jansendbd65032003-04-17 22:01:10 +0000328 case 'k':
Georg Brandlf06e30a2005-11-24 15:37:42 +0000329 {
330 unsigned long n;
331 n = va_arg(*p_va, unsigned long);
Tim Peters35c3f4f2005-12-24 06:23:41 +0000332 if (n > (unsigned long)PyInt_GetMax())
Georg Brandlf06e30a2005-11-24 15:37:42 +0000333 return PyLong_FromUnsignedLong(n);
334 else
335 return PyInt_FromLong(n);
336 }
Jack Jansendbd65032003-04-17 22:01:10 +0000337
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000338#ifdef HAVE_LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000339 case 'L':
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000340 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000341
342 case 'K':
Georg Brandlf06e30a2005-11-24 15:37:42 +0000343 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000344#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000345#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000346 case 'u':
347 {
348 PyObject *v;
349 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
350 int n;
351 if (**p_format == '#') {
352 ++*p_format;
353 n = va_arg(*p_va, int);
354 }
355 else
356 n = -1;
357 if (u == NULL) {
358 v = Py_None;
359 Py_INCREF(v);
360 }
361 else {
362 if (n < 0)
363 n = _ustrlen(u);
364 v = PyUnicode_FromUnicode(u, n);
365 }
366 return v;
367 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000368#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000369 case 'f':
370 case 'd':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000371 return PyFloat_FromDouble(
372 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000373
Fred Drakeaec79242001-03-12 21:03:26 +0000374#ifndef WITHOUT_COMPLEX
375 case 'D':
376 return PyComplex_FromCComplex(
377 *((Py_complex *)va_arg(*p_va, Py_complex *)));
378#endif /* WITHOUT_COMPLEX */
379
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000380 case 'c':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000381 {
382 char p[1];
Martin v. Löwis18e16552006-02-15 17:27:45 +0000383 p[0] = (char)va_arg(*p_va, int);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384 return PyString_FromStringAndSize(p, 1);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000385 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000386
387 case 's':
388 case 'z':
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000389 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390 PyObject *v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000391 char *str = va_arg(*p_va, char *);
392 int n;
393 if (**p_format == '#') {
394 ++*p_format;
395 n = va_arg(*p_va, int);
396 }
397 else
398 n = -1;
399 if (str == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000400 v = Py_None;
401 Py_INCREF(v);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000402 }
403 else {
Guido van Rossum582acec2000-06-28 22:07:35 +0000404 if (n < 0) {
405 size_t m = strlen(str);
406 if (m > INT_MAX) {
407 PyErr_SetString(PyExc_OverflowError,
408 "string too long for Python string");
409 return NULL;
410 }
411 n = (int)m;
412 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000413 v = PyString_FromStringAndSize(str, n);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000414 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000415 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000416 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417
Guido van Rossumd3415001998-12-23 05:01:38 +0000418 case 'N':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000419 case 'S':
420 case 'O':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000421 if (**p_format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000422 typedef PyObject *(*converter)(void *);
Guido van Rossumd1b93931995-01-20 16:56:02 +0000423 converter func = va_arg(*p_va, converter);
424 void *arg = va_arg(*p_va, void *);
425 ++*p_format;
426 return (*func)(arg);
427 }
428 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429 PyObject *v;
430 v = va_arg(*p_va, PyObject *);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000431 if (v != NULL) {
Guido van Rossumd3415001998-12-23 05:01:38 +0000432 if (*(*p_format - 1) != 'N')
433 Py_INCREF(v);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000434 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000435 else if (!PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000436 /* If a NULL was passed
437 * because a call that should
438 * have constructed a value
439 * failed, that's OK, and we
440 * pass the error on; but if
441 * no error occurred it's not
442 * clear that the caller knew
443 * what she was doing. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000444 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000445 "NULL object passed to Py_BuildValue");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000446 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000447 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000448
449 case ':':
450 case ',':
451 case ' ':
452 case '\t':
453 break;
454
455 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000457 "bad format char passed to Py_BuildValue");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000458 return NULL;
459
460 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000461 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000462}
463
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464
Fred Drakeceead6d2003-01-30 15:08:25 +0000465PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000466Py_BuildValue(const char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000467{
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000468 va_list va;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000469 PyObject* retval;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000470 va_start(va, format);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000471 retval = Py_VaBuildValue(format, va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000472 va_end(va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000473 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000474}
Guido van Rossume5372401993-03-16 12:15:04 +0000475
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000477Py_VaBuildValue(const char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000478{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000479 const char *f = format;
Guido van Rossume5372401993-03-16 12:15:04 +0000480 int n = countformat(f, '\0');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481 va_list lva;
482
483#ifdef VA_LIST_IS_ARRAY
484 memcpy(lva, va, sizeof(va_list));
485#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000486#ifdef __va_copy
487 __va_copy(lva, va);
488#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489 lva = va;
490#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000491#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000492
Guido van Rossume5372401993-03-16 12:15:04 +0000493 if (n < 0)
494 return NULL;
495 if (n == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000496 Py_INCREF(Py_None);
497 return Py_None;
Guido van Rossume5372401993-03-16 12:15:04 +0000498 }
499 if (n == 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000500 return do_mkvalue(&f, &lva);
501 return do_mktuple(&f, &lva, '\0', n);
502}
503
504
Guido van Rossum79f25d91997-04-29 20:08:16 +0000505PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000506PyEval_CallFunction(PyObject *obj, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000507{
508 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000509 PyObject *args;
510 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000511
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000512 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000513
Guido van Rossum79f25d91997-04-29 20:08:16 +0000514 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000515 va_end(vargs);
516
517 if (args == NULL)
518 return NULL;
519
Guido van Rossum79f25d91997-04-29 20:08:16 +0000520 res = PyEval_CallObject(obj, args);
521 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000522
523 return res;
524}
525
526
Guido van Rossum79f25d91997-04-29 20:08:16 +0000527PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000528PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529{
530 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000531 PyObject *meth;
532 PyObject *args;
533 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534
Guido van Rossumc4099e61998-08-08 20:51:26 +0000535 meth = PyObject_GetAttrString(obj, methodname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536 if (meth == NULL)
537 return NULL;
538
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542 va_end(vargs);
543
544 if (args == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 Py_DECREF(meth);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546 return NULL;
547 }
548
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 res = PyEval_CallObject(meth, args);
550 Py_DECREF(meth);
551 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552
553 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000554}
Fred Drake9e285152000-09-23 03:24:27 +0000555
556int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000557PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
Fred Drake9e285152000-09-23 03:24:27 +0000558{
559 PyObject *dict;
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000560 if (!PyModule_Check(m)) {
Fred Drake83115182002-06-17 17:16:57 +0000561 PyErr_SetString(PyExc_TypeError,
562 "PyModule_AddObject() needs module as first arg");
Fred Drake9e285152000-09-23 03:24:27 +0000563 return -1;
Fred Drake83115182002-06-17 17:16:57 +0000564 }
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000565 if (!o) {
Martin v. Löwisf8d59d22003-10-24 20:05:32 +0000566 if (!PyErr_Occurred())
567 PyErr_SetString(PyExc_TypeError,
568 "PyModule_AddObject() needs non-NULL value");
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000569 return -1;
570 }
571
Fred Drake83115182002-06-17 17:16:57 +0000572 dict = PyModule_GetDict(m);
573 if (dict == NULL) {
574 /* Internal error -- modules must have a dict! */
575 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
576 PyModule_GetName(m));
577 return -1;
578 }
579 if (PyDict_SetItemString(dict, name, o))
580 return -1;
581 Py_DECREF(o);
582 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000583}
584
585int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000586PyModule_AddIntConstant(PyObject *m, const char *name, long value)
Fred Drake9e285152000-09-23 03:24:27 +0000587{
588 return PyModule_AddObject(m, name, PyInt_FromLong(value));
589}
590
591int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000592PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
Fred Drake9e285152000-09-23 03:24:27 +0000593{
594 return PyModule_AddObject(m, name, PyString_FromString(value));
595}