Opened 10 years ago

Closed 9 years ago

#11986 closed defect (fixed)

inconsistent integer hashing on 64bit systems with python 2.7

Reported by: fbissey Owned by: jdemeyer
Priority: major Milestone: sage-5.0
Component: c_lib Keywords:
Cc: Merged in: sage-5.0.beta0
Authors: Jeroen Demeyer Reviewers: François Bissey
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: #9958 Stopgaps:

Status badges

Description

With python 2.7 integer hashing is broken on 64bit systems in a similar way than [ http://trac.sagemath.org/sage_trac/ticket/4957#comment:3] in #4957 which was fixed 3 years ago. The following tests

sage -t -long -force_lib "devel/sage-main/sage/rings/padics/padic_capped_relative_element.pyx"
**********************************************************************
File "/usr/share/sage/devel/sage-main/sage/rings/padics/padic_capped_relative_element.pyx", line 2339:
    sage: hash(R(-1)) 
Expected:
    95367431640624
Got:
    1977800240
**********************************************************************

and (which is reminiscent of #4957):

sage -t -long -force_lib "devel/sage-main/sage/rings/integer.pyx"
**********************************************************************
File "/usr/share/sage/devel/sage-main/sage/rings/integer.pyx", line 3046:
    sage: n = -920390823904823094890238490238484; n.__hash__()
Expected:
    6874330978542788722   
Got:
    -2623069716
**********************************************************************
File "/usr/share/sage/devel/sage-main/sage/rings/integer.pyx", line 3061:
    sage: hash(n)
Expected:
    -9223372036854767616      
Got:
    8192
**********************************************************************
File "/usr/share/sage/devel/sage-main/sage/rings/integer.pyx", line 3064:
    sage: hash(n) == hash(int(n))
Expected:
    True
Got:
    False
**********************************************************************

On my 64bit system with python-2.7:

sage: n=2^63+2^13
sage: n
9223372036854784000
sage: hash(n)
8192
sage: int(n)
9223372036854784000L
sage: hash(int(n))
-9223372036854767616

Attachments (1)

11986_integer_hash.patch (4.2 KB) - added by jdemeyer 9 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 10 years ago by fbissey

  • Type changed from PLEASE CHANGE to defect

comment:2 Changed 10 years ago by jdemeyer

  • Milestone sage-4.7.3 deleted

Milestone sage-4.7.3 deleted

comment:3 Changed 10 years ago by jdemeyer

  • Dependencies set to #9958
  • Milestone set to sage-4.8

Changed 9 years ago by jdemeyer

comment:4 Changed 9 years ago by jdemeyer

  • Authors set to Jeroen Demeyer

Attached patch fixes the problem on x86_64 Linux, not yet tested on other systems.

comment:5 Changed 9 years ago by jdemeyer

  • Status changed from new to needs_review

Works fine on 32-bit i386 Linux.

comment:6 Changed 9 years ago by fbissey

I tried this on one of my amd64 machine with a vanilla sage-4.8.alpha3 (using python-2.6) and got this back:

sage -t -long -force_lib "devel/sage-main/sage/rings/integer.pyx"
**********************************************************************
File "/home/work/fbissey/sandbox/sage-4.8.alpha3/devel/sage-main/sage/rings/integer.pyx", line 3075:
    sage: hash(n) == hash(long(n))
Expected:
    True
Got:
    False
**********************************************************************
File "/home/work/fbissey/sandbox/sage-4.8.alpha3/devel/sage-main/sage/rings/integer.pyx", line 3077:
    sage: hash(n-1) == hash(long(n-1))
Expected:
    True
Got:
    False
**********************************************************************
File "/home/work/fbissey/sandbox/sage-4.8.alpha3/devel/sage-main/sage/rings/integer.pyx", line 3079:
    sage: hash(-n) == hash(long(-n))
Expected:
    True
Got:
    False
**********************************************************************
File "/home/work/fbissey/sandbox/sage-4.8.alpha3/devel/sage-main/sage/rings/integer.pyx", line 3081:
    sage: hash(1-n) == hash(long(1-n))
Expected:
    True
Got:
    False
**********************************************************************
1 items had failures:
   4 of  25 in __main__.example_63
***Test Failed*** 4 failures.

Everything is working as intended with python-2.7 on that machine.

comment:7 Changed 9 years ago by fbissey

  • Reviewers set to François Bissey
  • Status changed from needs_review to positive_review

It's fine on a 32bit OS (OS X 10.5.8) with python 2.6. 4.8.alpha3 fails as above on my linux 64bit box but with 4.8.alpha4 (and python 2.6) on the same box everything is fine. I am setting this to positive review.

comment:8 Changed 9 years ago by strogdon

I applied just 11986_integer_hash.patch on top of a fresh build of sage-4.8.alpha4 with python-2.6. Both the padic_capped_relative_element.pyx and integer.pyx tests mentioned above pass here on 32-bit i686 and 64-bit amd64. The tests also pass when sage is built with python-2.7 on the same architectures.

comment:9 Changed 9 years ago by jdemeyer

  • Milestone changed from sage-4.8 to sage-5.0

comment:10 Changed 9 years ago by jdemeyer

  • Merged in set to sage-5.0.beta0
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.