Is PyArg_ParseTuple necessary to parse arguments?

R

rahulgarg44

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 :)

rahul
 
D

dieter

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?

If you like you can do the "parsing" yourself.
 
S

Stefan Behnel

(e-mail address removed), 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)
# ...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top