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

==========================================
There are never any bugs you haven't found yet.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to