summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Moody <pmoody@google.com>2009-10-08 06:35:42 +0000
committerPeter Moody <pmoody@google.com>2009-10-08 06:35:42 +0000
commit733a49c3ceba26537f3d1128a8b15a766ea108e2 (patch)
tree54b413e13c2291a1f3cefb89acbc53766bffc550
parent89a575d69bd2abbf1dec83b8928c66f2fd3f0ec2 (diff)
downloadipaddr-py-733a49c3ceba26537f3d1128a8b15a766ea108e2.tar.gz
+ make IPv4's _ip_int_from_string more robust.
patch from mattimustang git-svn-id: https://ipaddr-py.googlecode.com/svn/trunk@116 09200d28-7f98-11dd-ad27-0f66e57d2035
-rw-r--r--ipaddr.py10
-rwxr-xr-xipaddr_test.py4
2 files changed, 12 insertions, 2 deletions
diff --git a/ipaddr.py b/ipaddr.py
index 34d0c3f..51d70a6 100644
--- a/ipaddr.py
+++ b/ipaddr.py
@@ -1016,7 +1016,10 @@ class _BaseV4(object):
if len(octets) != 4:
raise IPv4IpValidationError(ip_str)
for oc in octets:
- packed_ip = (packed_ip << 8) | int(oc)
+ try:
+ packed_ip = (packed_ip << 8) | int(oc)
+ except ValueError:
+ raise IPv4IpValidationError(ip_str)
return packed_ip
def _string_from_ip_int(self, ip_int):
@@ -1304,7 +1307,10 @@ class IPv4Network(_BaseV4, _BaseNet):
A boolean, True if the IP string is a hostmask.
"""
- parts = [int(x) for x in ip_str.split('.')]
+ try:
+ parts = [int(x) for x in ip_str.split('.')]
+ except ValueError:
+ return False
if parts[0] < parts[-1]:
return True
return False
diff --git a/ipaddr_test.py b/ipaddr_test.py
index cdcf579..488b9ff 100755
--- a/ipaddr_test.py
+++ b/ipaddr_test.py
@@ -81,6 +81,10 @@ class IpaddrUnitTest(unittest.TestCase):
'1234:axy::b')
self.assertRaises(ipaddr.IPv6IpValidationError, ipaddr.IPv6Address,
'1234:axy::b')
+ self.assertRaises(ipaddr.IPv4IpValidationError,
+ ipaddr.IPv4Address(1)._ip_int_from_string,
+ '1.a.2.3')
+ self.assertEqual(False, ipaddr.IPv4Network(1)._is_hostmask('1.a.2.3'))
def testGetNetwork(self):
self.assertEqual(int(self.ipv4.network), 16909056)