Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Segfault accessing dictionary in C Python module

Reply
Thread Tools

Segfault accessing dictionary in C Python module

 
 
Mitko Haralanov
Guest
Posts: n/a
 
      04-22-2008
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
Senior Software Engineer 650.934.8064
HSG InfiniBand Engineering http://www.qlogic.com

==========================================
There are never any bugs you haven't found yet.
 
Reply With Quote
 
 
 
 
sturlamolden
Guest
Posts: n/a
 
      04-22-2008
On Apr 22, 2:00 am, Mitko Haralanov <(E-Mail Removed)> 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?


 
Reply With Quote
 
 
 
 
Mark Wooding
Guest
Posts: n/a
 
      04-22-2008
Mitko Haralanov <(E-Mail Removed)> 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]
 
Reply With Quote
 
Mitko Haralanov
Guest
Posts: n/a
 
      04-22-2008
On Mon, 21 Apr 2008 17:09:57 -0700 (PDT)
sturlamolden <(E-Mail Removed)> 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 (E-Mail 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
segfault in extension module Nathaniel Echols Python 12 12-07-2003 01:07 AM
segfault dup'ing string through C++ (via swig) module David Eger Python 0 07-11-2003 01:55 PM



Advertisments