Python 2.6 introduces a new feature, a cache for method in C-level
Python types. I believe this will severely interfere with the way
Cython implements 'classmethod' hackery.

I was getting extrange segfaults or very strange behavior (when
accessing a classmethod, then I actually got other method) in my new
mpi4py. Then I started to look at the whole beast, and I believe I've
found the fix. But then I do not kwnow how we could modify Cython for
implementing this, so I'll show and example of what needs to be done.
Look at the very end the 'else' of the pasted generated code, I've
manually added it!!!

  /* "/u/dalcinl/Devel/Cython/tmp/cls.pyx":3
 * cdef class Foo:
 *     def bar(cls): pass
 *     bar = classmethod(bar)             # <<<<<<<<<<<<<<
 *
 */
  __pyx_1 = __Pyx_GetName((PyObject *)__pyx_ptype_3cls_Foo,
__pyx_n_bar); if (unlikely(!__pyx_1)) { /* tb stuff */}
  __pyx_2 = __Pyx_Method_ClassMethod(__pyx_1); if (unlikely(!__pyx_2))
{ /* tb stuff*/}
  Py_DECREF(__pyx_1); __pyx_1 = 0;
  if (PyDict_SetItem((PyObject *)__pyx_ptype_3cls_Foo->tp_dict,
__pyx_n_bar, __pyx_2) < 0) { /* tb stuff*/}
 /* AND  NOW THE FIX !!!!! */
  else {
    (&__pyx_type_3cls_Foo)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
  }

This way, in the next _PyType_Lookup, Python will ignore the method
cache and do a normal lookup and then update the cache.

This seems to solve all the nasty problems I was experienced. Of
course, I'm not completely sure if my fix is the best one. You will
surelly need to ask at Python-Dev with the hope the guy that
implemented this very, very clever hackery on type objects can give
some directions for Cython.

Finally, there is a public function PyType_ClearCache(), but this
function invalidates all the caches starting from the base
'PyBaseObject_Type' type object and recursing on all subclasses.
Perhaps this would be the right way to go, but this would means that
every time you import a cython extension module, you have a full
traversal of type objects clearing caches, and all this because of a
'classmethod' in a cdef class...

Comments?

-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to