Forgot to reply-to-all ... ---------- Forwarded message ---------- From: Lisandro Dalcin <[email protected]> Date: 27 August 2010 18:36 Subject: Re: [Cython] some diffs to boost::python classes To: Stefan Behnel <[email protected]>
On 27 August 2010 17:54, Stefan Behnel <[email protected]> wrote: > Lisandro Dalcin, 27.08.2010 18:08: >> >> On 27 August 2010 09:40, Stefan Behnel wrote: >>> >>> BTW, now that you mention it - does anyone know what became of the idea >>> to >>> allow a >>> >>> cdef __dict__ >>> >>> attribute notation (as for __weakref__) to add a dict to an extension >>> type? >>> AFAIR, that was already proposed more than once on this list. >>> >> >> Here you have a tentative patch (use "hg import --no-commit ...") >> >> There is a small gotcha, tough... CPython makes an optimization and >> the instance dict in created on-demand. My patch unconditionally >> creates the dict at tp_new. If not, the dict slot in the object >> structure has to be set to NULL, but any usage of __dict__ in cdef >> methods will assume non-NULL and segfault. > > Why is that? Does the external lookup code pass through the slot directly? I > can't see that from your patch. I do not understand you, and perhaps I was not clear enough. When I say that there is a gotcha, it actually means that CPython is a bit more optimized: if you create an instance but never set and attr nor access the __dict__, then the instance dict is never created and the object structure maintain a NULL in the slot. In my patch, the __dict__ slot is always filled with PyDict_New() at tp_new. > Also, I don't see why this would be > necessary. I don't think access to TheType.__dict__ or obj.__dict__ is > commonly performance critical enough to require direct slot access. From my > point of view, descriptor based access would be perfectly ok, and on-demand > instantiation would work just fine with that. > My current implementation permits Cython code to access the instance __dict__ from the object structure, like any other cdef attribute. Moreover, by using 'public' and 'readonly', cdef classes can decide how to expose instance dictionaries to Python code... You can see that in action in the testcase. > I think the right way to implement this is with custom decriptors for both > the type and its instance. > Do you mean adding a descriptor for __dict__ ? If you use cdef public|readonly __dict__, you get it. But Cython code will make a direct pointer access, like for any other cdef member... What's wrong with this approach? In short, I think the patch in its current status (modulo some possible oversight) is safe, Python code accessing instances will work as expected, Cython code can access dict safely and fast... The only improvement I can imagine right now is to hack on type inference to attach 'dict' type to __dict__ regardless of the user declaration, my patch does not includes that just because I do not know how to implement the feature. -- Lisandro Dalcin --------------- CIMEC (INTEC/CONICET-UNL) Predio CONICET-Santa Fe Colectora RN 168 Km 472, Paraje El Pozo Tel: +54-342-4511594 (ext 1011) Tel/Fax: +54-342-4511169 -- Lisandro Dalcin --------------- CIMEC (INTEC/CONICET-UNL) Predio CONICET-Santa Fe Colectora RN 168 Km 472, Paraje El Pozo Tel: +54-342-4511594 (ext 1011) Tel/Fax: +54-342-4511169 _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
