473,473 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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 },
{ NULL, NULL }
};

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 spam.so
$ 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
0


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

Thanks in advance..

Jul 18 '05 #1
1 2403
> if (!PyArg_Parse(args, "O", &PyDict_Type, &dict)) {
Shouldn't this be "O!" ?
How can i access to the dictionary from C module??

http://www.python.org/doc/2.3.3/api/dictObjects.html

Regards,
Technoumena
Jul 18 '05 #2

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

Similar topics

2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
25
by: John Nagle | last post by:
Some faster Python implementations are under development. JPython has been around for a while, and PyPy and ShedSkin continue to move forward. It's worth thinking about what slows down Python...
11
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.