[Python-Dev] 2.5 status

"Martin v. Löwis" martin at v.loewis.de
Mon Aug 7 16:55:24 CEST 2006


tanzer at swing.co.at schrieb:
>     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.

No (not really). Instead, it combines the hash of the target object
with the address of the function object. The hash function of the
method object, in itself, cannot raise this overflow error.

However, it involves hash(p.im_self). So if Script_Category.__hash__
is implemented as you show below, this error might occur.

>         >>> 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

Yes, this comes from id() now always returning positive integers,
which might be a long if the object pointer is > MAXINT

I think both instance_hash and slot_tp_hash should be changed
to just truncate long ints to the range LONG_MIN..LONG_MAX

Notice that this error could have occurred already in 2.4,
on a 64-bit system where sizeof(void*) > sizeof(long) (i.e.
on Win64).

Regards,
Martin


More information about the Python-Dev mailing list