Next: Reference counts Up: Extending Python with Previous: Format strings for

The mkvalue() function

This function is the counterpart to getargs(). It is declared in `Include/modsupport.h' as follows:


    object *mkvalue(char *format, ...);

It supports exactly the same format letters as getargs(), but the arguments (which are input to the function, not output) must not be pointers, just values. If a byte, short or float is passed to a varargs function, it is widened by the compiler to int or double, so `b' and `h' are treated as `i' and `f' is treated as `d'. `S' is treated as `O', `s' is treated as `z'. `z#' and `s#' are supported: a second argument specifies the length of the data (negative means use strlen()). `S' and `O' add a reference to their argument (so you should DECREF() it if you've just created it and aren't going to use it again).

If the argument for `O' or `S' is a NULL pointer, it is assumed that this was caused because the call producing the argument found an error and set an exception. Therefore, mkvalue() will return NULL but won't set an exception if one is already set. If no exception is set, SystemError is set.

If there is an error in the format string, the SystemError exception is set, since it is the calling C code's fault, not that of the Python user who sees the exception.

Example:


    return mkvalue("(ii)", 0, 0);

returns a tuple containing two zeros. (Outer parentheses in the format string are actually superfluous, but you can use them for compatibility with getargs(), which requires them if more than one argument is expected.)


guido@cwi.nl