Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Segfault accessing dictionary in C Python module (http://www.velocityreviews.com/forums/t612738-segfault-accessing-dictionary-in-c-python-module.html)

Mitko Haralanov 04-22-2008 12:00 AM

Segfault accessing dictionary in C Python module
 
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 mitko@qlogic.com
Senior Software Engineer 650.934.8064
HSG InfiniBand Engineering http://www.qlogic.com

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

sturlamolden 04-22-2008 12:09 AM

Re: Segfault accessing dictionary in C Python module
 
On Apr 22, 2:00 am, Mitko Haralanov <mi...@qlogic.com> wrote:

> 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?



Mark Wooding 04-22-2008 12:47 PM

Re: Segfault accessing dictionary in C Python module
 
Mitko Haralanov <mitko@qlogic.com> wrote:

> 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]

Mitko Haralanov 04-22-2008 05:13 PM

Re: Segfault accessing dictionary in C Python module
 
On Mon, 21 Apr 2008 17:09:57 -0700 (PDT)
sturlamolden <sturlamolden@yahoo.no> wrote:

> 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 mitko@qlogic.com
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.


All times are GMT. The time now is 11:24 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.