C API for new-style classes

E

Eric Wilhelm

Hi,

By new-style classes, I'm referring to the changes which came into 2.2 as
a result of PEP 252 and 253. I discovered this problem when trying to
use the Perl Inline::python module with a python class that was
inheriting from the new builting 'object' class like so:

class MyClass(object):

The problem is in detecting that this class should be treated as a class
from C. 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.)

Thanks,
Eric
 
A

Aahz

By new-style classes, I'm referring to the changes which came into 2.2 as
a result of PEP 252 and 253. I discovered this problem when trying to
use the Perl Inline::python module with a python class that was
inheriting from the new builting 'object' class like so:

class MyClass(object):

The problem is in detecting that this class should be treated as a class
from C. 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.

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

Eric Wilhelm

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top