473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

C API with *args and **kw

Hello All,

I'm try to write the C equivalent of:

def kw(*args, **kw):
print "%d args" % len(args),
if "default" in kw:
print "default is %s" % kw["default"]
else:
print "no default"

I've written:
static PyObject *
kw(PyObject *self, PyObject *args, PyObject *kwds) {
PyObject *defval = NULL;
long nargs;
char *str;

nargs = PyTuple_GET_SIZE(args);
printf("%d args ", nargs);

defval = PyDict_GetItemString(kwds, "default");

if (NULL == defval) {
printf("no default\n");
}
else {
str = PyString_AsString(defval);
printf("default is %s\n", str);
}

return Py_BuildValue("");
}

However when running the test:
from kw import kw
kw(default="2")
kw(1)
kw()

I get "bus error" on the 2'nd call (OS X Python 2.5.2).

Any idea?

TIA,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com
Oct 14 '08 #1
5 1679
Miki wrote:
Hello All,

I'm try to write the C equivalent of:
Use PyArg_ParseTupleAndKeywords() to parse the args and kwargs objects.

Christian

Oct 14 '08 #2
Miki wrote:
I'm try to write the C equivalent of:

def kw(*args, **kw):
print "%d args" % len(args),
if "default" in kw:
print "default is %s" % kw["default"]
else:
print "no default"
Consider using Cython instead, it will make your life a lot easier. The above
is valid Cython code that the Cython compiler translates into optimised C code.

http://cython.org/

Stefan
Oct 14 '08 #3
[ Note that there's a mailing list dedicated to the C API,
http://mail.python.org/mailman/listinfo/capi-sig ]

Miki <mi*********@gmail.comwrites:
However when running the test:
from kw import kw
kw(default="2")
kw(1)
kw()

I get "bus error" on the 2'nd call (OS X Python 2.5.2).
When called without keywords, kwds is NULL. You need to handle that
case explicitly.

if (NULL == defval) {
printf("no default\n");
}
else {
str = PyString_AsString(defval);
printf("default is %s\n", str);
}
Note that PyString_AsString expects a string; you should check that
with PyString_Check.
return Py_BuildValue("");
The equivalent of your Python code would be to return None using
Py_RETURN_NONE (sugar for Py_INCREF(Py_None); return Py_None;).
Oct 14 '08 #4
I'm try to write the C equivalent of:
>
Use PyArg_ParseTupleAndKeywords() to parse the args and kwargs objects.
Couldn't figure out how to get *args with ParseTupleAndKeywords.

Thanks,
Miki
Oct 14 '08 #5
Miki <miki.teb...@gmail.comwrites:
However when running the test:
from kw import kw
kw(default="2")
kw(1)
kw()
I get "bus error" on the 2'nd call (OS X Python 2.5.2).

When called without keywords, kwds is NULL. *You need to handle that
case explicitly.
Great, thanks!
* * return Py_BuildValue("");

The equivalent of your Python code would be to return None using
Py_RETURN_NONE (sugar for Py_INCREF(Py_None); return Py_None;).
OK, Thanks.
Oct 14 '08 #6

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

Similar topics

4
by: John J. Lee | last post by:
I'm trying define a class to act as a Mock "handler" object for testing urllib2.OpenerDirector. OpenerDirector (actually, my copy of it) does dir(handler.__class__) to find out what methods a...
3
by: fowlertrainer | last post by:
Hello ! I see these things in many sources, under wxPy: def __init__(self,params,**kw,**args) What are these parameters, and what's the meaning of the double * ? Thanx for any info.
6
by: Peter Kleiweg | last post by:
I'm still new to Python. All my experience with OO programming is in a distant past with C++. Now I have written my first class in Python. The class behaves exactly as I want, but I would like to...
2
by: Pierre Fortin | last post by:
This quest for understanding started very innocently... A simple error on my part, passing on args as "args" instead of "*args" to os.path.join() led me to wonder why an error wasn't raised... ...
15
by: Stefan Behnel | last post by:
Hi! I'm trying to do this in Py2.4b1: ------------------------------- import logging values = {'test':'bla'} logging.log(logging.FATAL, 'Test is %(test)s', values)...
10
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
27
by: TheDD | last post by:
Hello all, right now, i'm using the following macro to automatically add informations to exceptions: #define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args) but AFAIK, it's...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
7
by: 7stud | last post by:
Hi, I can't find any documentation for the * operator when applied in front of a name. Is it a pointer? What about the @ operator? Are there python names for these operators that would...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.