On Fri, Apr 17, 2009 at 12:06 PM, Stefan Behnel <[email protected]> wrote: > Lisandro Dalcin wrote: >> On Fri, Apr 17, 2009 at 10:09 AM, Stefan Behnel wrote: >>> ... and that's a feature request, it would be nice to make that work. >>> Could you create two tickets for those? > > What I meant was: add support for setting special methods by assignment, > at least in some cases. I didn't specifically mean "__int__" and > "__long__". > > >> Mmm... we will have to have care here... In Py3k, nb_long was renamed >> to nb_reserved (moreover, now it is a bare void* pointer). > > Yes, they removed all sorts of stuff (and I lobbied for removing all sorts > of dead meat), but they forgot to remove that one in time. It's still in > 3.1a1. > > >> So, In >> Py3k, if a class only defines one of __int__() or __long__(), Cython >> should generate code filling the 'nb_int' slot, > > Sure. > > >> and put a NULL in 'nb_reserved'. > > Always in Py3. > > >> What I do not know is what to do >> if a class defines both special methods... > > Good question. I think if __long__ is defined, that's what best resembles > nb_int in Py3. People are more likely to expect C-int compatibility in > __int__, so the only reason why one would define __long__ is to pass > larger numbers. > > If both are defined, I'd vote for using __long__ in Py3. >
I agree in all above points with you. > >> Also note that the in implementation of __Pyx_PyNumber_Int() (from my >> patch for C-int conversion), "nb_long" takes precedence over "nb_int" >> in Python 2. IMHO, that was the best thing to do... > > What's CPython doing here? > CPython is a real mess here... At the price of manually inspecting the existence of nb_int/nb_long slots, __Pyx_PyNumber_Int(ob) is my best attempt for making all work as expected for Cython. I've worked hard to make to avoid the PyInt/PyLong distinction. So if you do "cdef short j = obj" it will work for PyInt/PyLong in all supported Python runtimes. The only possible issue is that if a integral-like type defines both nb_int/nb_long and they return Python integrals with different values (in the == sense), then the return of "nb_long" will be used and "nb_int" will not be ever called. However, what would be the use case for such implementations of __int__() and __long__() ???. However, this work on Python. In [1]: class A(object): ...: def __int__(self): return 2 ...: def __long__(self): return 3 ...: ...: In [2]: In [3]: int(A()) Out[3]: 2 In [4]: long(A()) Out[4]: 3 -- 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
