Segfault accessing dictionary in C Python module

Discussion in 'Python' started by Mitko Haralanov, Apr 22, 2008.

  1. 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
    Senior Software Engineer 650.934.8064
    HSG InfiniBand Engineering http://www.qlogic.com

    ==========================================
    There are never any bugs you haven't found yet.
    Mitko Haralanov, Apr 22, 2008
    #1
    1. Advertising

  2. Mitko Haralanov

    sturlamolden Guest

    On Apr 22, 2:00 am, Mitko Haralanov <> 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?
    sturlamolden, Apr 22, 2008
    #2
    1. Advertising

  3. Mitko Haralanov

    Mark Wooding Guest

    Mitko Haralanov <> 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]
    Mark Wooding, Apr 22, 2008
    #3
  4. On Mon, 21 Apr 2008 17:09:57 -0700 (PDT)
    sturlamolden <> 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
    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.
    Mitko Haralanov, Apr 22, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Eger
    Replies:
    0
    Views:
    380
    David Eger
    Jul 11, 2003
  2. Nathaniel Echols

    segfault in extension module

    Nathaniel Echols, Dec 1, 2003, in forum: Python
    Replies:
    12
    Views:
    539
    Nat Echols
    Dec 7, 2003
  3. Ilias Lazaridis
    Replies:
    6
    Views:
    438
    Ilias Lazaridis
    Feb 21, 2006
  4. james_027
    Replies:
    1
    Views:
    324
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  5. Andrey Vul
    Replies:
    8
    Views:
    685
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page