[Python-Dev] PyInstance_Check() and new-style classes
ewilhelm at sbcglobal.net
Mon Jul 12 03:11:45 CEST 2004
I've succeeded in getting the perl module Inline::Python to work with
new-style classes, but it seems kind of clunky and I'm wonding if someone can
tell me if I'm doing this "the right way" (TM).
To avoid boring you, the file in question is py2pl.c
With this test file:
(this is just a translation of 07nherit.t into inherited new-style classes)
The question is: How do I determine that an instance of a new-style class
(class Daddy(object)) is an instance without doing this:
int type_sum =
(obj == Py_None);
if ((! type_sum && PyType_Check(PyObject_Type(obj))) ||
I've read the pep's 252 and 253, as well as the warts paper, and the current
embedding documentation, and the descrintro. If I've missed something,
please point it out, since it is very hard to search the documentation for
information about "type objects."
Some background: The Inline modules allow us to extend Perl using various
languages without going through XS. Essentially, the code from the other
language is embedded in some C code which is a Perl extension. But, we need
to be able to translate the data-types from Python to Perl through C,
including creating entries in Perl's symbol table so that Python instances
can be returned to the Perl code and then used to call the methods as if they
were Perl subroutines.
For starters, I'm working from Neil Watkiss's code
From there, I checked the original into my subversion server. There have been
quite a few whitespace changes, so if you have subversion, an svn diff would
be a good way to see the changes.
The source directory can be seen here:
Essentially, the non-whitespace changes look like this:
svn diff -r 199:202 \
Where, the original Inline-Python-0.20 code would just return a string from
the constructor of a new-style class because PyInstance_Check() did not
So, is PyInstance_Check() supposed to work, or is there some PyFoo_Check()
function that will work?
It seems really wrong to have to add-up all of the other types and use this
process of elimination to identify a new-style class instance. Also, it
seems that it would fail if a class inherited from a built-in type. I'm
almost sure that I'm way off-course, so if anyone could put me on track, I'd
really appreciate it.
More information about the Python-Dev