diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2014-05-23 22:32:29 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2014-05-23 22:32:29 (GMT) |
commit | b19e75d0c025514ca07aae9ccfaebfdf0674d369 (patch) | |
tree | 0cd78c3fceafae4bcedb9be40d0096e4afdee00f /Lib | |
parent | f573ce913531d2e68d162a7d4cb8b96c35c12559 (diff) | |
download | cpython-b19e75d0c025514ca07aae9ccfaebfdf0674d369.zip cpython-b19e75d0c025514ca07aae9ccfaebfdf0674d369.tar.gz cpython-b19e75d0c025514ca07aae9ccfaebfdf0674d369.tar.bz2 |
Refactor precomputed constants in the ipaddress module (suggested by Charles-François)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ipaddress.py | 125 |
1 files changed, 67 insertions, 58 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index cba157c..bf2de2d 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1325,7 +1325,7 @@ class IPv4Address(_BaseV4, _BaseAddress): reserved IPv4 Network range. """ - return self in self._reserved_network + return self in self._constants._reserved_network @property @functools.lru_cache() @@ -1337,7 +1337,7 @@ class IPv4Address(_BaseV4, _BaseAddress): iana-ipv4-special-registry. """ - return any(self in net for net in self._private_networks) + return any(self in net for net in self._constants._private_networks) @property def is_multicast(self): @@ -1348,7 +1348,7 @@ class IPv4Address(_BaseV4, _BaseAddress): See RFC 3171 for details. """ - return self in self._multicast_network + return self in self._constants._multicast_network @property def is_unspecified(self): @@ -1359,7 +1359,7 @@ class IPv4Address(_BaseV4, _BaseAddress): RFC 5735 3. """ - return self == self._unspecified_address + return self == self._constants._unspecified_address @property def is_loopback(self): @@ -1369,7 +1369,7 @@ class IPv4Address(_BaseV4, _BaseAddress): A boolean, True if the address is a loopback per RFC 3330. """ - return self in self._loopback_network + return self in self._constants._loopback_network @property def is_link_local(self): @@ -1379,7 +1379,7 @@ class IPv4Address(_BaseV4, _BaseAddress): A boolean, True if the address is link-local per RFC 3927. """ - return self in self._linklocal_network + return self in self._constants._linklocal_network class IPv4Interface(IPv4Address): @@ -1578,32 +1578,36 @@ class IPv4Network(_BaseV4, _BaseNetwork): not self.is_private) -IPv4Address._linklocal_network = IPv4Network('169.254.0.0/16') +class _IPv4Constants: + _linklocal_network = IPv4Network('169.254.0.0/16') -IPv4Address._loopback_network = IPv4Network('127.0.0.0/8') + _loopback_network = IPv4Network('127.0.0.0/8') -IPv4Address._multicast_network = IPv4Network('224.0.0.0/4') + _multicast_network = IPv4Network('224.0.0.0/4') -IPv4Address._private_networks = [ - IPv4Network('0.0.0.0/8'), - IPv4Network('10.0.0.0/8'), - IPv4Network('127.0.0.0/8'), - IPv4Network('169.254.0.0/16'), - IPv4Network('172.16.0.0/12'), - IPv4Network('192.0.0.0/29'), - IPv4Network('192.0.0.170/31'), - IPv4Network('192.0.2.0/24'), - IPv4Network('192.168.0.0/16'), - IPv4Network('198.18.0.0/15'), - IPv4Network('198.51.100.0/24'), - IPv4Network('203.0.113.0/24'), - IPv4Network('240.0.0.0/4'), - IPv4Network('255.255.255.255/32'), - ] + _private_networks = [ + IPv4Network('0.0.0.0/8'), + IPv4Network('10.0.0.0/8'), + IPv4Network('127.0.0.0/8'), + IPv4Network('169.254.0.0/16'), + IPv4Network('172.16.0.0/12'), + IPv4Network('192.0.0.0/29'), + IPv4Network('192.0.0.170/31'), + IPv4Network('192.0.2.0/24'), + IPv4Network('192.168.0.0/16'), + IPv4Network('198.18.0.0/15'), + IPv4Network('198.51.100.0/24'), + IPv4Network('203.0.113.0/24'), + IPv4Network('240.0.0.0/4'), + IPv4Network('255.255.255.255/32'), + ] + + _reserved_network = IPv4Network('240.0.0.0/4') -IPv4Address._reserved_network = IPv4Network('240.0.0.0/4') + _unspecified_address = IPv4Address('0.0.0.0') -IPv4Address._unspecified_address = IPv4Address('0.0.0.0') + +IPv4Address._constants = _IPv4Constants class _BaseV6: @@ -1946,7 +1950,7 @@ class IPv6Address(_BaseV6, _BaseAddress): See RFC 2373 2.7 for details. """ - return self in self._multicast_network + return self in self._constants._multicast_network @property def is_reserved(self): @@ -1957,7 +1961,7 @@ class IPv6Address(_BaseV6, _BaseAddress): reserved IPv6 Network ranges. """ - return any(self in x for x in self._reserved_networks) + return any(self in x for x in self._constants._reserved_networks) @property def is_link_local(self): @@ -1967,7 +1971,7 @@ class IPv6Address(_BaseV6, _BaseAddress): A boolean, True if the address is reserved per RFC 4291. """ - return self in self._linklocal_network + return self in self._constants._linklocal_network @property def is_site_local(self): @@ -1981,7 +1985,7 @@ class IPv6Address(_BaseV6, _BaseAddress): A boolean, True if the address is reserved per RFC 3513 2.5.6. """ - return self in self._sitelocal_network + return self in self._constants._sitelocal_network @property @functools.lru_cache() @@ -1993,7 +1997,7 @@ class IPv6Address(_BaseV6, _BaseAddress): iana-ipv6-special-registry. """ - return any(self in net for net in self._private_networks) + return any(self in net for net in self._constants._private_networks) @property def is_global(self): @@ -2277,32 +2281,37 @@ class IPv6Network(_BaseV6, _BaseNetwork): self.broadcast_address.is_site_local) -IPv6Address._linklocal_network = IPv6Network('fe80::/10') - -IPv6Address._multicast_network = IPv6Network('ff00::/8') - -IPv6Address._private_networks = [ - IPv6Network('::1/128'), - IPv6Network('::/128'), - IPv6Network('::ffff:0:0/96'), - IPv6Network('100::/64'), - IPv6Network('2001::/23'), - IPv6Network('2001:2::/48'), - IPv6Network('2001:db8::/32'), - IPv6Network('2001:10::/28'), - IPv6Network('fc00::/7'), - IPv6Network('fe80::/10'), +class _IPv6Constants: + + _linklocal_network = IPv6Network('fe80::/10') + + _multicast_network = IPv6Network('ff00::/8') + + _private_networks = [ + IPv6Network('::1/128'), + IPv6Network('::/128'), + IPv6Network('::ffff:0:0/96'), + IPv6Network('100::/64'), + IPv6Network('2001::/23'), + IPv6Network('2001:2::/48'), + IPv6Network('2001:db8::/32'), + IPv6Network('2001:10::/28'), + IPv6Network('fc00::/7'), + IPv6Network('fe80::/10'), + ] + + _reserved_networks = [ + IPv6Network('::/8'), IPv6Network('100::/8'), + IPv6Network('200::/7'), IPv6Network('400::/6'), + IPv6Network('800::/5'), IPv6Network('1000::/4'), + IPv6Network('4000::/3'), IPv6Network('6000::/3'), + IPv6Network('8000::/3'), IPv6Network('A000::/3'), + IPv6Network('C000::/3'), IPv6Network('E000::/4'), + IPv6Network('F000::/5'), IPv6Network('F800::/6'), + IPv6Network('FE00::/9'), ] -IPv6Address._reserved_networks = [ - IPv6Network('::/8'), IPv6Network('100::/8'), - IPv6Network('200::/7'), IPv6Network('400::/6'), - IPv6Network('800::/5'), IPv6Network('1000::/4'), - IPv6Network('4000::/3'), IPv6Network('6000::/3'), - IPv6Network('8000::/3'), IPv6Network('A000::/3'), - IPv6Network('C000::/3'), IPv6Network('E000::/4'), - IPv6Network('F000::/5'), IPv6Network('F800::/6'), - IPv6Network('FE00::/9'), - ] + _sitelocal_network = IPv6Network('fec0::/10') + -IPv6Address._sitelocal_network = IPv6Network('fec0::/10') +IPv6Address._constants = _IPv6Constants |