By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

[Python/C] Help me.. How to passa dictionary to C function?

P: n/a
I'm a newbie in Python and Python/C API..

I have a simple question. I want to access python dictionary data type from
C extension module.

---- spam.c ----
#include <Python.h>

PyObject *wrap_clear(PyObject *self, PyObject *args) {
int count;
PyObject *dict;
PyObject *newdict;

newdict = PyDict_New(); // newdict is created in C module

if (!PyArg_Parse(args, "O", &PyDict_Type, &dict)) {
return NULL;

if (PyDict_Check(newdict)) { // newdict is a dictionary
printf("newdict is ok\n");
} else {
printf("newdict is not ok\n");

if (PyDict_Check(dict)) { // dict is from python
printf("dict is ok\n");
} else {
printf("dict is not ok\n");

return Py_BuildValue("i", 0);
static PyMethodDef spamMethods[] = {
{"clear", wrap_clear, 1 },

void initspam() {
PyObject *m;

m = Py_InitModule("spam", spamMethods);
gcc -fpic -c -I/usr/local/python/include/python2.3 -I/usr/local/python/lib/p
ython2.3/config spam.c
$ gcc -shared spam.o -o
$ python
Python 2.3.3 (#1, Mar 29 2004, 18:57:19)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-113)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import spam
d = {"count":1}
d {'count': 1} spam.clear(d) newdict is ok -------> newdict is a dictionary
dict is not ok --------> but dict is not a dictionary

How can i access to the dictionary from C module??

Thanks in advance..

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
> if (!PyArg_Parse(args, "O", &PyDict_Type, &dict)) {
Shouldn't this be "O!" ?
How can i access to the dictionary from C module??

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.