| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 433dd40 commit 1a232c4
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,7 +22,7 @@ | |||
| 22 | 22 | ||
| 23 | 23 | from __future__ import annotations | |
| 24 | 24 | ||
| 25 | - from functools import cache, lru_cache | ||
| 25 | + from functools import lru_cache | ||
| 26 | 26 | from ipaddress import AddressValueError, IPv4Address, IPv6Address, NetmaskValueError | |
| 27 | 27 | from typing import Any | |
| 28 | 28 | ||
@@ -34,7 +34,7 @@ | |||
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | 36 | class ZeroconfIPv4Address(IPv4Address): | |
| 37 | - __slots__ = ("__hash__", "_is_link_local", "_is_loopback", "_is_unspecified", "_str", "zc_integer") | ||
| 37 | + __slots__ = ("_hash", "_is_link_local", "_is_loopback", "_is_unspecified", "_str", "zc_integer") | ||
| 38 | 38 | ||
| 39 | 39 | def __init__(self, *args: Any, **kwargs: Any) -> None: | |
| 40 | 40 | """Initialize a new IPv4 address.""" | |
@@ -43,13 +43,17 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: | |||
| 43 | 43 | self._is_link_local = super().is_link_local | |
| 44 | 44 | self._is_unspecified = super().is_unspecified | |
| 45 | 45 | self._is_loopback = super().is_loopback | |
| 46 | - self.__hash__ = cache(lambda: IPv4Address.__hash__(self)) # type: ignore[method-assign] | ||
| 46 | + self._hash = IPv4Address.__hash__(self) | ||
| 47 | 47 | self.zc_integer = int(self) | |
| 48 | 48 | ||
| 49 | 49 | def __str__(self) -> str: | |
| 50 | 50 | """Return the string representation of the IPv4 address.""" | |
| 51 | 51 | return self._str | |
| 52 | 52 | ||
| 53 | + def __hash__(self) -> int: | ||
| 54 | + """Return the precomputed hash of the IPv4 address.""" | ||
| 55 | + return self._hash | ||
| 56 | + | ||
| 53 | 57 | @property | |
| 54 | 58 | def is_link_local(self) -> bool: | |
| 55 | 59 | """Return True if this is a link-local address.""" | |
@@ -67,7 +71,7 @@ def is_loopback(self) -> bool: | |||
| 67 | 71 | ||
| 68 | 72 | ||
| 69 | 73 | class ZeroconfIPv6Address(IPv6Address): | |
| 70 | - __slots__ = ("__hash__", "_is_link_local", "_is_loopback", "_is_unspecified", "_str", "zc_integer") | ||
| 74 | + __slots__ = ("_hash", "_is_link_local", "_is_loopback", "_is_unspecified", "_str", "zc_integer") | ||
| 71 | 75 | ||
| 72 | 76 | def __init__(self, *args: Any, **kwargs: Any) -> None: | |
| 73 | 77 | """Initialize a new IPv6 address.""" | |
@@ -76,13 +80,17 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: | |||
| 76 | 80 | self._is_link_local = super().is_link_local | |
| 77 | 81 | self._is_unspecified = super().is_unspecified | |
| 78 | 82 | self._is_loopback = super().is_loopback | |
| 79 | - self.__hash__ = cache(lambda: IPv6Address.__hash__(self)) # type: ignore[method-assign] | ||
| 83 | + self._hash = IPv6Address.__hash__(self) | ||
| 80 | 84 | self.zc_integer = int(self) | |
| 81 | 85 | ||
| 82 | 86 | def __str__(self) -> str: | |
| 83 | 87 | """Return the string representation of the IPv6 address.""" | |
| 84 | 88 | return self._str | |
| 85 | 89 | ||
| 90 | + def __hash__(self) -> int: | ||
| 91 | + """Return the precomputed hash of the IPv6 address.""" | ||
| 92 | + return self._hash | ||
| 93 | + | ||
| 86 | 94 | @property | |
| 87 | 95 | def is_link_local(self) -> bool: | |
| 88 | 96 | """Return True if this is a link-local address.""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,20 @@ def test_cached_ip_addresses_wrapper(): | |||
| 48 | 48 | assert ipv6.is_unspecified is True | |
| 49 | 49 | ||
| 50 | 50 | ||
| 51 | + def test_address_hash_matches_stdlib_and_dedups(): | ||
| 52 | + """Cached address objects hash like their stdlib equals and dedup in sets.""" | ||
| 53 | + v4 = ipaddress.cached_ip_addresses("192.168.1.1") | ||
| 54 | + assert v4 is not None | ||
| 55 | + assert hash(v4) == hash(ipaddress.IPv4Address("192.168.1.1")) | ||
| 56 | + assert hash(v4) == hash(v4) | ||
| 57 | + assert len({v4, ipaddress.ZeroconfIPv4Address("192.168.1.1")}) == 1 | ||
| 58 | + | ||
| 59 | + v6 = ipaddress.cached_ip_addresses("fe80::1") | ||
| 60 | + assert v6 is not None | ||
| 61 | + assert hash(v6) == hash(ipaddress.IPv6Address("fe80::1")) | ||
| 62 | + assert len({v6, ipaddress.ZeroconfIPv6Address("fe80::1")}) == 1 | ||
| 63 | + | ||
| 64 | + | ||
| 51 | 65 | def test_get_ip_address_object_from_record(): | |
| 52 | 66 | """Test the get_ip_address_object_from_record.""" | |
| 53 | 67 | # not link local | |
| Back | FazBrowse Home | New Git URL |
0 commit comments