timmerk / ipaddr-py

Automatically exported from code.google.com/p/ipaddr-py
0 stars 0 forks source link

IPv4Address: equal objects produce different hash() values #70

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

In [1]: from ipaddr import IPv4Address
In [2]: a = IPv4Address('1.2.3.4')
In [3]: b = IPv4Address(long(a._ip))
In [4]: a == b
Out[4]: True
In [5]: a in {b: 'foo'}
Out[5]: False
In [6]: a.__dict__
Out[6]: {'_ip': 16909060, '_max_prefixlen': 32, '_version': 4}
In [7]: b.__dict__
Out[7]: {'_ip': 16909060L, '_max_prefixlen': 32, '_version': 4}
In [8]: hash(a)
Out[8]: -5959201619878872315
In [9]: hash(b)
Out[9]: 1175528138704931938

What is the expected output? What do you see instead?

The Python documentation says that objects which compare equal should have the 
same hash value. This is violated here, which means IPv4Address cannot reliably 
be part of a set(), or cannot reliably be used as a dictionary key.

What version of the product are you using? On what operating system?

2.1.5-2, Debian Sid

Please provide any additional information below.

Original issue reported on code.google.com by gomba...@gmail.com on 3 Nov 2010 at 10:55

GoogleCodeExporter commented 9 years ago
thanks for reporting this, and apologies for the delay in getting back to you 
(I was out on vacation until sunday).

r196 (fix) r197 (unittest). Can you test trunk?

Original comment by pmo...@google.com on 16 Nov 2010 at 7:46