[issue1700288] Armin's method cache optimization updated for Python 2.6

Amaury Forgeot d'Arc report at bugs.python.org
Mon Jan 14 01:01:42 CET 2008


Amaury Forgeot d'Arc added the comment:

After some debug work, I found an explanation:
- The attribute "name" is the name of a type slot. So it becomes cached
during the type construction.
- in function ctypes/_ctypes.c::StructUnionType_new(), the type's
__dict__ is replaced by a "stgdict".
- this "stgdict" is then modified with PyDict_ functions, without any
call to _PyType_Lookup()
- the method cache becomes out of sync ==> boom.

I have come with a patch which corrects the problem, and all ctypes
tests pass:

Index: Modules/_ctypes/stgdict.c
===================================================================
--- Modules/_ctypes/stgdict.c   (revision 59939)
+++ Modules/_ctypes/stgdict.c   (working copy)
@@ -470,7 +470,7 @@
                        Py_DECREF(pair);
                        return -1;
                }
-               if (-1 == PyDict_SetItem(realdict, name, prop)) {
+               if (-1 == PyObject_SetAttr(type, name, prop)) {
                        Py_DECREF(prop);
                        Py_DECREF(pair);
                        return -1;
Index: Modules/_ctypes/_ctypes.c
===================================================================
--- Modules/_ctypes/_ctypes.c   (revision 59939)
+++ Modules/_ctypes/_ctypes.c   (working copy)
@@ -410,7 +410,7 @@
 StructType_setattro(PyObject *self, PyObject *key, PyObject *value)
 {
        /* XXX Should we disallow deleting _fields_? */
-       if (-1 == PyObject_GenericSetAttr(self, key, value))
+       if (-1 == Py_TYPE(self)->tp_base->tp_setattro(self, key, value))
                return -1;

        if (value && PyString_Check(key) &&

I think that these changes are sensible: The first one deal with the
type's attribute instead of updating its __dict__, and the second
properly delegates "tp_setattro" to the base class ('type' in this case).

----------
nosy: +amaury.forgeotdarc

_____________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1700288>
_____________________________________


More information about the Python-bugs-list mailing list