On 26 March 2010 01:09, Greg Ewing <[email protected]> wrote:
> Stefan Behnel wrote:
>> From the top of my
>> head, I don't even know which Python operations are really dangerous in
>> __dealloc__, and even those that are may not lead to an error in a
>> particular case due to the way GC works.
>
> The only thing I can think of offhand that might
> be a problem in the face of GC in particular is
> that when __dealloc__ gets called, some of the
> object's Python-valued attributes may have been
> set to None as a result of cycle-breaking. If
> you were to assume they were pointing to some
> particular type of object, a crash could result.
>
> More generally, the main thing to be careful of
> in __dealloc__ is that Python-valued attributes
> defined by a subclass may no longer contain valid
> references (not even to None) by the time the
> base class's __dealloc__ gets called. So if it
> does anything that directly or indirectly results
> in a method of the subclass getting called, and
> that method tries to use one of the subclass's
> attributes, a crash could result. But this has
> nothing to do with GC particularly.
>

So, Greg, see this:

cdef class Foo:
    cdef object bar
    def __dealloc__(self):
        pass

And this (Cython) generated code:


static void __pyx_tp_dealloc_2qq_Foo(PyObject *o) {
  struct __pyx_obj_2qq_Foo *p = (struct __pyx_obj_2qq_Foo *)o;
  {
    PyObject *etype, *eval, *etb;
    PyErr_Fetch(&etype, &eval, &etb);
    ++Py_REFCNT(o);
    __pyx_pf_2qq_3Foo___dealloc__(o);
    if (PyErr_Occurred()) PyErr_WriteUnraisable(o);
    --Py_REFCNT(o);
    PyErr_Restore(etype, eval, etb);
  }
  Py_XDECREF(p->bar);
  (*Py_TYPE(o)->tp_free)(o);
}

Is the object "resurrection" (I mean, the ++Py_REFCNT(o) line) safe?

-- 
Lisandro Dalcin
---------------
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