Merged revisions 72241 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r72241 | gregory.p.smith | 2009-05-03 12:37:05 -0700 (Sun, 03 May 2009) | 3 lines

  Optimization: move RFC defined network constant construction out of
  the is_*() methods and into module private instances.
........
This commit is contained in:
Gregory P. Smith 2009-05-03 19:38:29 +00:00
parent 297c9fbe1f
commit 75930f85df
1 changed files with 33 additions and 14 deletions

View File

@ -26,6 +26,7 @@ __version__ = '1.0.2'
import struct import struct
class Error(Exception): class Error(Exception):
"""Base class for exceptions.""" """Base class for exceptions."""
@ -537,6 +538,7 @@ class IPv4(BaseIP):
# Equivalent to 255.255.255.255 or 32 bits of 1's. # Equivalent to 255.255.255.255 or 32 bits of 1's.
_ALL_ONES = 0xffffffff _ALL_ONES = 0xffffffff
_version = 4
def __init__(self, ipaddr): def __init__(self, ipaddr):
"""Instantiate a new IPv4 object. """Instantiate a new IPv4 object.
@ -566,7 +568,6 @@ class IPv4(BaseIP):
""" """
BaseIP.__init__(self) BaseIP.__init__(self)
self._version = 4
# Efficient constructor from integer. # Efficient constructor from integer.
if isinstance(ipaddr, int): if isinstance(ipaddr, int):
@ -715,9 +716,10 @@ class IPv4(BaseIP):
A boolean, True if the address is reserved per RFC 1918. A boolean, True if the address is reserved per RFC 1918.
""" """
return (self in IPv4('10.0.0.0/8') or for network in _IPV4_RFC1918_NETWORKS:
self in IPv4('172.16.0.0/12') or if self in network:
self in IPv4('192.168.0.0/16')) return True
return False
@property @property
def is_multicast(self): def is_multicast(self):
@ -728,7 +730,7 @@ class IPv4(BaseIP):
See RFC 3171 for details. See RFC 3171 for details.
""" """
return self in IPv4('224.0.0.0/4') return self in _IPV4_RFC3171_MULTICAST
@property @property
def is_loopback(self): def is_loopback(self):
@ -738,7 +740,7 @@ class IPv4(BaseIP):
A boolean, True if the address is a loopback per RFC 3330. A boolean, True if the address is a loopback per RFC 3330.
""" """
return self in IPv4('127.0.0.0/8') return self in _IPV4_RFC3330_LOOPBACK
@property @property
def is_link_local(self): def is_link_local(self):
@ -748,7 +750,7 @@ class IPv4(BaseIP):
A boolean, True if the address is link-local per RFC 3927. A boolean, True if the address is link-local per RFC 3927.
""" """
return self in IPv4('169.254.0.0/16') return self in _IPV4_RFC3927_LINK_LOCAL
@property @property
def version(self): def version(self):
@ -877,6 +879,7 @@ class IPv6(BaseIP):
""" """
_ALL_ONES = (2**128) - 1 _ALL_ONES = (2**128) - 1
_version = 6
def __init__(self, ipaddr): def __init__(self, ipaddr):
"""Instantiate a new IPv6 object. """Instantiate a new IPv6 object.
@ -904,7 +907,6 @@ class IPv6(BaseIP):
""" """
BaseIP.__init__(self) BaseIP.__init__(self)
self._version = 6
# Efficient constructor from integer. # Efficient constructor from integer.
if isinstance(ipaddr, int): if isinstance(ipaddr, int):
@ -1044,7 +1046,7 @@ class IPv6(BaseIP):
See RFC 2373 2.7 for details. See RFC 2373 2.7 for details.
""" """
return self in IPv6('ff00::/8') return self in _IPV6_RFC2373_MULTICAST
@property @property
def is_unspecified(self): def is_unspecified(self):
@ -1055,7 +1057,7 @@ class IPv6(BaseIP):
RFC 2373 2.5.2. RFC 2373 2.5.2.
""" """
return self == IPv6('::') return self == _IPV6_RFC2373_UNSPECIFIED
@property @property
def is_loopback(self): def is_loopback(self):
@ -1066,7 +1068,7 @@ class IPv6(BaseIP):
RFC 2373 2.5.3. RFC 2373 2.5.3.
""" """
return self == IPv6('::1') return self == _IPV6_RFC2373_LOOPBACK
@property @property
def is_link_local(self): def is_link_local(self):
@ -1076,7 +1078,7 @@ class IPv6(BaseIP):
A boolean, True if the address is reserved per RFC 4291. A boolean, True if the address is reserved per RFC 4291.
""" """
return self in IPv6('fe80::/10') return self in _IPV6_RFC4291_LINK_LOCAL
@property @property
def is_site_local(self): def is_site_local(self):
@ -1090,7 +1092,7 @@ class IPv6(BaseIP):
A boolean, True if the address is reserved per RFC 3513 2.5.6. A boolean, True if the address is reserved per RFC 3513 2.5.6.
""" """
return self in IPv6('fec0::/10') return self in _IPV6_RFC3513_SITE_LOCAL
@property @property
def is_private(self): def is_private(self):
@ -1100,7 +1102,7 @@ class IPv6(BaseIP):
A boolean, True if the address is reserved per RFC 4193. A boolean, True if the address is reserved per RFC 4193.
""" """
return self in IPv6('fc00::/7') return self in _IPV6_RFC4193_PRIVATE
@property @property
def version(self): def version(self):
@ -1347,3 +1349,20 @@ class IPv6(BaseIP):
""" """
return self.prefixlen return self.prefixlen
# IPv4 constants.
_IPV4_RFC1918_NETWORKS = (IPv4('10.0.0.0/8'),
IPv4('172.16.0.0/12'),
IPv4('192.168.0.0/16'))
_IPV4_RFC3171_MULTICAST = IPv4('224.0.0.0/4')
_IPV4_RFC3330_LOOPBACK = IPv4('127.0.0.0/8')
_IPV4_RFC3927_LINK_LOCAL = IPv4('169.254.0.0/16')
# IPv6 constants.
_IPV6_RFC2373_MULTICAST = IPv6('ff00::/8')
_IPV6_RFC2373_UNSPECIFIED = IPv6('::')
_IPV6_RFC2373_LOOPBACK = IPv6('::1')
_IPV6_RFC4291_LINK_LOCAL = IPv6('fe80::/10')
_IPV6_RFC3513_SITE_LOCAL = IPv6('fec0::/10') # Deprecated by RFC3879.
_IPV6_RFC4193_PRIVATE = IPv6('fc00::/7')