summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpmoody@google.com <pmoody@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035>2011-11-29 01:38:30 +0000
committerpmoody@google.com <pmoody@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035>2011-11-29 01:38:30 +0000
commit102a7e13c03bfbe84891d0debd7ae6074c411b9a (patch)
tree2b77439b43847e75c51c002233e72a2139a81768
parent5e3f1d3f468a5e981e0544e273d361e9f6fce517 (diff)
downloadipaddr-py-102a7e13c03bfbe84891d0debd7ae6074c411b9a.tar.gz
patch r235 - r237 into the pep3144 branch
git-svn-id: https://ipaddr-py.googlecode.com/svn@238 09200d28-7f98-11dd-ad27-0f66e57d2035
-rw-r--r--branches/3144/ipaddr.py20
-rwxr-xr-xbranches/3144/ipaddr_test.py11
2 files changed, 20 insertions, 11 deletions
diff --git a/branches/3144/ipaddr.py b/branches/3144/ipaddr.py
index 755d3f0..87b7597 100644
--- a/branches/3144/ipaddr.py
+++ b/branches/3144/ipaddr.py
@@ -1073,10 +1073,8 @@ class _BaseV4(object):
self._version = 4
self._max_prefixlen = IPV4LENGTH
- def _explode_shorthand_ip_string(self, ip_str=None):
- if not ip_str:
- ip_str = str(self)
- return ip_str
+ def _explode_shorthand_ip_string(self):
+ return str(self)
def _ip_int_from_string(self, ip_str):
"""Turn the given IP string into an integer for comparison.
@@ -1376,6 +1374,8 @@ class IPv4Interface(_BaseV4, _BaseInterface):
self._prefixlen = self._max_prefixlen
self.netmask = IPv4Address(self._ip_int_from_prefix(
self._prefixlen))
+ if self._prefixlen == (self._max_prefixlen - 1):
+ self.iterhosts = self.__iter__
def _is_hostmask(self, ip_str):
"""Test if the IP string is a hostmask (rather than a netmask).
@@ -1647,7 +1647,7 @@ class _BaseV6(object):
hextets = self._compress_hextets(hextets)
return ':'.join(hextets)
- def _explode_shorthand_ip_string(self, ip_str=None):
+ def _explode_shorthand_ip_string(self):
"""Expand a shortened IPv6 address.
Args:
@@ -1657,10 +1657,10 @@ class _BaseV6(object):
A string, the expanded IPv6 address.
"""
- if not ip_str:
+ if isinstance(self, _BaseInterface):
+ ip_str = str(self.ip)
+ else:
ip_str = str(self)
- if isinstance(self, _BaseInterface):
- ip_str = str(self.ip)
ip_int = self._ip_int_from_string(ip_str)
parts = []
@@ -1668,6 +1668,8 @@ class _BaseV6(object):
parts.append('%04x' % (ip_int & 0xFFFF))
ip_int >>= 16
parts.reverse()
+ if isinstance(self, _BaseInterface):
+ return '%s/%d' % (':'.join(parts), self.prefixlen)
return ':'.join(parts)
@property
@@ -1954,6 +1956,8 @@ class IPv6Interface(_BaseV6, _BaseInterface):
self._prefixlen = self._max_prefixlen
self.netmask = IPv6Address(self._ip_int_from_prefix(self._prefixlen))
+ if self._prefixlen == (self._max_prefixlen - 1):
+ self.iterhosts = self.__iter__
def _is_valid_netmask(self, prefixlen):
diff --git a/branches/3144/ipaddr_test.py b/branches/3144/ipaddr_test.py
index 054166d..259d010 100755
--- a/branches/3144/ipaddr_test.py
+++ b/branches/3144/ipaddr_test.py
@@ -333,6 +333,11 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual(self.ipv4.subnet(), list(self.ipv4.iter_subnets()))
self.assertEqual(self.ipv6.subnet(), list(self.ipv6.iter_subnets()))
+ def testIterHosts(self):
+ self.assertEqual([ipaddr.IPv4Address('2.0.0.0'),
+ ipaddr.IPv4Address('2.0.0.1')],
+ list(ipaddr.ip_interface('2.0.0.0/31').iterhosts()))
+
def testFancySubnetting(self):
self.assertEqual(sorted(self.ipv4.subnet(prefixlen_diff=3)),
sorted(self.ipv4.subnet(new_prefix=27)))
@@ -967,9 +972,9 @@ class IpaddrUnitTest(unittest.TestCase):
def testExplodeShortHandIpStr(self):
addr1 = ipaddr.IPv6Interface('2001::1')
addr2 = ipaddr.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
- self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001',
- addr1._explode_shorthand_ip_string(str(addr1.ip)))
- self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001',
+ self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001/128',
+ addr1.exploded)
+ self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128',
ipaddr.IPv6Interface('::1/128').exploded)
# issue 77
self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',