Commit 02953d24 authored by Gregory P. Smith's avatar Gregory P. Smith

ipaddr cleanup for python 3.x:

* Get rid of __hex__.
* Support bytearray as well as bytes.
* Don't double test for integer input.
parent b83819f2
...@@ -263,9 +263,6 @@ class BaseIP(object): ...@@ -263,9 +263,6 @@ class BaseIP(object):
def __int__(self): def __int__(self):
return self.ip return self.ip
def __hex__(self):
return hex(int(self))
def address_exclude(self, other): def address_exclude(self, other):
"""Remove an address from a larger block. """Remove an address from a larger block.
...@@ -572,7 +569,7 @@ class IPv4(BaseIP): ...@@ -572,7 +569,7 @@ class IPv4(BaseIP):
self._version = 4 self._version = 4
# Efficient constructor from integer. # Efficient constructor from integer.
if isinstance(ipaddr, int) or isinstance(ipaddr, int): if isinstance(ipaddr, int):
self.ip = ipaddr self.ip = ipaddr
self._prefixlen = 32 self._prefixlen = 32
self.netmask = self._ALL_ONES self.netmask = self._ALL_ONES
...@@ -580,7 +577,8 @@ class IPv4(BaseIP): ...@@ -580,7 +577,8 @@ class IPv4(BaseIP):
raise IPv4IpValidationError(ipaddr) raise IPv4IpValidationError(ipaddr)
return return
if isinstance(ipaddr, bytes) and len(ipaddr) == 4: # Constructing from a packed address
if isinstance(ipaddr, (bytes, bytearray)) and len(ipaddr) == 4:
self.ip = struct.unpack('!I', ipaddr)[0] self.ip = struct.unpack('!I', ipaddr)[0]
self._prefixlen = 32 self._prefixlen = 32
self.netmask = self._ALL_ONES self.netmask = self._ALL_ONES
...@@ -909,7 +907,7 @@ class IPv6(BaseIP): ...@@ -909,7 +907,7 @@ class IPv6(BaseIP):
self._version = 6 self._version = 6
# Efficient constructor from integer. # Efficient constructor from integer.
if isinstance(ipaddr, int) or isinstance(ipaddr, int): if isinstance(ipaddr, int):
self.ip = ipaddr self.ip = ipaddr
self._prefixlen = 128 self._prefixlen = 128
self.netmask = self._ALL_ONES self.netmask = self._ALL_ONES
...@@ -917,7 +915,8 @@ class IPv6(BaseIP): ...@@ -917,7 +915,8 @@ class IPv6(BaseIP):
raise IPv6IpValidationError(ipaddr) raise IPv6IpValidationError(ipaddr)
return return
if isinstance(ipaddr, bytes) and len(ipaddr) == 16: # Constructing from a packed address
if isinstance(ipaddr, (bytes, bytearray)) and len(ipaddr) == 16:
tmp = struct.unpack('!QQ', ipaddr) tmp = struct.unpack('!QQ', ipaddr)
self.ip = (tmp[0] << 64) | tmp[1] self.ip = (tmp[0] << 64) | tmp[1]
self._prefixlen = 128 self._prefixlen = 128
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment