[Python-checkins] cpython: Improve an internal ipaddress test, add a comment explaining why treating

nick.coghlan python-checkins at python.org
Sun Jun 17 08:33:14 CEST 2012


http://hg.python.org/cpython/rev/739f5c725958
changeset: 77478:739f5c725958
user: Nick Coghlan <ncoghlan at gmail.com>
date: Sun Jun 17 16:33:00 2012 +1000
summary:
 Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
files:
 Lib/ipaddress.py | 21 +++++++++----------
 Lib/test/test_ipaddress.py | 27 ++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -416,6 +416,11 @@
 """Return the shorthand version of the IP address as a string."""
 return str(self)
 
+ @property
+ def version(self):
+ msg = '%200s has no version specified' % (type(self),)
+ raise NotImplementedError(msg)
+
 def _ip_int_from_prefix(self, prefixlen=None):
 """Turn the prefix length netmask into a int for comparison.
 
@@ -555,10 +560,6 @@
 def _get_address_key(self):
 return (self._version, self)
 
- @property
- def version(self):
- raise NotImplementedError('BaseIP has no version')
-
 
 class _BaseNetwork(_IPAddressBase):
 
@@ -727,12 +728,12 @@
 return int(self.broadcast_address) - int(self.network_address) + 1
 
 @property
- def version(self):
- raise NotImplementedError('BaseNet has no version')
-
- @property
 def _address_class(self):
- raise NotImplementedError('BaseNet has no associated address class')
+ # Returning bare address objects (rather than interfaces) allows for
+ # more consistent behaviour across the network address, broadcast
+ # address and individual host addresses.
+ msg = '%200s has no associated address class' % (type(self),)
+ raise NotImplementedError(msg)
 
 @property
 def prefixlen(self):
@@ -1333,7 +1334,6 @@
 
 """
 # Class to use when creating address objects
- # TODO (ncoghlan): Investigate using IPv4Interface instead
 _address_class = IPv4Address
 
 def __init__(self, address, strict=True):
@@ -1945,7 +1945,6 @@
 """
 
 # Class to use when creating address objects
- # TODO (ncoghlan): Investigate using IPv6Interface instead
 _address_class = IPv6Address
 
 def __init__(self, address, strict=True):
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -150,16 +150,31 @@
 self.assertEqual(first, last)
 self.assertEqual(0, ipaddress._get_prefix_length(2**32, 0, 32))
 self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
- base_ip = ipaddress._BaseAddress('127.0.0.1')
- try:
- base_ip.version
- self.fail('_BaseAddress.version didn\'t raise NotImplementedError')
- except NotImplementedError:
- pass
 self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
 self.assertEqual('0x1020318', hex(self.ipv4_network))
 self.assertRaises(TypeError, self.ipv4_network.__eq__, object())
 
+ def testMissingAddressVersion(self):
+ class Broken(ipaddress._BaseAddress):
+ pass
+ broken = Broken('127.0.0.1')
+ with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
+ broken.version
+
+ def testMissingNetworkVersion(self):
+ class Broken(ipaddress._BaseNetwork):
+ pass
+ broken = Broken('127.0.0.1')
+ with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
+ broken.version
+
+ def testMissingAddressClass(self):
+ class Broken(ipaddress._BaseNetwork):
+ pass
+ broken = Broken('127.0.0.1')
+ with self.assertRaisesRegex(NotImplementedError, "Broken.*address"):
+ broken._address_class
+
 def testGetNetwork(self):
 self.assertEqual(int(self.ipv4_network.network_address), 16909056)
 self.assertEqual(str(self.ipv4_network.network_address), '1.2.3.0')
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /