Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test that id() == hash() is not safe across architectures #17

Closed
llimeht opened this issue Nov 20, 2016 · 2 comments
Closed

test that id() == hash() is not safe across architectures #17

llimeht opened this issue Nov 20, 2016 · 2 comments

Comments

@llimeht
Copy link
Contributor

llimeht commented Nov 20, 2016

Hi!

When executing the test suite on 32 bit architectures (Debian's i386 and armhf), the test suite fails with an architecture specific error:

FAIL: test__mock_hashability (tests.test__mocking_instances.MockingTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/build/1st/python-pyforge-1.3.0/.pybuild/pythonX.Y_2.7/build/tests/test__mocking_instances.py",     line 19, in test__mock_hashability
    self.assertEquals(id(self.obj), hash(self.obj))
AssertionError: 3057633680L != -1237333616

The difference in these values is 2^32. From the python documentation, if __hash__ returns a long, then that long may be further hashed to produce a 32 bit integer which appears to be what is happening here, where id() is returning longs on 32 bit archs but not on 64 bit archs.

https://docs.python.org/2/reference/datamodel.html#object.__hash__

Overall, this test appears to be testing the internals of the interpreter and not turning the hashing on/off as desired. Would a better test be to check that a TypeError is not raised?

64 bit Python

(sid_amd64-dchroot)$ python
Python 2.7.12+ (default, Nov  4 2016, 17:04:30) 
[GCC 6.2.0 20161027] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo():
...     def __hash__(self):
...         return id(self)
... 
>>> f = Foo()
>>> id(f)
140149834025872
>>> hash(f)
140149834025872
>>> 

32 bit Python

(sid_i386-dchroot)$ python
Python 2.7.12+ (default, Nov  4 2016, 17:04:30) 
[GCC 6.2.0 20161027] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo():
...     def __hash__(self):
...         return id(self)
... 
>>> f = Foo()
>>> id(f)
4143861580L
>>> hash(f)
-151105716
>>>
@vmalloc
Copy link
Owner

vmalloc commented Nov 21, 2016

Ack. Will fix. Thanks for reporting!

@vmalloc
Copy link
Owner

vmalloc commented Nov 21, 2016

Removed the specific check. Didn't really serve any purpose.

@vmalloc vmalloc closed this as completed Nov 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants