Problems with dict and C API

Alex Martelli aleaxit at yahoo.com
Mon Sep 6 23:16:06 CEST 2004


Matjaz <surfmatj at email.si> wrote:

> Dear all,
> 
> I have trouble with creating objects with Python C API,
> subclassed from dict type. What I am trying to do is to
> subclass a dict class (in Python) and create its instance
> in a Python extension (C API).

PyIntance_New wants specifically an old-style class as its first
argument, and you're not giving that in the latter case (a subclass of
dict is intrinsically new-style) so it's diagnosing that and raising the
exception you see.  Just use PyObject_Call and you should be fine.  More
generally, use abstract-object-layer API calls unless you've got very
specific reasons to use something lower-level (concrete layer) -- 90%+
of the time you'll be far happied with the AOL calls.

BTW, to get sys.modules, I suggest you use PyImport_GetModuleDict()
rather than PySys_GetObject("modules") -- it's slightly more direct.


Alex



More information about the Python-list mailing list