Python C/API - *arg,**kwds variable argumnents

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mdcb808@gmail.com

    #1

    Python C/API - *arg,**kwds variable argumnents

    I am writing a C extension with python 2.3.5 and need constructs
    similar to python
    func(*args, **kwds)
    What's a neat way to do that?
    I found pyrex has a __Pyx_GetStarAr gs -
    is there something I'm missing from the regular C/API maybe using one
    of the PyArg_Parse.. calls ?

    Thanks,
    M.

  • Raymond L. Buvel

    #2
    Re: Python C/API - *arg,**kwds variable argumnents

    mdcb808@gmail.c om wrote:[color=blue]
    > I am writing a C extension with python 2.3.5 and need constructs
    > similar to python
    > func(*args, **kwds)
    > What's a neat way to do that?
    > I found pyrex has a __Pyx_GetStarAr gs -
    > is there something I'm missing from the regular C/API maybe using one
    > of the PyArg_Parse.. calls ?
    >
    > Thanks,
    > M.
    >[/color]

    It looks like the PyArg_ParseTupl eAndKeywords API call is what you are
    looking for. Documentation is


    Comment

    • mdcb808@gmail.com

      #3
      Re: Python C/API - *arg,**kwds variable argumnents

      I saw that in 2.4, although it's not clear how I can make it work. the
      argument char *format and char *keywords[] are fixed there. Does anyone
      have a working example?
      I'm just wondering if there are plans or already an API/C in python
      core similar to __Pyx_GetStarAr gs. It seems like a very usefull and
      common thing for extensions writer.
      pyrex actually does a very neat job, looking at its output is really
      instructive. How widespread is this solution? I started my extensions
      long ago before realizing such solution existed, and rather stick to a
      regular python API, understood this is what pyrex generates -

      M.


      Raymond L. Buvel wrote:[color=blue]
      > mdcb808@gmail.c om wrote:[color=green]
      > > I am writing a C extension with python 2.3.5 and need constructs
      > > similar to python
      > > func(*args, **kwds)
      > > What's a neat way to do that?
      > > I found pyrex has a __Pyx_GetStarAr gs -
      > > is there something I'm missing from the regular C/API maybe using one
      > > of the PyArg_Parse.. calls ?
      > >
      > > Thanks,
      > > M.
      > >[/color]
      >
      > It looks like the PyArg_ParseTupl eAndKeywords API call is what you are
      > looking for. Documentation is
      >
      > http://python.org/doc/2.4.2/ext/pars...dKeywords.html[/color]

      Comment

      • mdcb808@gmail.com

        #4
        Re: Python C/API - *arg,**kwds variable argumnents

        essentially I already use PyArg_ParseTupl eAndKeywords, but that seems
        to emulate fixed arg list definitions like -
        func (x,y,t=0,u=1)

        Comment

        • Fredrik Lundh

          #5
          Re: Python C/API - *arg,**kwds variable argumnents

          mdcb808@gmail.c om wrote:
          [color=blue]
          > I am writing a C extension with python 2.3.5 and need constructs
          > similar to python
          > func(*args, **kwds)
          > What's a neat way to do that?[/color]

          static PyObject*
          myfunc(PyObject * self, PyObject* args, PyObject* kw)
          {
          ... args is a tuple, kw is a dictionary ...
          }

          static PyMethodDef _functions[] = {
          {"myfunc", (PyCFunction) myfunc, METH_VARARGS|ME TH_KEYWORDS},
          {NULL, NULL}
          };

          </F>



          Comment

          • Carsten Haese

            #6
            Re: Python C/API - *arg,**kwds variable argumnents

            On Wed, 2005-12-14 at 12:00, mdcb808@gmail.c om wrote:[color=blue]
            > essentially I already use PyArg_ParseTupl eAndKeywords, but that seems
            > to emulate fixed arg list definitions like -
            > func (x,y,t=0,u=1)[/color]

            It's unclear what you are actually trying to accomplish. My guess is
            that you want to implement a function/method that takes any number of
            arbitrarily named keyword arguments. If that is the case, you can simply
            use the dictionary that is passed to your C function as the third
            argument.

            Hope this helps,

            Carsten.

            Comment

            • mdcb808@gmail.com

              #7
              Re: Python C/API - *arg,**kwds variable argumnents


              thanks, I get it now.

              Comment

              Working...