Segfault accessing dictionary in C Python module

M

Mitko Haralanov

I have a Python module that I have written using the C API and I am
having a problem accessing a dictionary from that module.

Here is what I have done:
1. In my init function I call
module = Py_InitModule ("name", methods);
2. then I get the module's __dict__ structure:
dict = PyModule_GetDict (module);
3. Later, I insert a dictionary that I have constructed using the
PyDict_* API
new_dict = PyDict_New ();
<Insert values in dict>
PyDict_SetItemString (dict, "dict_name", new_dict);

Here is the problem:
In one of my methods, I need to retrieve the new_dict dictionary and
use it to get a value out of it. I get the dictionary using the
PyObject_GetAttrString function:
new_dict = PyObject_GetAttrString (module, "dict_name");
(module is a global variable for the file)

I make the key from an int with:
key = PyInt_FromLong (state); (where state is an int variable)

and then get the value for that key:
value = PyDict_GetItem (new_dict, key);

The problem is that for the first few times I call the function,
everything work but after that value (which should contain a PyString)
starts getting random values and eventually my program crashes with a
segfault.

When I try to print the address of the dict variable returned by
PyObject_GetAttrString() I always get the same value, so the function
is returning the same thing. However, when I try to print the string
representation of the return value with this:
obj = PyObject_Str (new_dict);
if (PyString_Check (obj))
printf ("%s\n", PyString_AsString (obj));

it works the first few calls and then my program segfaults at the
PyObject_Str (new_dict) call.

As far as I know, I have done everything by the book yet I can't seem
to figure out where the problem is. Any help would be great?

Thank you

--
Mitko Haralanov (e-mail address removed)
Senior Software Engineer 650.934.8064
HSG InfiniBand Engineering http://www.qlogic.com

==========================================
There are never any bugs you haven't found yet.
 
S

sturlamolden

As far as I know, I have done everything by the book yet I can't seem
to figure out where the problem is. Any help would be great?

Albeit not having looked at your code in detail, I'm wiling to bet you
have one of the refcounts wrong.

Have you considered using Cython?
 
M

Mark Wooding

Mitko Haralanov said:
value = PyDict_GetItem (new_dict, key);

You're not calling Py_DECREF on this value are you? That's a no-no,
since you're borrowing the dictionary's reference.

-- [mdw]
 
M

Mitko Haralanov

Albeit not having looked at your code in detail, I'm wiling to bet you
have one of the refcounts wrong.

It turns out you are correct. I forgot to increment the refcount on the
value extracted from the dict (since PyDict_GetItem returns a borrowed
reference). Once I did that, all was well.

Thank you!

--
Mitko Haralanov (e-mail address removed)
Senior Software Engineer 650.934.8064
HSG InfiniBand Engineering http://www.qlogic.com

==========================================
Fry: Drugs are for losers, and hypnosis is for losers with big weird
eyebrows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top