473,569 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Dec 14 '05 #1
6 1570
md*****@gmail.c om wrote:
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.


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
Dec 14 '05 #2
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:
md*****@gmail.c om wrote:
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.


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


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

Dec 14 '05 #4
md*****@gmail.c om wrote:
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?


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>

Dec 14 '05 #5
On Wed, 2005-12-14 at 12:00, md*****@gmail.c om wrote:
essentially I already use PyArg_ParseTupl eAndKeywords, but that seems
to emulate fixed arg list definitions like -
func (x,y,t=0,u=1)


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.

Dec 14 '05 #6

thanks, I get it now.

Dec 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2948
by: Raymond Hettinger | last post by:
Here are four more mini-mysteries for your amusement and edification. In this episode, the program output is not shown. Your goal is to predict the output and, if anything mysterious occurs, then explain what happened (again, in blindingly obvious terms). There's extra credit for giving a design insight as to why things are as they are.
699
33517
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
10
3047
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
23
1882
by: Simon Wittber | last post by:
For the first time, I have been bitten by Python. The below code produces the results: False True when I initially expected the results: False False It took me a while to work out that default keyword argument values
76
3707
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax, rather than completely losing the ability to have anonymous functions. Anyway, I'm looking for feedback on a def-based syntax that came up in a...
12
1498
by: could ildg | last post by:
I have learned python for over a month. I heard that it was very easy to learn, but when I tried to know OO of python, I found it really weird, some expressions seem very hard to understand, and I can't find enough doc to know any more about it. So, I wonder how did you master python? And where to find some cool docs? Thanks.
6
1391
by: Michele Simionato | last post by:
could ildg wrote: > I think decorator is a function which return a function, is this right? > e.g. The decorator below if from http://www.python.org/peps/pep-0318.html#id1. > > def accepts(*types): > def check_accepts(f): > assert len(types) == f.func_code.co_argcount
8
3019
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled...
5
6752
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.