2 /* Module support implementation */
7 typedef double va_double
;
9 static PyObject
*va_build_value(const char *, va_list, int);
11 /* Package context -- the full module name for package imports */
12 char *_Py_PackageContext
= NULL
;
14 /* Helper for mkvalue() to scan the length of a format */
17 countformat(const char *format
, int endchar
)
21 while (level
> 0 || *format
!= endchar
) {
25 PyErr_SetString(PyExc_SystemError
,
26 "unmatched paren in format");
57 /* Generic function to create a value -- the inverse of getargs() */
58 /* After an original idea and first implementation by Steven Miale */
60 static PyObject
*do_mktuple(const char**, va_list *, int, int, int);
61 static PyObject
*do_mklist(const char**, va_list *, int, int, int);
62 static PyObject
*do_mkdict(const char**, va_list *, int, int, int);
63 static PyObject
*do_mkvalue(const char**, va_list *, int);
67 do_mkdict(const char **p_format
, va_list *p_va
, int endchar
, int n
, int flags
)
74 if ((d
= PyDict_New()) == NULL
)
76 /* Note that we can't bail immediately on error as this will leak
77 refcounts on any 'N' arguments. */
78 for (i
= 0; i
< n
; i
+= 2) {
81 k
= do_mkvalue(p_format
, p_va
, flags
);
87 v
= do_mkvalue(p_format
, p_va
, flags
);
93 err
= PyDict_SetItem(d
, k
, v
);
96 if (err
< 0 || itemfailed
) {
101 if (d
!= NULL
&& **p_format
!= endchar
) {
104 PyErr_SetString(PyExc_SystemError
,
105 "Unmatched paren in format");
113 do_mklist(const char **p_format
, va_list *p_va
, int endchar
, int n
, int flags
)
123 /* Note that we can't bail immediately on error as this will leak
124 refcounts on any 'N' arguments. */
125 for (i
= 0; i
< n
; i
++) {
126 PyObject
*w
= do_mkvalue(p_format
, p_va
, flags
);
132 PyList_SET_ITEM(v
, i
, w
);
136 /* do_mkvalue() should have already set an error */
140 if (**p_format
!= endchar
) {
142 PyErr_SetString(PyExc_SystemError
,
143 "Unmatched paren in format");
152 _ustrlen(Py_UNICODE
*u
)
156 while (*v
!= 0) { i
++; v
++; }
161 do_mktuple(const char **p_format
, va_list *p_va
, int endchar
, int n
, int flags
)
168 if ((v
= PyTuple_New(n
)) == NULL
)
170 /* Note that we can't bail immediately on error as this will leak
171 refcounts on any 'N' arguments. */
172 for (i
= 0; i
< n
; i
++) {
173 PyObject
*w
= do_mkvalue(p_format
, p_va
, flags
);
179 PyTuple_SET_ITEM(v
, i
, w
);
182 /* do_mkvalue() should have already set an error */
186 if (**p_format
!= endchar
) {
188 PyErr_SetString(PyExc_SystemError
,
189 "Unmatched paren in format");
198 do_mkvalue(const char **p_format
, va_list *p_va
, int flags
)
201 switch (*(*p_format
)++) {
203 return do_mktuple(p_format
, p_va
, ')',
204 countformat(*p_format
, ')'), flags
);
207 return do_mklist(p_format
, p_va
, ']',
208 countformat(*p_format
, ']'), flags
);
211 return do_mkdict(p_format
, p_va
, '}',
212 countformat(*p_format
, '}'), flags
);
218 return PyLong_FromLong((long)va_arg(*p_va
, int));
221 return PyLong_FromLong((long)va_arg(*p_va
, unsigned int));
226 n
= va_arg(*p_va
, unsigned int);
227 return PyLong_FromUnsignedLong(n
);
231 #if SIZEOF_SIZE_T!=SIZEOF_LONG
232 return PyLong_FromSsize_t(va_arg(*p_va
, Py_ssize_t
));
234 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
236 return PyLong_FromLong(va_arg(*p_va
, long));
241 n
= va_arg(*p_va
, unsigned long);
242 return PyLong_FromUnsignedLong(n
);
245 #ifdef HAVE_LONG_LONG
247 return PyLong_FromLongLong((PY_LONG_LONG
)va_arg(*p_va
, PY_LONG_LONG
));
250 return PyLong_FromUnsignedLongLong((PY_LONG_LONG
)va_arg(*p_va
, unsigned PY_LONG_LONG
));
255 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
257 if (**p_format
== '#') {
259 if (flags
& FLAG_SIZE_T
)
260 n
= va_arg(*p_va
, Py_ssize_t
);
262 n
= va_arg(*p_va
, int);
273 v
= PyUnicode_FromUnicode(u
, n
);
279 return PyFloat_FromDouble(
280 (double)va_arg(*p_va
, va_double
));
282 #ifndef WITHOUT_COMPLEX
284 return PyComplex_FromCComplex(
285 *((Py_complex
*)va_arg(*p_va
, Py_complex
*)));
286 #endif /* WITHOUT_COMPLEX */
291 p
[0] = (char)va_arg(*p_va
, int);
292 return PyBytes_FromStringAndSize(p
, 1);
296 int i
= va_arg(*p_va
, int);
297 if (i
< 0 || i
> PyUnicode_GetMax()) {
298 PyErr_SetString(PyExc_OverflowError
,
299 "%c arg not in range(0x110000)");
302 return PyUnicode_FromOrdinal(i
);
309 char *str
= va_arg(*p_va
, char *);
311 if (**p_format
== '#') {
313 if (flags
& FLAG_SIZE_T
)
314 n
= va_arg(*p_va
, Py_ssize_t
);
316 n
= va_arg(*p_va
, int);
326 size_t m
= strlen(str
);
327 if (m
> PY_SSIZE_T_MAX
) {
328 PyErr_SetString(PyExc_OverflowError
,
329 "string too long for Python string");
334 v
= PyUnicode_FromStringAndSize(str
, n
);
342 char *str
= va_arg(*p_va
, char *);
344 if (**p_format
== '#') {
346 if (flags
& FLAG_SIZE_T
)
347 n
= va_arg(*p_va
, Py_ssize_t
);
349 n
= va_arg(*p_va
, int);
359 size_t m
= strlen(str
);
360 if (m
> PY_SSIZE_T_MAX
) {
361 PyErr_SetString(PyExc_OverflowError
,
362 "string too long for Python string");
367 v
= PyUnicode_FromStringAndSize(str
, n
);
375 char *str
= va_arg(*p_va
, char *);
377 if (**p_format
== '#') {
379 if (flags
& FLAG_SIZE_T
)
380 n
= va_arg(*p_va
, Py_ssize_t
);
382 n
= va_arg(*p_va
, int);
392 size_t m
= strlen(str
);
393 if (m
> PY_SSIZE_T_MAX
) {
394 PyErr_SetString(PyExc_OverflowError
,
395 "string too long for Python bytes");
400 v
= PyBytes_FromStringAndSize(str
, n
);
408 if (**p_format
== '&') {
409 typedef PyObject
*(*converter
)(void *);
410 converter func
= va_arg(*p_va
, converter
);
411 void *arg
= va_arg(*p_va
, void *);
417 v
= va_arg(*p_va
, PyObject
*);
419 if (*(*p_format
- 1) != 'N')
422 else if (!PyErr_Occurred())
423 /* If a NULL was passed
424 * because a call that should
425 * have constructed a value
426 * failed, that's OK, and we
427 * pass the error on; but if
428 * no error occurred it's not
429 * clear that the caller knew
430 * what she was doing. */
431 PyErr_SetString(PyExc_SystemError
,
432 "NULL object passed to Py_BuildValue");
443 PyErr_SetString(PyExc_SystemError
,
444 "bad format char passed to Py_BuildValue");
453 Py_BuildValue(const char *format
, ...)
457 va_start(va
, format
);
458 retval
= va_build_value(format
, va
, 0);
464 _Py_BuildValue_SizeT(const char *format
, ...)
468 va_start(va
, format
);
469 retval
= va_build_value(format
, va
, FLAG_SIZE_T
);
475 Py_VaBuildValue(const char *format
, va_list va
)
477 return va_build_value(format
, va
, 0);
481 _Py_VaBuildValue_SizeT(const char *format
, va_list va
)
483 return va_build_value(format
, va
, FLAG_SIZE_T
);
487 va_build_value(const char *format
, va_list va
, int flags
)
489 const char *f
= format
;
490 int n
= countformat(f
, '\0');
493 #ifdef VA_LIST_IS_ARRAY
494 memcpy(lva
, va
, sizeof(va_list));
510 return do_mkvalue(&f
, &lva
, flags
);
511 return do_mktuple(&f
, &lva
, '\0', n
, flags
);
516 PyEval_CallFunction(PyObject
*obj
, const char *format
, ...)
522 va_start(vargs
, format
);
524 args
= Py_VaBuildValue(format
, vargs
);
530 res
= PyEval_CallObject(obj
, args
);
538 PyEval_CallMethod(PyObject
*obj
, const char *methodname
, const char *format
, ...)
545 meth
= PyObject_GetAttrString(obj
, methodname
);
549 va_start(vargs
, format
);
551 args
= Py_VaBuildValue(format
, vargs
);
559 res
= PyEval_CallObject(meth
, args
);
567 PyModule_AddObject(PyObject
*m
, const char *name
, PyObject
*o
)
570 if (!PyModule_Check(m
)) {
571 PyErr_SetString(PyExc_TypeError
,
572 "PyModule_AddObject() needs module as first arg");
576 if (!PyErr_Occurred())
577 PyErr_SetString(PyExc_TypeError
,
578 "PyModule_AddObject() needs non-NULL value");
582 dict
= PyModule_GetDict(m
);
584 /* Internal error -- modules must have a dict! */
585 PyErr_Format(PyExc_SystemError
, "module '%s' has no __dict__",
586 PyModule_GetName(m
));
589 if (PyDict_SetItemString(dict
, name
, o
))
596 PyModule_AddIntConstant(PyObject
*m
, const char *name
, long value
)
598 PyObject
*o
= PyLong_FromLong(value
);
601 if (PyModule_AddObject(m
, name
, o
) == 0)
608 PyModule_AddStringConstant(PyObject
*m
, const char *name
, const char *value
)
610 PyObject
*o
= PyUnicode_FromString(value
);
613 if (PyModule_AddObject(m
, name
, o
) == 0)