[Python-Dev] PyInstance_Check() and new-style classes (original) (raw)
Eric Wilhelm ewilhelm at sbcglobal.net
Mon Jul 12 18:37:40 CEST 2004
- Previous message: [Python-Dev] PyInstance_Check() and new-style classes
- Next message: [Python-Dev] PyInstance_Check() and new-style classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
The following was supposedly scribed by
Phillip J. Eby
on Monday 12 July 2004 11:12 am:
I'm not sure if I understand what this flag is telling me. According to the api/type-structs.html, PyTPFLAGSHEAPTYPE is set when the object is allocated. To me, that reads that it would be set for builtin tuples, etc even if they were not subclassed. A "heap type" is a type whose type struct is in heap memory. A "non-heap" type is one whose type struct is declared statically, i.e. in C code. So, a non-heap type is either a built-in type, or an extension type provided by a C extension module. A heap type is either one coded in pure Python, or potentially a C extension coded in an unusual fashion (e.g. via Pyrex).
Okay, looks like this will work just beautifully. Heaptype is true for an instance of a class which inherits from a builtin type, but not for a simple instance of a builtin type.
True, if I were trying to wrap a C extension module via Inline::Python, heaptype would not be true, but a really easy work-around should exist by simply defining a python class which does nothing but inherit the C extension. That would give the Inline::Python code something to grab onto.
Anyway, this is much cleaner:
http://ericwilhelm.homeip.net/svn/Inline-Python/trunk/xs-based/py2pl.c
int type_sum =
PyString_Check(obj)+
PyNumber_Check(obj)+
<snip lots of PyFoo_Check(obj) + lines>
/* wrap an instance of a Python class */
/* elw: here we need to make these look like instances: */
if ((! type_sum && PyType_Check(PyObject_Type(obj))) ||
PyInstance_Check(obj)) {
if ((obj->ob_type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
PyInstance_Check(obj)) {
So, I think, with everybody's help, I may have found "the right way" (TM.)
Should this be better documented, or is this an obscure usage? Will it break in future versions of Python?
Thanks, Eric
I arise in the morning torn between a desire to improve the world and a desire to enjoy the world. This makes it hard to plan the day. --E.B. White
- Previous message: [Python-Dev] PyInstance_Check() and new-style classes
- Next message: [Python-Dev] PyInstance_Check() and new-style classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]