using the C API to access objects derived from builtin classes [Was: Re: error converting list to tuple]

Stefan Seefeld seefeld at sympatico.ca
Tue May 18 14:48:46 EDT 2004


Terry Reedy wrote:

> It is easy to mess up in C ;-).  You are most likely to get an answer (from
> another C extension writer) if you post a short as possible code snippet
> that exhibits the error, and the actual and complete error message.

I found the problem, and I'd like to share my new insight, as I don't
think it is obvious:

I hold an object reference that is a dictionary, and I modify it with
the PyDict_* family of functions.

However, the real object is not a builtin 'dict', but instead it is
derived from it:

class Dictionary(dict):
    ...

I just realized that PyDict_SetItem (et al.) don't lookup the
methods in the object's __dict__, so the method that gets really
executed is not the overloaded I wrote, but the method from the
base class.

Replacing 'PyDict_SetItem' by 'PyObject_SetItem' did the trick.
I guess 'PyDict_SetItem' is a shortcut to circumvent the lengthy
method lookup. Fair enough, but the python C API documentation could
be a bit more clear about that...

Kind regards,
		Stefan

PS: for the curious: the missing link between the dict and
     the list / tuple issue is that I derived my own dictionary
     precisely to be able to use a list as key.




More information about the Python-list mailing list