[Python-Dev] PyInstance_Check() and new-style classes
Eric Wilhelm
ewilhelm at sbcglobal.net
Mon Jul 12 03:11:45 CEST 2004
Hi,
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
http://ericwilhelm.homeip.net/svn/Inline-Python/trunk/xs-based/py2pl.c
With this test file:
http://ericwilhelm.homeip.net/svn/Inline-Python/trunk/xs-based/t/18newclass.t
(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 =
PyString_Check(obj)+
PyNumber_Check(obj)+
PyInt_Check(obj)+
PyLong_Check(obj)+
PyFloat_Check(obj)+
PyType_Check(obj)+
PyInstance_Check(obj)+
PyDict_Check(obj)+
PyMapping_Check(obj)+
PySequence_Check(obj)+
PyIter_Check(obj)+
PyModule_Check(obj)+
PyClass_Check(obj)+
PyMethod_Check(obj)+
PyCallable_Check(obj) +
(obj == Py_None);
if ((! type_sum && PyType_Check(PyObject_Type(obj))) ||
PyInstance_Check(obj)) {
END QUESTION
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
http://search.cpan.org/CPAN/authors/id/N/NE/NEILW/Inline-Python-0.20.tar.gz
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:
http://ericwilhelm.homeip.net/svn/Inline-Python/trunk/xs-based/
Essentially, the non-whitespace changes look like this:
svn diff -r 199:202 \
http://ericwilhelm.homeip.net/svn/Inline-Python/trunk/xs-based/py2pl.c
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
return true.
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.
Thanks,
Eric
More information about the Python-Dev
mailing list