This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Python / modsupport.c
blob0450a8a1c08481866d540b76d4e19d80900fbb7b
2 /* Module support implementation */
4 #include "Python.h"
6 #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
7 typedef extended va_double;
8 #else
9 typedef double va_double;
10 #endif
12 /* Package context -- the full module name for package imports */
13 char *_Py_PackageContext = NULL;
15 /* Py_InitModule4() parameters:
16 - 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
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.
28 static char api_version_warning[] =
29 "Python C API version mismatch for module %.100s:\
30 This Python has API version %d, module %.100s has version %d.";
32 PyObject *
33 Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
34 PyObject *passthrough, int module_api_version)
36 PyObject *m, *d, *v;
37 PyMethodDef *ml;
38 if (!Py_IsInitialized())
39 Py_FatalError("Interpreter not initialized (version mismatch?)");
40 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;
49 if (_Py_PackageContext != NULL) {
50 char *p = strrchr(_Py_PackageContext, '.');
51 if (p != NULL && strcmp(name, p+1) == 0) {
52 name = _Py_PackageContext;
53 _Py_PackageContext = NULL;
56 if ((m = PyImport_AddModule(name)) == NULL)
57 return NULL;
58 d = PyModule_GetDict(m);
59 for (ml = methods; ml->ml_name != NULL; ml++) {
60 v = PyCFunction_New(ml, passthrough);
61 if (v == NULL)
62 return NULL;
63 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
64 Py_DECREF(v);
65 return NULL;
67 Py_DECREF(v);
69 if (doc != NULL) {
70 v = PyString_FromString(doc);
71 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
72 Py_DECREF(v);
73 return NULL;
75 Py_DECREF(v);
77 return m;
81 /* Helper for mkvalue() to scan the length of a format */
83 static int countformat(char *format, int endchar)
85 int count = 0;
86 int level = 0;
87 while (level > 0 || *format != endchar) {
88 switch (*format) {
89 case '\0':
90 /* Premature end */
91 PyErr_SetString(PyExc_SystemError,
92 "unmatched paren in format");
93 return -1;
94 case '(':
95 case '[':
96 case '{':
97 if (level == 0)
98 count++;
99 level++;
100 break;
101 case ')':
102 case ']':
103 case '}':
104 level--;
105 break;
106 case '#':
107 case '&':
108 case ',':
109 case ':':
110 case ' ':
111 case '\t':
112 break;
113 default:
114 if (level == 0)
115 count++;
117 format++;
119 return count;
123 /* Generic function to create a value -- the inverse of getargs() */
124 /* After an original idea and first implementation by Steven Miale */
126 static PyObject *do_mktuple(char**, va_list *, int, int);
127 static PyObject *do_mklist(char**, va_list *, int, int);
128 static PyObject *do_mkdict(char**, va_list *, int, int);
129 static PyObject *do_mkvalue(char**, va_list *);
132 static PyObject *
133 do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
135 PyObject *d;
136 int i;
137 if (n < 0)
138 return NULL;
139 if ((d = PyDict_New()) == NULL)
140 return NULL;
141 for (i = 0; i < n; i+= 2) {
142 PyObject *k, *v;
143 int err;
144 k = do_mkvalue(p_format, p_va);
145 if (k == NULL) {
146 Py_DECREF(d);
147 return NULL;
149 v = do_mkvalue(p_format, p_va);
150 if (v == NULL) {
151 Py_DECREF(k);
152 Py_DECREF(d);
153 return NULL;
155 err = PyDict_SetItem(d, k, v);
156 Py_DECREF(k);
157 Py_DECREF(v);
158 if (err < 0) {
159 Py_DECREF(d);
160 return NULL;
163 if (d != NULL && **p_format != endchar) {
164 Py_DECREF(d);
165 d = NULL;
166 PyErr_SetString(PyExc_SystemError,
167 "Unmatched paren in format");
169 else if (endchar)
170 ++*p_format;
171 return d;
174 static PyObject *
175 do_mklist(char **p_format, va_list *p_va, int endchar, int n)
177 PyObject *v;
178 int i;
179 if (n < 0)
180 return NULL;
181 if ((v = PyList_New(n)) == NULL)
182 return NULL;
183 for (i = 0; i < n; i++) {
184 PyObject *w = do_mkvalue(p_format, p_va);
185 if (w == NULL) {
186 Py_DECREF(v);
187 return NULL;
189 PyList_SetItem(v, i, w);
191 if (v != NULL && **p_format != endchar) {
192 Py_DECREF(v);
193 v = NULL;
194 PyErr_SetString(PyExc_SystemError,
195 "Unmatched paren in format");
197 else if (endchar)
198 ++*p_format;
199 return v;
202 #ifdef Py_USING_UNICODE
203 static int
204 _ustrlen(Py_UNICODE *u)
206 int i = 0;
207 Py_UNICODE *v = u;
208 while (*v != 0) { i++; v++; }
209 return i;
211 #endif
213 static PyObject *
214 do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
216 PyObject *v;
217 int i;
218 if (n < 0)
219 return NULL;
220 if ((v = PyTuple_New(n)) == NULL)
221 return NULL;
222 for (i = 0; i < n; i++) {
223 PyObject *w = do_mkvalue(p_format, p_va);
224 if (w == NULL) {
225 Py_DECREF(v);
226 return NULL;
228 PyTuple_SetItem(v, i, w);
230 if (v != NULL && **p_format != endchar) {
231 Py_DECREF(v);
232 v = NULL;
233 PyErr_SetString(PyExc_SystemError,
234 "Unmatched paren in format");
236 else if (endchar)
237 ++*p_format;
238 return v;
241 static PyObject *
242 do_mkvalue(char **p_format, va_list *p_va)
244 for (;;) {
245 switch (*(*p_format)++) {
246 case '(':
247 return do_mktuple(p_format, p_va, ')',
248 countformat(*p_format, ')'));
250 case '[':
251 return do_mklist(p_format, p_va, ']',
252 countformat(*p_format, ']'));
254 case '{':
255 return do_mkdict(p_format, p_va, '}',
256 countformat(*p_format, '}'));
258 case 'b':
259 case 'B':
260 case 'h':
261 case 'i':
262 return PyInt_FromLong((long)va_arg(*p_va, int));
264 case 'H':
265 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
267 case 'l':
268 return PyInt_FromLong((long)va_arg(*p_va, long));
270 #ifdef HAVE_LONG_LONG
271 case 'L':
272 return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
273 #endif
274 #ifdef Py_USING_UNICODE
275 case 'u':
277 PyObject *v;
278 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
279 int n;
280 if (**p_format == '#') {
281 ++*p_format;
282 n = va_arg(*p_va, int);
284 else
285 n = -1;
286 if (u == NULL) {
287 v = Py_None;
288 Py_INCREF(v);
290 else {
291 if (n < 0)
292 n = _ustrlen(u);
293 v = PyUnicode_FromUnicode(u, n);
295 return v;
297 #endif
298 case 'f':
299 case 'd':
300 return PyFloat_FromDouble(
301 (double)va_arg(*p_va, va_double));
303 #ifndef WITHOUT_COMPLEX
304 case 'D':
305 return PyComplex_FromCComplex(
306 *((Py_complex *)va_arg(*p_va, Py_complex *)));
307 #endif /* WITHOUT_COMPLEX */
309 case 'c':
311 char p[1];
312 p[0] = va_arg(*p_va, int);
313 return PyString_FromStringAndSize(p, 1);
316 case 's':
317 case 'z':
319 PyObject *v;
320 char *str = va_arg(*p_va, char *);
321 int n;
322 if (**p_format == '#') {
323 ++*p_format;
324 n = va_arg(*p_va, int);
326 else
327 n = -1;
328 if (str == NULL) {
329 v = Py_None;
330 Py_INCREF(v);
332 else {
333 if (n < 0) {
334 size_t m = strlen(str);
335 if (m > INT_MAX) {
336 PyErr_SetString(PyExc_OverflowError,
337 "string too long for Python string");
338 return NULL;
340 n = (int)m;
342 v = PyString_FromStringAndSize(str, n);
344 return v;
347 case 'N':
348 case 'S':
349 case 'O':
350 if (**p_format == '&') {
351 typedef PyObject *(*converter)(void *);
352 converter func = va_arg(*p_va, converter);
353 void *arg = va_arg(*p_va, void *);
354 ++*p_format;
355 return (*func)(arg);
357 else {
358 PyObject *v;
359 v = va_arg(*p_va, PyObject *);
360 if (v != NULL) {
361 if (*(*p_format - 1) != 'N')
362 Py_INCREF(v);
364 else if (!PyErr_Occurred())
365 /* If a NULL was passed
366 * because a call that should
367 * have constructed a value
368 * failed, that's OK, and we
369 * pass the error on; but if
370 * no error occurred it's not
371 * clear that the caller knew
372 * what she was doing. */
373 PyErr_SetString(PyExc_SystemError,
374 "NULL object passed to Py_BuildValue");
375 return v;
378 case ':':
379 case ',':
380 case ' ':
381 case '\t':
382 break;
384 default:
385 PyErr_SetString(PyExc_SystemError,
386 "bad format char passed to Py_BuildValue");
387 return NULL;
394 PyObject *Py_BuildValue(char *format, ...)
396 va_list va;
397 PyObject* retval;
398 va_start(va, format);
399 retval = Py_VaBuildValue(format, va);
400 va_end(va);
401 return retval;
404 PyObject *
405 Py_VaBuildValue(char *format, va_list va)
407 char *f = format;
408 int n = countformat(f, '\0');
409 va_list lva;
411 #ifdef VA_LIST_IS_ARRAY
412 memcpy(lva, va, sizeof(va_list));
413 #else
414 lva = va;
415 #endif
417 if (n < 0)
418 return NULL;
419 if (n == 0) {
420 Py_INCREF(Py_None);
421 return Py_None;
423 if (n == 1)
424 return do_mkvalue(&f, &lva);
425 return do_mktuple(&f, &lva, '\0', n);
429 PyObject *
430 PyEval_CallFunction(PyObject *obj, char *format, ...)
432 va_list vargs;
433 PyObject *args;
434 PyObject *res;
436 va_start(vargs, format);
438 args = Py_VaBuildValue(format, vargs);
439 va_end(vargs);
441 if (args == NULL)
442 return NULL;
444 res = PyEval_CallObject(obj, args);
445 Py_DECREF(args);
447 return res;
451 PyObject *
452 PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
454 va_list vargs;
455 PyObject *meth;
456 PyObject *args;
457 PyObject *res;
459 meth = PyObject_GetAttrString(obj, methodname);
460 if (meth == NULL)
461 return NULL;
463 va_start(vargs, format);
465 args = Py_VaBuildValue(format, vargs);
466 va_end(vargs);
468 if (args == NULL) {
469 Py_DECREF(meth);
470 return NULL;
473 res = PyEval_CallObject(meth, args);
474 Py_DECREF(meth);
475 Py_DECREF(args);
477 return res;
481 PyModule_AddObject(PyObject *m, char *name, PyObject *o)
483 PyObject *dict;
484 if (!PyModule_Check(m) || o == NULL)
485 return -1;
486 dict = PyModule_GetDict(m);
487 if (dict == NULL)
488 return -1;
489 if (PyDict_SetItemString(dict, name, o))
490 return -1;
491 Py_DECREF(o);
492 return 0;
495 int
496 PyModule_AddIntConstant(PyObject *m, char *name, long value)
498 return PyModule_AddObject(m, name, PyInt_FromLong(value));
501 int
502 PyModule_AddStringConstant(PyObject *m, char *name, char *value)
504 return PyModule_AddObject(m, name, PyString_FromString(value));