File tree 3 files changed +32
-1
lines changed
Filter options
3 files changed +32
-1
lines changed
Original file line number Diff line number Diff line change @@ -729,7 +729,7 @@ def __eq__(self, other):
729
729
return NotImplemented
730
730
731
731
def __hash__ (self ):
732
- return hash (int (self .network_address ) ^ int (self .netmask ))
732
+ return hash (( int (self .network_address ), int (self .netmask ) ))
733
733
734
734
def __contains__ (self , other ):
735
735
# always false if one is v4 and the other is v6.
Original file line number Diff line number Diff line change @@ -2762,6 +2762,34 @@ def testV6HashIsNotConstant(self):
2762
2762
ipv6_address2 = ipaddress .IPv6Interface ("2001:658:22a:cafe:200:0:0:2" )
2763
2763
self .assertNotEqual (ipv6_address1 .__hash__ (), ipv6_address2 .__hash__ ())
2764
2764
2765
+ # issue 134062 Hash collisions in IPv4Network and IPv6Network
2766
+ def testNetworkV4HashCollisions (self ):
2767
+ self .assertNotEqual (
2768
+ ipaddress .IPv4Network ("192.168.1.255/32" ).__hash__ (),
2769
+ ipaddress .IPv4Network ("192.168.1.0/24" ).__hash__ ()
2770
+ )
2771
+ self .assertNotEqual (
2772
+ ipaddress .IPv4Network ("172.24.255.0/24" ).__hash__ (),
2773
+ ipaddress .IPv4Network ("172.24.0.0/16" ).__hash__ ()
2774
+ )
2775
+ self .assertNotEqual (
2776
+ ipaddress .IPv4Network ("192.168.1.87/32" ).__hash__ (),
2777
+ ipaddress .IPv4Network ("192.168.1.86/31" ).__hash__ ()
2778
+ )
2779
+
2780
+ # issue 134062 Hash collisions in IPv4Network and IPv6Network
2781
+ def testNetworkV6HashCollisions (self ):
2782
+ self .assertNotEqual (
2783
+ ipaddress .IPv6Network ("fe80::/64" ).__hash__ (),
2784
+ ipaddress .IPv6Network ("fe80::ffff:ffff:ffff:0/112" ).__hash__ ()
2785
+ )
2786
+ self .assertNotEqual (
2787
+ ipaddress .IPv4Network ("10.0.0.0/8" ).__hash__ (),
2788
+ ipaddress .IPv6Network (
2789
+ "ffff:ffff:ffff:ffff:ffff:ffff:aff:0/112"
2790
+ ).__hash__ ()
2791
+ )
2792
+
2765
2793
2766
2794
if __name__ == '__main__' :
2767
2795
unittest .main ()
Original file line number Diff line number Diff line change
1
+ :mod: `ipaddress `: fix collisions in :meth: `~object.__hash__ ` for
2
+ :class: `~ipaddress.IPv4Network ` and :class: `~ipaddress.IPv6Network `
3
+ objects.
You can’t perform that action at this time.
0 commit comments