[Python-Dev] NotImplemented comparisons
People: Pablo Hoffman opened this bug: "[1764761] Decimal comparison with None fails in Windows". It's not a Decimal problem, see the differente behaviour of this basic test in Linux and Windows: Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 >>> class C(object): ... def __cmp__(self, other): ... return NotImplemented ... >>> c = C() >>> print c < None False >>> print NotImplemented < None False Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 >>> class C(object): def __cmp__(self, other): return NotImplemented >>> c = C() >>> print c < None True >>> print NotImplemented < None False Here's where I stop: don't know where to keep looking... Does somebody know why is a difference here? Furthermore, we can check that is a problem regarding __cmp__: >>> class C(object): def __cmp__(self, other): return NotImplemented def m(self): return NotImplemented >>> c = C() >>> print c < None True >>> print c.m() < None False This is not the first time I find an issue through Decimal regarding NotImplemented, there was this thread: http://mail.python.org/pipermail/python-dev/2005-December/059046.html , but I don't know if that's a separate issue or not. Thanks for your help! -- .Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
NonImplemented isn't treated as special when returned by __cmp__(); __cmp__ is not considered a binary operator like __add__. (__lt__ and friends *do* get treated as such -- but instead of __rlt__ we use __gt__, etc.) --Guido On 8/2/07, Facundo Batista <[EMAIL PROTECTED]> wrote: > People: > > Pablo Hoffman opened this bug: "[1764761] Decimal comparison with None > fails in Windows". > > It's not a Decimal problem, see the differente behaviour of this basic > test in Linux and Windows: > > Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu > 4.1.2-0ubuntu4)] on linux2 > >>> class C(object): > ... def __cmp__(self, other): > ... return NotImplemented > ... > >>> c = C() > >>> print c < None > False > >>> print NotImplemented < None > False > > > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit > (Intel)] on win32 > >>> class C(object): > def __cmp__(self, other): > return NotImplemented > > >>> c = C() > >>> print c < None > True > >>> print NotImplemented < None > False > > > Here's where I stop: don't know where to keep looking... Does somebody > know why is a difference here? > > Furthermore, we can check that is a problem regarding __cmp__: > > >>> class C(object): > def __cmp__(self, other): > return NotImplemented > def m(self): > return NotImplemented > > >>> c = C() > >>> print c < None > True > >>> print c.m() < None > False > > > This is not the first time I find an issue through Decimal regarding > NotImplemented, there was this thread: > > http://mail.python.org/pipermail/python-dev/2005-December/059046.html > > , but I don't know if that's a separate issue or not. > > Thanks for your help! > > -- > .Facundo > > Blog: http://www.taniquetil.com.ar/plog/ > PyAr: http://www.python.org/ar/ > ___ > Python-Dev mailing list > Python-Dev@python.org > http://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > http://mail.python.org/mailman/options/python-dev/guido%40python.org > -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
2007/8/2, Guido van Rossum <[EMAIL PROTECTED]>: > NonImplemented isn't treated as special when returned by __cmp__(); > __cmp__ is not considered a binary operator like __add__. (__lt__ and > friends *do* get treated as such -- but instead of __rlt__ we use > __gt__, etc.) I understand that is tricky how NotImplemented and comparisons interact. But how do you explain the difference in behaviour between Linux and Windows? Thanks! -- .Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
On 02/08/07, Facundo Batista <[EMAIL PROTECTED]> wrote: > I understand that is tricky how NotImplemented and comparisons interact. > > But how do you explain the difference in behaviour between Linux and Windows? A wild guess: c < None falls back to checking c.__cmp__(None) < 0. This translates to NotImplemented < 0, and as the ordering of built in types is implementation dependent, maybe that explains the difference between Windows and Linux? Paul. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
Guido van Rossum schrieb: > NonImplemented isn't treated as special when returned by __cmp__(); > __cmp__ is not considered a binary operator like __add__. (__lt__ and > friends *do* get treated as such -- but instead of __rlt__ we use > __gt__, etc.) But if it's not treated as special, why doesn't the comparison raise an exception, like when __cmp__ returns "foo", for example? Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
"Facundo Batista" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | >>> class C(object): | ... def __cmp__(self, other): | ... return NotImplemented | ... Given that you 'should' return an int, doing elsewise has undefined results. | >>> c = C() | >>> print c < None I presume that this translates into c.__compare(None) < 0 which becomes NotImplemented < 0. The result of that is undefined and interpreter dependent. | >>> print NotImplemented < None As is this. There is no reason to expect the two comparisons (NotImplemented to 0 and None) to give the same or different results. | Does somebody know why is a difference here? Different interpreters, different arbitrary results. I believe checking the ids of the right objects (the type objects, I have read) would explain. | Furthermore, we can check that is a problem regarding __cmp__: | | >>> class C(object): |def __cmp__(self, other): |return NotImplemented |def m(self): | return NotImplemented | | >>> c = C() | >>> print c < None | True | >>> print c.m() < None | False This is still NotImplemented < 0 versus NotImplemented < None. As I understand, such nonsense comparisions will raise exceptions in 3.0. tjr ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
Facundo Batista schrieb: > 2007/8/2, Guido van Rossum <[EMAIL PROTECTED]>: > >> NonImplemented isn't treated as special when returned by __cmp__(); >> __cmp__ is not considered a binary operator like __add__. (__lt__ and >> friends *do* get treated as such -- but instead of __rlt__ we use >> __gt__, etc.) > > I understand that is tricky how NotImplemented and comparisons interact. > > But how do you explain the difference in behaviour between Linux and Windows? I now investigated that, and it seems that if you return NotImplemented from a __cmp__() function, and the other's __cmp__() isn't helpful either, you end up comparing the addresses of the objects (in your case c and None) -- the outcome of which is not consistent across machines or sessions. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
2007/8/2, Paul Moore <[EMAIL PROTECTED]>: > A wild guess: c < None falls back to checking c.__cmp__(None) < 0. > This translates to NotImplemented < 0, and as the ordering of built in > types is implementation dependent, maybe that explains the difference > between Windows and Linux? "NotImplemented < 0" returns False, which is ok, but different from "c < None" 2007/8/2, Guido van Rossum <[EMAIL PROTECTED]>: > > But how do you explain the difference in behaviour between Linux and > > Windows? > > Perhaps the comparison compares the objects' address. No, because NotImplemented and None are always the same: if this is the problem Linux and Windows could be different but they would be consistent with themselves (and Windows is not coherent with itself). Bottom line: I can easily fix Decimal to handle this special case, the point is that maybe we have a lower level bug here... Regards, -- .Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
Terry Reedy schrieb: > "Facundo Batista" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > | >>> class C(object): > | ... def __cmp__(self, other): > | ... return NotImplemented > | ... > > Given that you 'should' return an int, doing elsewise has undefined > results. Returning anything other than an int or NotImplemented raises an exception. NotImplemented seems to be special cased so that the other object's __cmp__ can be tried too. > | >>> c = C() > | >>> print c < None > > I presume that this translates into c.__compare(None) < 0 which becomes > NotImplemented < 0. The result of that is undefined and interpreter > dependent. No, it becomes id(c) < id(None). See half_compare in Objects/typeobject.c. > This is still NotImplemented < 0 versus NotImplemented < None. As I > understand, such nonsense comparisions will raise exceptions in 3.0. Yes, fortunately. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
2007/8/2, Terry Reedy <[EMAIL PROTECTED]>: > Given that you 'should' return an int, doing elsewise has undefined > results. I'll fix decimal to always return sane values from __cmp__, :) Thank you all! Regards, -- .Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] NotImplemented comparisons
On 8/2/07, Georg Brandl <[EMAIL PROTECTED]> wrote: > Returning anything other than an int or NotImplemented raises an exception. > NotImplemented seems to be special cased so that the other object's > __cmp__ can be tried too. Oops, sorry for the misinformation. :-( -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] T_PYSSIZET in Include/structmember.h can be hidden
Martin, Do you know why T_PYSSIZET is inside a #ifdef HAVE_LONG_LONG? That seems like a mistake. Here's the code: #ifdef HAVE_LONG_LONG #define T_LONGLONG 17 #define T_ULONGLONG 18 #define T_PYSSIZET 19 /* Py_ssize_t */ #endif /* HAVE_LONG_LONG */ ISTM, that T_PYSSIZET should be after the #endif. Was this a mistake or intentional? n ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] T_PYSSIZET in Include/structmember.h can be hidden
Neal Norwitz wrote: > Martin, > > Do you know why T_PYSSIZET is inside a #ifdef HAVE_LONG_LONG? That > seems like a mistake. Here's the code: > > #ifdef HAVE_LONG_LONG > #define T_LONGLONG 17 > #define T_ULONGLONG 18 > #define T_PYSSIZET 19 /* Py_ssize_t */ > #endif /* HAVE_LONG_LONG */ > > ISTM, that T_PYSSIZET should be after the #endif. Was this a mistake > or intentional? That was my mistake. Iy should be outside of the #ifdef. Servus, Walter ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com