PyObject_IsInstance is not safe when used to check if the object is an
instance of specified builtin type. Typical code:
rc = PyObject_IsInstance(obj, &Someting_Type);
if (rc < 0) return NULL;
if (rc) {
SometingObject *something = (SometingObject *)obj;
something->some_field ...
}
The __class__ attribute can be modified and PyObject_IsInstance() can
return true if the object has not layout compatible with specified
structure. And even worse, __class__ can be dynamic property and
PyObject_IsInstance() can execute arbitrary Python code, that can
invalidate cached values of pointers and sizes in C code.
More safe way would be to use PyObject_IsSubclass().
rc = PyObject_IsSubclass((Py_Object *)obj->ob_type, &Someting_Type);
if (rc < 0) return NULL;
if (rc) {
SometingObject *something = (SometingObject *)obj;
something->some_field ...
}
For example see issue24102 [1], issue24091 [2] and many other issues
opened by pkt.
[1] http://bugs.python.org/issue24102
[2] http://bugs.python.org/issue24091
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com