Catch situations where currentframe() returns None. See SF patch #1447410, this is...
[python.git] / Python / modsupport.c
blobcb6bdfd2853fd71380d613955c7dcd3f1ab7e0d6
2 /* Module support implementation */
4 #include "Python.h"
6 typedef double va_double;
8 /* Package context -- the full module name for package imports */
9 char *_Py_PackageContext = NULL;
11 /* Py_InitModule4() parameters:
12 - 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
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.
24 static char api_version_warning[] =
25 "Python C API version mismatch for module %.100s:\
26 This Python has API version %d, module %.100s has version %d.";
28 PyObject *
29 Py_InitModule4(const char *name, PyMethodDef *methods, const char *doc,
30 PyObject *passthrough, int module_api_version)
32 PyObject *m, *d, *v, *n;
33 PyMethodDef *ml;
34 if (!Py_IsInitialized())
35 Py_FatalError("Interpreter not initialized (version mismatch?)");
36 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;
45 /* Make sure name is fully qualified.
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).
54 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;
61 if ((m = PyImport_AddModule(name)) == NULL)
62 return NULL;
63 d = PyModule_GetDict(m);
64 if (methods != NULL) {
65 n = PyString_FromString(name);
66 if (n == NULL)
67 return NULL;
68 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");
74 Py_DECREF(n);
75 return NULL;
77 v = PyCFunction_NewEx(ml, passthrough, n);
78 if (v == NULL) {
79 Py_DECREF(n);
80 return NULL;
82 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
83 Py_DECREF(v);
84 Py_DECREF(n);
85 return NULL;
87 Py_DECREF(v);
89 Py_DECREF(n);
91 if (doc != NULL) {
92 v = PyString_FromString(doc);
93 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
94 Py_XDECREF(v);
95 return NULL;
97 Py_DECREF(v);
99 return m;
103 /* Helper for mkvalue() to scan the length of a format */
105 static int
106 countformat(const char *format, int endchar)
108 int count = 0;
109 int level = 0;
110 while (level > 0 || *format != endchar) {
111 switch (*format) {
112 case '\0':
113 /* Premature end */
114 PyErr_SetString(PyExc_SystemError,
115 "unmatched paren in format");
116 return -1;
117 case '(':
118 case '[':
119 case '{':
120 if (level == 0)
121 count++;
122 level++;
123 break;
124 case ')':
125 case ']':
126 case '}':
127 level--;
128 break;
129 case '#':
130 case '&':
131 case ',':
132 case ':':
133 case ' ':
134 case '\t':
135 break;
136 default:
137 if (level == 0)
138 count++;
140 format++;
142 return count;
146 /* Generic function to create a value -- the inverse of getargs() */
147 /* After an original idea and first implementation by Steven Miale */
149 static PyObject *do_mktuple(const char**, va_list *, int, int);
150 static PyObject *do_mklist(const char**, va_list *, int, int);
151 static PyObject *do_mkdict(const char**, va_list *, int, int);
152 static PyObject *do_mkvalue(const char**, va_list *);
155 static PyObject *
156 do_mkdict(const char **p_format, va_list *p_va, int endchar, int n)
158 PyObject *d;
159 int i;
160 int itemfailed = 0;
161 if (n < 0)
162 return NULL;
163 if ((d = PyDict_New()) == NULL)
164 return NULL;
165 /* Note that we can't bail immediately on error as this will leak
166 refcounts on any 'N' arguments. */
167 for (i = 0; i < n; i+= 2) {
168 PyObject *k, *v;
169 int err;
170 k = do_mkvalue(p_format, p_va);
171 if (k == NULL) {
172 itemfailed = 1;
173 Py_INCREF(Py_None);
174 k = Py_None;
176 v = do_mkvalue(p_format, p_va);
177 if (v == NULL) {
178 itemfailed = 1;
179 Py_INCREF(Py_None);
180 v = Py_None;
182 err = PyDict_SetItem(d, k, v);
183 Py_DECREF(k);
184 Py_DECREF(v);
185 if (err < 0 || itemfailed) {
186 Py_DECREF(d);
187 return NULL;
190 if (d != NULL && **p_format != endchar) {
191 Py_DECREF(d);
192 d = NULL;
193 PyErr_SetString(PyExc_SystemError,
194 "Unmatched paren in format");
196 else if (endchar)
197 ++*p_format;
198 return d;
201 static PyObject *
202 do_mklist(const char **p_format, va_list *p_va, int endchar, int n)
204 PyObject *v;
205 int i;
206 int itemfailed = 0;
207 if (n < 0)
208 return NULL;
209 v = PyList_New(n);
210 if (v == NULL)
211 return NULL;
212 /* Note that we can't bail immediately on error as this will leak
213 refcounts on any 'N' arguments. */
214 for (i = 0; i < n; i++) {
215 PyObject *w = do_mkvalue(p_format, p_va);
216 if (w == NULL) {
217 itemfailed = 1;
218 Py_INCREF(Py_None);
219 w = Py_None;
221 PyList_SetItem(v, i, w);
224 if (itemfailed) {
225 /* do_mkvalue() should have already set an error */
226 Py_DECREF(v);
227 return NULL;
229 if (**p_format != endchar) {
230 Py_DECREF(v);
231 PyErr_SetString(PyExc_SystemError,
232 "Unmatched paren in format");
233 return NULL;
236 if (endchar)
237 ++*p_format;
238 return v;
241 #ifdef Py_USING_UNICODE
242 static int
243 _ustrlen(Py_UNICODE *u)
245 int i = 0;
246 Py_UNICODE *v = u;
247 while (*v != 0) { i++; v++; }
248 return i;
250 #endif
252 static PyObject *
253 do_mktuple(const char **p_format, va_list *p_va, int endchar, int n)
255 PyObject *v;
256 int i;
257 int itemfailed = 0;
258 if (n < 0)
259 return NULL;
260 if ((v = PyTuple_New(n)) == NULL)
261 return NULL;
262 /* Note that we can't bail immediately on error as this will leak
263 refcounts on any 'N' arguments. */
264 for (i = 0; i < n; i++) {
265 PyObject *w = do_mkvalue(p_format, p_va);
266 if (w == NULL) {
267 itemfailed = 1;
268 Py_INCREF(Py_None);
269 w = Py_None;
271 PyTuple_SetItem(v, i, w);
273 if (v != NULL && **p_format != endchar) {
274 Py_DECREF(v);
275 v = NULL;
276 PyErr_SetString(PyExc_SystemError,
277 "Unmatched paren in format");
279 else if (endchar)
280 ++*p_format;
281 if (itemfailed) {
282 Py_DECREF(v);
283 v = NULL;
285 return v;
288 static PyObject *
289 do_mkvalue(const char **p_format, va_list *p_va)
291 for (;;) {
292 switch (*(*p_format)++) {
293 case '(':
294 return do_mktuple(p_format, p_va, ')',
295 countformat(*p_format, ')'));
297 case '[':
298 return do_mklist(p_format, p_va, ']',
299 countformat(*p_format, ']'));
301 case '{':
302 return do_mkdict(p_format, p_va, '}',
303 countformat(*p_format, '}'));
305 case 'b':
306 case 'B':
307 case 'h':
308 case 'i':
309 return PyInt_FromLong((long)va_arg(*p_va, int));
311 case 'H':
312 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
314 case 'I':
316 unsigned int n;
317 n = va_arg(*p_va, unsigned int);
318 if (n > (unsigned long)PyInt_GetMax())
319 return PyLong_FromUnsignedLong((unsigned long)n);
320 else
321 return PyInt_FromLong(n);
324 case 'n':
325 #if SIZEOF_SIZE_T!=SIZEOF_LONG
326 return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
327 #endif
328 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
329 case 'l':
330 return PyInt_FromLong(va_arg(*p_va, long));
332 case 'k':
334 unsigned long n;
335 n = va_arg(*p_va, unsigned long);
336 if (n > (unsigned long)PyInt_GetMax())
337 return PyLong_FromUnsignedLong(n);
338 else
339 return PyInt_FromLong(n);
342 #ifdef HAVE_LONG_LONG
343 case 'L':
344 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
346 case 'K':
347 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
348 #endif
349 #ifdef Py_USING_UNICODE
350 case 'u':
352 PyObject *v;
353 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
354 int n;
355 if (**p_format == '#') {
356 ++*p_format;
357 n = va_arg(*p_va, int);
359 else
360 n = -1;
361 if (u == NULL) {
362 v = Py_None;
363 Py_INCREF(v);
365 else {
366 if (n < 0)
367 n = _ustrlen(u);
368 v = PyUnicode_FromUnicode(u, n);
370 return v;
372 #endif
373 case 'f':
374 case 'd':
375 return PyFloat_FromDouble(
376 (double)va_arg(*p_va, va_double));
378 #ifndef WITHOUT_COMPLEX
379 case 'D':
380 return PyComplex_FromCComplex(
381 *((Py_complex *)va_arg(*p_va, Py_complex *)));
382 #endif /* WITHOUT_COMPLEX */
384 case 'c':
386 char p[1];
387 p[0] = (char)va_arg(*p_va, int);
388 return PyString_FromStringAndSize(p, 1);
391 case 's':
392 case 'z':
394 PyObject *v;
395 char *str = va_arg(*p_va, char *);
396 int n;
397 if (**p_format == '#') {
398 ++*p_format;
399 n = va_arg(*p_va, int);
401 else
402 n = -1;
403 if (str == NULL) {
404 v = Py_None;
405 Py_INCREF(v);
407 else {
408 if (n < 0) {
409 size_t m = strlen(str);
410 if (m > INT_MAX) {
411 PyErr_SetString(PyExc_OverflowError,
412 "string too long for Python string");
413 return NULL;
415 n = (int)m;
417 v = PyString_FromStringAndSize(str, n);
419 return v;
422 case 'N':
423 case 'S':
424 case 'O':
425 if (**p_format == '&') {
426 typedef PyObject *(*converter)(void *);
427 converter func = va_arg(*p_va, converter);
428 void *arg = va_arg(*p_va, void *);
429 ++*p_format;
430 return (*func)(arg);
432 else {
433 PyObject *v;
434 v = va_arg(*p_va, PyObject *);
435 if (v != NULL) {
436 if (*(*p_format - 1) != 'N')
437 Py_INCREF(v);
439 else if (!PyErr_Occurred())
440 /* If a NULL was passed
441 * because a call that should
442 * have constructed a value
443 * failed, that's OK, and we
444 * pass the error on; but if
445 * no error occurred it's not
446 * clear that the caller knew
447 * what she was doing. */
448 PyErr_SetString(PyExc_SystemError,
449 "NULL object passed to Py_BuildValue");
450 return v;
453 case ':':
454 case ',':
455 case ' ':
456 case '\t':
457 break;
459 default:
460 PyErr_SetString(PyExc_SystemError,
461 "bad format char passed to Py_BuildValue");
462 return NULL;
469 PyObject *
470 Py_BuildValue(const char *format, ...)
472 va_list va;
473 PyObject* retval;
474 va_start(va, format);
475 retval = Py_VaBuildValue(format, va);
476 va_end(va);
477 return retval;
480 PyObject *
481 Py_VaBuildValue(const char *format, va_list va)
483 const char *f = format;
484 int n = countformat(f, '\0');
485 va_list lva;
487 #ifdef VA_LIST_IS_ARRAY
488 memcpy(lva, va, sizeof(va_list));
489 #else
490 #ifdef __va_copy
491 __va_copy(lva, va);
492 #else
493 lva = va;
494 #endif
495 #endif
497 if (n < 0)
498 return NULL;
499 if (n == 0) {
500 Py_INCREF(Py_None);
501 return Py_None;
503 if (n == 1)
504 return do_mkvalue(&f, &lva);
505 return do_mktuple(&f, &lva, '\0', n);
509 PyObject *
510 PyEval_CallFunction(PyObject *obj, const char *format, ...)
512 va_list vargs;
513 PyObject *args;
514 PyObject *res;
516 va_start(vargs, format);
518 args = Py_VaBuildValue(format, vargs);
519 va_end(vargs);
521 if (args == NULL)
522 return NULL;
524 res = PyEval_CallObject(obj, args);
525 Py_DECREF(args);
527 return res;
531 PyObject *
532 PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
534 va_list vargs;
535 PyObject *meth;
536 PyObject *args;
537 PyObject *res;
539 meth = PyObject_GetAttrString(obj, methodname);
540 if (meth == NULL)
541 return NULL;
543 va_start(vargs, format);
545 args = Py_VaBuildValue(format, vargs);
546 va_end(vargs);
548 if (args == NULL) {
549 Py_DECREF(meth);
550 return NULL;
553 res = PyEval_CallObject(meth, args);
554 Py_DECREF(meth);
555 Py_DECREF(args);
557 return res;
561 PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
563 PyObject *dict;
564 if (!PyModule_Check(m)) {
565 PyErr_SetString(PyExc_TypeError,
566 "PyModule_AddObject() needs module as first arg");
567 return -1;
569 if (!o) {
570 if (!PyErr_Occurred())
571 PyErr_SetString(PyExc_TypeError,
572 "PyModule_AddObject() needs non-NULL value");
573 return -1;
576 dict = PyModule_GetDict(m);
577 if (dict == NULL) {
578 /* Internal error -- modules must have a dict! */
579 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
580 PyModule_GetName(m));
581 return -1;
583 if (PyDict_SetItemString(dict, name, o))
584 return -1;
585 Py_DECREF(o);
586 return 0;
589 int
590 PyModule_AddIntConstant(PyObject *m, const char *name, long value)
592 return PyModule_AddObject(m, name, PyInt_FromLong(value));
595 int
596 PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
598 return PyModule_AddObject(m, name, PyString_FromString(value));