[PATCH] Add support for Python 3.

6 views
Skip to first unread message

Mikel Ward

unread,
Feb 6, 2017, 7:10:19 PM2/6/17
to ipaddr...@googlegroups.com, Mikel Ward
Python 3 doesn't have 'long' type, xrange, dict.has_key.

Fixes #120.
---
ipaddr.py | 31 ++++++++++++++++++++++---------
ipaddr_test.py | 32 ++++++++++++++++----------------
2 files changed, 38 insertions(+), 25 deletions(-)

diff --git a/ipaddr.py b/ipaddr.py
index c30f298..1f6353f 100644
--- a/ipaddr.py
+++ b/ipaddr.py
@@ -29,6 +29,19 @@ import struct
IPV4LENGTH = 32
IPV6LENGTH = 128

+# Use xrange on Python 2, range on Python 3.
+try:
+ xrange
+except:
+ xrange = range
+
+# Python 2 has long, Python 3 doesn't.
+try:
+ type(long)
+ integer_types = (int, long)
+except NameError:
+ integer_types = (int,)
+

class AddressValueError(ValueError):
"""A Value Error related to the address."""
@@ -516,7 +529,7 @@ class _BaseIP(_IPAddrBase):
return '%s' % self._string_from_ip_int(self._ip)

def __hash__(self):
- return hash(hex(long(self._ip)))
+ return hash(hex(int(self._ip)))

