Issue 1393: function comparing lacks NotImplemented error (original) (raw)

Created on 2007-11-05 20:40 by _doublep, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py _doublep,2007-11-05 20:40
Messages (6)
msg57135 - (view) Author: Paul Pogonyshev (_doublep) Date: 2007-11-05 20:40
I believe attached script demonstrates a bug in Python 3000. As far as I can tell, it should print four times 'True'.
msg57165 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-06 17:37
Odd. I'll look into it.
msg57237 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-08 13:28
Additional prints make it easy to understand what happens here: >>> class Anything: ... def __eq__(self, other): ... print("eq") ... return True ... def __ne__(self, other): ... print("ne") ... return False ... >>> x = lambda: None >>> print(x == Anything()) False >>> print(Anything() == x) eq True >>> y = object() >>> print(y == Anything()) eq True >>> print(Anything() == y) eq True x == Anything() doesn't invoke Anything.__eq__(). It's using function.__eq__().
msg57286 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-09 00:23
It's still odd though. Why does object() == Anything() pass control to the right hand side, while (lambda: None) == Anything() doesn't? There's no definition of equality in PyFunction_Type, so it would seem to fall back on the definition in PyBaseObject_Type, which I would expect to return False from the code in object_richcompare()... [...gdb...] OK, here's the solution of the mystery. do_richcompare() ends up taking the swapped code path for object() == Anything(), but not for function objects. This is because Anything() is a subclass of object, but not of . Perhaps the solution is to change object_richcompare() to return NotImplemented instead of returning False? Or perhaps even remove object_richcompare() altogether, since it doesn't do anything that do_richcompare() doesn't know how to do...
msg59288 - (view) Author: Paul Pogonyshev (_doublep) Date: 2008-01-05 16:30
I'd like to ping this issue as I think it is important enough (core is affected).
msg59346 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-06 00:10
That solution (return NotImplemented instead of False) appears to work. Committed revision 59761. Thanks for bringing up this regression w.r.t. 2.5!
History
Date User Action Args
2022-04-11 14:56:27 admin set github: 45734
2008-01-06 00:10:12 gvanrossum set status: open -> closedresolution: fixedmessages: +
2008-01-05 16:30:44 _doublep set messages: +
2007-11-20 00:33:56 ggenellina set nosy: + ggenellina
2007-11-09 00:23:01 gvanrossum set messages: +
2007-11-08 13:28:13 christian.heimes set nosy: + christian.heimesmessages: +
2007-11-06 17:37:59 gvanrossum set priority: normalassignee: gvanrossummessages: + nosy: + gvanrossum
2007-11-05 20:40:45 _doublep create