My script reads data from CSV-files and passes the rows to C code, which uses PyArg_ParseTuple() and/or PyArg_ParseTupleAndKeywords() to parse the arguments and then work on them.
Some of the arguments are supposed to be strings, others -- numbers (integers and floating point). The expected types are passed to the PyArg-functions:
if (!PyArg_ParseTupleAndKeywords(args, kw, "sdddd", keywords,
&name, &height, &width, &depth, &weight))
return NULL;
Trouble is, all of the fields parsed from CSV are strings. When passed to PyArg_ParseTupleAndKeywords this triggers a type error because "11" is not a number, even if 11 is...
So I'm explicitly casting some fields to numbers, but that looks ugly and duplicates the knowledge already expressed in the format string.
Is there a way to tell the PyArg-functions to try to convert the objects into numbers, when necessary -- when the specified argument is supposed to be a number -- and only fail the parsing, if the conversion fails?
Ocode for the parse function, but that's it.