Is PyArg_ParseTuple necessary to parse arguments?

Stefan Behnel stefan_ml at behnel.de
Wed Jan 23 02:50:23 EST 2013


rahulgarg44 at gmail.com, 22.01.2013 18:20:
> Or can I just loop through the argument tuple manually by using something like PyTuple_GET_ITEM(args,i), then putting manual code to convert the objects to appropriate C type?
> The use case is that I am interfacing Python with another interpreter and do not know the type and number of arguments till runtime :)

Just in case you're not aware of it, C extensions are quite commonly
written in Cython instead of C these days. I've used it for Lupa, for
example, which embeds the Lua(JIT) runtime in CPython. That should be quite
similar to what you're after. It certainly makes things a lot easier to write

  cdef class Wrapper:
      "Wraps an external object for usage in Python."

      cdef mylib.sometype* _wrapped_object  # pointer to external 'thing'

      def __call__(self, *args):
         mapped_args = malloc(len(args)*...)
         # ...
         for i, arg in enumerate(args):
             map_value_to_external(arg, mapped_args[i])
         # ...
         result = call_external(self._wrapped_object, mapped_args)
         return map_value_from_external(result)

than to do these things in plain C. If nothing else, it saves you from
having to put thoughts into reference counting and other common CPython
C-API pitfalls.

Stefan





More information about the Python-list mailing list