C API for new-style classes

Eric Wilhelm ewilhelm at somethinglike.sbcglobalDOTnet
Sat Jul 10 22:44:15 CEST 2004


On Thu, 10 Jun 2004 18:24:35 -0400, Aahz wrote:

>>With old-style classes, PyClass_Check() returns true, but it
>>doesn't work with new-style classes.  With those, apparently we have to
>>use PyType_Check(), but I cannot find this in the extension
>>documentation.
>>
>>Another problem comes in identifying instances of this type of
>>type-class, where PyInstance_Check() no longer returns true.
>>
>>Does anyone know of any documentation for the API for new-style classes?
>>(It must work somehow or the python interpreter couldn't do it.)
> 
> I'm no expert on this, but since nobody else has stepped up to the
> plate...
> 
> New-style classes make things both easier and harder.  Fundamentally,
> there's no longer any difference between a type and an instance --
> that's how you get metaclasses that generate classes as their instances.
> Unfortunately, your best bet is to look at the source of the new builtin
> objects to see how they work.

What I have figured out so far (above) is from looking at the source.

> Let's turn this around a bit: what problem are you trying to solve?

It's somewhat of a generic problem.  I was trying to use the perl module
Inline::Python, which connects python code to perl via the C api's of both
of them.  Thus, the python code is executed within C, and bindings are
created for each method of each class found.  Thus, the return value of a
constructor is a reference to a python object.

For this whole scheme to work, the Inline::Python backend has to be able
to identify classes (it was using PyClass_Check()) and it has to verify
that the object being used is an instance of a class (PyInstance_Check().)

>From the work I have done with it (including lots of digging in the
Inline::Python C and Perl source, along with lots of digging in the python
source and documentation), it basically looks like new-style classes have
broken the C API, or that the "right way" to ask objects if they are
classes or instances has not been documented.

So, I guess the problem is "what is the right way to query objects which
are using the new-style class functionality from the C API?"

--Eric




More information about the Python-list mailing list