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/
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 <facundobatista@gmail.com> 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/)
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.
"Facundo Batista" <facundobatista@gmail.com> wrote in message news:e04bdf310708021111g2870662bo5c6fdb3c1c68a9c2@mail.gmail.com... | >>> 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
Terry Reedy schrieb:
"Facundo Batista" <facundobatista@gmail.com> wrote in message news:e04bdf310708021111g2870662bo5c6fdb3c1c68a9c2@mail.gmail.com... | >>> 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.
On 8/2/07, Georg Brandl <g.brandl@gmx.net> 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/)
2007/8/2, Terry Reedy <tjreedy@udel.edu>:
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/
2007/8/2, Facundo Batista <facundobatista@gmail.com>:
Given that you 'should' return an int, doing elsewise has undefined results.
I'll fix decimal to always return sane values from __cmp__, :)
Done, thanks again everybody! -- . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/
participants (4)
-
Facundo Batista
-
Georg Brandl
-
Guido van Rossum
-
Terry Reedy