C API with *args and **kw

Hrvoje Niksic hniksic at xemacs.org
Tue Oct 14 22:36:51 CEST 2008

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

Miki <miki.tebeka at gmail.com> writes:

> 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;).

More information about the Python-list mailing list