def _get_address_key(self):
return (self._version, self)
@@ -891,7 +904,7 @@ class _BaseNet(_IPAddrBase):
Raises:
NetmaskValueError: If the input is not an integer, or out of range.
"""
- if not isinstance(prefixlen, (int, long)):
+ if not isinstance(prefixlen, integer_types):
raise NetmaskValueError('%r is not an integer' % prefixlen)
prefixlen = int(prefixlen)
if not (0 <= prefixlen <= self._max_prefixlen):
@@ -1266,7 +1279,7 @@ class IPv4Address(_BaseV4, _BaseIP):
return

# Efficient constructor from integer.
- if isinstance(address, (int, long)):
+ if isinstance(address, integer_types):
self._ip = address
if address < 0 or address > self._ALL_ONES:
raise AddressValueError(address)
@@ -1348,7 +1361,7 @@ class IPv4Network(_BaseV4, _BaseNet):
_BaseV4.__init__(self, address)

# Constructing from a single IP address.
- if isinstance(address, (int, long, Bytes, IPv4Address)):
+ if isinstance(address, integer_types + (Bytes, IPv4Address)):
self.ip = IPv4Address(address)
self._ip = self.ip._ip
self._prefixlen = self._max_prefixlen
@@ -1426,7 +1439,7 @@ class _BaseV6(object):
ip_str: A string, the IPv6 ip_str.

Returns:
- A long, the IPv6 ip_str.
+ An integer, the IPv6 ip_str.

Raises:
AddressValueError: if ip_str isn't a valid IPv6 Address.
@@ -1486,7 +1499,7 @@ class _BaseV6(object):

try:
# Now, parse the hextets into a 128-bit integer.
- ip_int = 0L
+ ip_int = 0
for i in xrange(parts_hi):
ip_int <<= 16
ip_int |= self._parse_hextet(parts[i])
@@ -1780,7 +1793,7 @@ class IPv6Address(_BaseV6, _BaseIP):

Additionally, an integer can be passed, so
IPv6Address('2001:4860::') ==
- IPv6Address(42541956101370907050197289607612071936L).
+ IPv6Address(42541956101370907050197289607612071936).
or, more generally
IPv6Address(IPv6Address('2001:4860::')._ip) ==
IPv6Address('2001:4860::')
@@ -1797,7 +1810,7 @@ class IPv6Address(_BaseV6, _BaseIP):
return

# Efficient constructor from integer.
- if isinstance(address, (int, long)):
+ if isinstance(address, integer_types):
self._ip = address
if address < 0 or address > self._ALL_ONES:
raise AddressValueError(address)
@@ -1875,7 +1888,7 @@ class IPv6Network(_BaseV6, _BaseNet):
_BaseV6.__init__(self, address)

# Constructing from a single IP address.
- if isinstance(address, (int, long, Bytes, IPv6Address)):
+ if isinstance(address, integer_types + (Bytes, IPv6Address)):
self.ip = IPv6Address(address)
self._ip = self.ip._ip
self._prefixlen = self._max_prefixlen
diff --git a/ipaddr_test.py b/ipaddr_test.py
index 30b4ccb..b7c5b61 100755
--- a/ipaddr_test.py
+++ b/ipaddr_test.py
@@ -275,7 +275,7 @@ class IpaddrUnitTest(unittest.TestCase):
'2001:658:22a:cafe:200::1')

def testGetNetmask(self):
- self.assertEqual(int(self.ipv4.netmask), 4294967040L)
+ self.assertEqual(int(self.ipv4.netmask), 4294967040)
self.assertEqual(str(self.ipv4.netmask), '255.255.255.0')
self.assertEqual(str(self.ipv4_hostmask.netmask), '255.0.0.0')
self.assertEqual(int(self.ipv6.netmask),
@@ -292,7 +292,7 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual(ipv6_zero_netmask._prefix_from_prefix_string('0'), 0)

def testGetBroadcast(self):
- self.assertEqual(int(self.ipv4.broadcast), 16909311L)
+ self.assertEqual(int(self.ipv4.broadcast), 16909311)
self.assertEqual(str(self.ipv4.broadcast), '1.2.3.255')

self.assertEqual(int(self.ipv6.broadcast),
@@ -1031,7 +1031,7 @@ class IpaddrUnitTest(unittest.TestCase):
# i70
self.assertEqual(hash(ipaddr.IPAddress('1.2.3.4')),
hash(ipaddr.IPAddress(
- long(ipaddr.IPAddress('1.2.3.4')._ip))))
+ int(ipaddr.IPAddress('1.2.3.4')._ip))))
ip1 = ipaddr.IPAddress('10.1.1.0')
ip2 = ipaddr.IPAddress('1::')
dummy = {}
@@ -1078,7 +1078,7 @@ class IpaddrUnitTest(unittest.TestCase):
'7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
'0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
}
- for uncompressed, compressed in test_addresses.items():
+ for uncompressed, compressed in list(test_addresses.items()):
self.assertEqual(compressed, str(ipaddr.IPv6Network(uncompressed)))

def testExplodeShortHandIpStr(self):
@@ -1159,9 +1159,9 @@ class IpaddrUnitTest(unittest.TestCase):

def testNetworkElementCaching(self):
# V4 - make sure we're empty
- self.assertFalse(self.ipv4._cache.has_key('network'))
- self.assertFalse(self.ipv4._cache.has_key('broadcast'))
- self.assertFalse(self.ipv4._cache.has_key('hostmask'))
+ self.assertFalse('network' in self.ipv4._cache)
+ self.assertFalse('broadcast' in self.ipv4._cache)
+ self.assertFalse('hostmask' in self.ipv4._cache)

# V4 - populate and test
self.assertEqual(self.ipv4.network, ipaddr.IPv4Address('1.2.3.0'))
@@ -1169,14 +1169,14 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual(self.ipv4.hostmask, ipaddr.IPv4Address('0.0.0.255'))

# V4 - check we're cached
- self.assertTrue(self.ipv4._cache.has_key('network'))
- self.assertTrue(self.ipv4._cache.has_key('broadcast'))
- self.assertTrue(self.ipv4._cache.has_key('hostmask'))
+ self.assertTrue('network' in self.ipv4._cache)
+ self.assertTrue('broadcast' in self.ipv4._cache)
+ self.assertTrue('hostmask' in self.ipv4._cache)

# V6 - make sure we're empty
- self.assertFalse(self.ipv6._cache.has_key('network'))
- self.assertFalse(self.ipv6._cache.has_key('broadcast'))
- self.assertFalse(self.ipv6._cache.has_key('hostmask'))
+ self.assertFalse('network' in self.ipv6._cache)
+ self.assertFalse('broadcast' in self.ipv6._cache)
+ self.assertFalse('hostmask' in self.ipv6._cache)

# V6 - populate and test
self.assertEqual(self.ipv6.network,
@@ -1187,9 +1187,9 @@ class IpaddrUnitTest(unittest.TestCase):
ipaddr.IPv6Address('::ffff:ffff:ffff:ffff'))

# V6 - check we're cached
- self.assertTrue(self.ipv6._cache.has_key('network'))
- self.assertTrue(self.ipv6._cache.has_key('broadcast'))
- self.assertTrue(self.ipv6._cache.has_key('hostmask'))
+ self.assertTrue('network' in self.ipv6._cache)
+ self.assertTrue('broadcast' in self.ipv6._cache)
+ self.assertTrue('hostmask' in self.ipv6._cache)

def testTeredo(self):
# stolen from wikipedia
--
2.11.0.483.g087da7b7c-goog

Reply all
Reply to author
Forward
0 new messages