Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37424

Re: Is PyArg_ParseTuple necessary to parse arguments?

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: Is PyArg_ParseTuple necessary to parse arguments?
Date 2013-01-23 08:50 +0100
References <a4d078cf-b670-46e2-9a52-dfb3d43a25a0@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.871.1358927435.2939.python-list@python.org> (permalink)

Show all headers | View raw


rahulgarg44@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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Is PyArg_ParseTuple necessary to parse arguments? rahulgarg44@gmail.com - 2013-01-22 09:20 -0800
  Re: Is PyArg_ParseTuple necessary to parse arguments? dieter <dieter@handshake.de> - 2013-01-23 08:00 +0100
  Re: Is PyArg_ParseTuple necessary to parse arguments? Stefan Behnel <stefan_ml@behnel.de> - 2013-01-23 08:50 +0100

csiph-web