Issue1536021
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2006-08-07 14:21 by tanzer, last changed 2022-04-11 14:56 by admin. This issue is now closed.
Messages (7) | |||
---|---|---|---|
msg29462 - (view) | Author: Christian Tanzer (tanzer) * | Date: 2006-08-07 14:21 | |
I've run into a problem with a big application that I wasn't able to reproduce with a small example. The code (exception handler added to demonstrate and work around the problem): try : h = hash(p) except OverflowError, e: print type(p), p, id(p), e h = id(p) & 0x0FFFFFFF prints the following output: <type 'instancemethod'> <bound method Script_Category.is_applicable of <Script_Menu_Mgr.Script_Category object at 0xb6cb4f8c>> 3066797028 long int too large to convert to int This happens with Python 2.5b3, but didn't happen with Python 2.4.3. I assume that the hash-function for function/methods returns the `id` of the function. The following code demonstrates the same problem with a Python class whose `__hash__` returns the `id` of the object: $ python2.4 Python 2.4.3 (#1, Jun 30 2006, 10:02:59) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class X(object): ... def __hash__(self): return id(self) ... >>> hash (X()) -1211078036 $ python2.5 Python 2.5b3 (r25b3:51041, Aug 7 2006, 15:35:35) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class X(object): ... def __hash__(self): return id(self) ... >>> hash (X()) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: long int too large to convert to int |
|||
msg29463 - (view) | Author: Nick Coghlan (ncoghlan) * | Date: 2006-08-07 15:03 | |
Logged In: YES user_id=1038590 MvL diagnosed the problem on python-dev as being due to id(obj) now always returning positive values (which may sometimes be a long). This seems like sufficient justification to change the hashing implementation to tolerate long values being returned from __hash__ methods (e.g. by using the hash of a returned long value, instead of trying to convert it to a C int directly). |
|||
msg29464 - (view) | Author: Armin Rigo (arigo) * | Date: 2006-08-08 10:25 | |
Logged In: YES user_id=4771 The hash of instance methods changed, and id() changed to return non-negative numbers (so that id() is not the default hash any more). But I cannot see how your problem shows up. The only thing I could imagine is that the Script_Category class has a custom __hash__() method which returns a value that is sometimes a long, as it would be if it were based on id(). (It has always been documented that just returning id() in custom __hash__() methods doesn't work because of this, but on 32-bit machines the problem only became apparent with the change in id() in Python 2.5.) |
|||
msg29465 - (view) | Author: Martin v. Löwis (loewis) * | Date: 2006-08-09 07:58 | |
Logged In: YES user_id=21627 Thanks for the report. Fixed in r51160 |
|||
msg29466 - (view) | Author: Christian Tanzer (tanzer) * | Date: 2006-08-09 09:24 | |
Logged In: YES user_id=2402 > The only thing I could imagine is that the Script_Category > class has a custom __hash__() method which returns a value > that is sometimes a long, as it would be if it were > based on id(). That was indeed the problem in my code (returning `id(self)`). > It has always been documented that just returning id() > in custom __hash__() methods doesn't work because of > this AFAIR, it was once documented that the default hash value is the id of an object. And I just found a message by the BFDL himself proclaiming so: http://python.project.cwi.nl/search/hypermail/python-recent/0168.html. OTOH, I don't remember seeing anything about this in AMK's `What's new in Python 2.x` documents (but found an entry in NEWS.txt for some 2.5 alpha). I've now changed all my broken `__hash__` methods (not that many fortunately) but it might be a good idea to document this change in a more visible way. |
|||
msg29467 - (view) | Author: Georg Brandl (georg.brandl) * | Date: 2006-08-09 09:47 | |
Logged In: YES user_id=849994 Andrew, do you want to add a whatsnew entry? |
|||
msg29468 - (view) | Author: A.M. Kuchling (akuchling) * | Date: 2006-08-09 13:20 | |
Logged In: YES user_id=11375 Added. Closing this bug. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:56:19 | admin | set | github: 43789 |
2006-08-07 14:21:37 | tanzer | create |