From 55149cb1eecb486c2ea8a2170f7e58ae1b815e15 Mon Sep 17 00:00:00 2001 From: Jeff Ferland Date: Wed, 27 Feb 2019 14:45:30 -0800 Subject: [PATCH 34/35] (#57) obey explicit version for small int as string --- IPy.py | 8 ++++---- test/test_IPy.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/IPy.py b/IPy.py index 99c9196..486ad44 100644 --- a/IPy.py +++ b/IPy.py @@ -243,7 +243,7 @@ class IPint(object): else: raise ValueError("can't parse") - (self.ip, parsedVersion) = parseAddress(ip) + (self.ip, parsedVersion) = parseAddress(ip, ipversion) if ipversion == 0: ipversion = parsedVersion if prefixlen == -1: @@ -1341,7 +1341,7 @@ def _parseAddressIPv6(ipstr): index += 1 return value -def parseAddress(ipstr): +def parseAddress(ipstr, ipversion=0): """ Parse a string and return the corresponding IP address (as integer) and a guess of the IP version. @@ -1410,7 +1410,7 @@ def parseAddress(ipstr): # assume IPv6 in pure hexadecimal notation return (hexval, 6) - elif ipstr.find('.') != -1 or (intval is not None and intval < 256): + elif ipstr.find('.') != -1 or (intval is not None and intval < 256 and ipversion != 6): # assume IPv4 ('127' gets interpreted as '127.0.0.0') bytes = ipstr.split('.') if len(bytes) > 4: @@ -1428,7 +1428,7 @@ def parseAddress(ipstr): # will be interpreted as IPv4 first byte if intval > MAX_IPV6_ADDRESS: raise ValueError("IP Address can't be larger than %x: %x" % (MAX_IPV6_ADDRESS, intval)) - if intval <= MAX_IPV4_ADDRESS: + if intval <= MAX_IPV4_ADDRESS and ipversion != 6: return (intval, 4) else: return (intval, 6) diff --git a/test/test_IPy.py b/test/test_IPy.py index d363d0e..dc4b61f 100644 --- a/test/test_IPy.py +++ b/test/test_IPy.py @@ -891,6 +891,9 @@ class RegressionTest(unittest.TestCase): self.assertEqual(len(IPy.IP('192.168.0.0/24')), 256) self.assertRaises(ValueError, IPy.IP, '192.168.1.0/42') + def testConsistentIP6StrInt(self): + self.assertEqual(IPy.IP('11', ipversion=6), IPy.IP(11, ipversion=6)) + class TestConstrutor(unittest.TestCase): def testCheckAddrPrefixlenOff(self): self.assertRaises(ValueError, IPy.IP, 0xffffffff + 1, ipversion=4) -- 1.8.3.1