Is PyArg_ParseTuple necessary to parse arguments?

Discussion in 'Python' started by rahulgarg44@gmail.com, Jan 22, 2013.

  1. Guest

    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
    , Jan 22, 2013
    #1
    1. Advertising

  2. dieter Guest

    writes:

    > 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.
    dieter, Jan 23, 2013
    #2
    1. Advertising

  3. , 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
    Stefan Behnel, Jan 23, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Gross
    Replies:
    0
    Views:
    616
    Michael Gross
    Jul 30, 2003
  2. =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte

    Re: Python/C API - PyArg_ParseTuple problem ?

    =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte, Aug 22, 2003, in forum: Python
    Replies:
    1
    Views:
    392
    =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte
    Aug 22, 2003
  3. Skip Montanaro
    Replies:
    1
    Views:
    338
    Torsten Mohr
    Jun 7, 2004
  4. Patrick Stinson

    PyArg_ParseTuple

    Patrick Stinson, Jun 9, 2004, in forum: Python
    Replies:
    1
    Views:
    442
    Jeff Epler
    Jun 10, 2004
  5. Gabriel Genellina
    Replies:
    0
    Views:
    423
    Gabriel Genellina
    Sep 16, 2008
Loading...

Share This Page