C extension argument parsing

Hi folks,

I am writing an extension which will be called from python using C-style printf variable arguments.

For example:

import the_extension
something="string"
number=10
the_extension.doit("foo %s:%d\n", something, number)

Looking at the documentation on argument parsing I don’t see a way to handle this style of arguments. It would seem I would need to check that the argument list is a tuple and then manually iterate over the arguments. Can anyone suggest a better way?

Hello! I think whether there are “better ways” depends on what you want to implement in doit(). If it should support a full format-string minilanguage with its own semantics not elsewhere found in the API functions, you probably have to write your own format string parser to tackle the first argument, and consume the remaining arguments one by one. But if all you want is to construct a Python Unicode string and operate on it in the implementation of doit() in simpler ways, you can use PyUnicode_Format(), but the format specifiers it accepts are Python style, not exactly printf-style. But in that case you can just make doit() accept a Python string and let Python do the formatting?