diff options
author | Gregory P. Smith <greg@mad-scientist.com> | 2009-05-02 18:35:58 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@mad-scientist.com> | 2009-05-02 18:35:58 (GMT) |
commit | 02953d244fdb2fe99853d2fe5db905df53c6596f (patch) | |
tree | f8aeeea9807ac47dfb4601d479fab53b881b448a /Lib/ipaddr.py | |
parent | b83819f2912b7be618be35dc5c0bf911bd37220c (diff) | |
download | cpython-02953d244fdb2fe99853d2fe5db905df53c6596f.zip cpython-02953d244fdb2fe99853d2fe5db905df53c6596f.tar.gz cpython-02953d244fdb2fe99853d2fe5db905df53c6596f.tar.bz2 |
ipaddr cleanup for python 3.x:
* Get rid of __hex__.
* Support bytearray as well as bytes.
* Don't double test for integer input.
Diffstat (limited to 'Lib/ipaddr.py')
-rw-r--r-- | Lib/ipaddr.py | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/Lib/ipaddr.py b/Lib/ipaddr.py index 77c21f5..1fec59a 100644 --- a/Lib/ipaddr.py +++ b/Lib/ipaddr.py @@ -263,9 +263,6 @@ class BaseIP(object): def __int__(self): return self.ip - def __hex__(self): - return hex(int(self)) - def address_exclude(self, other): """Remove an address from a larger block. @@ -572,7 +569,7 @@ class IPv4(BaseIP): self._version = 4 # Efficient constructor from integer. - if isinstance(ipaddr, int) or isinstance(ipaddr, int): + if isinstance(ipaddr, int): self.ip = ipaddr self._prefixlen = 32 self.netmask = self._ALL_ONES @@ -580,7 +577,8 @@ class IPv4(BaseIP): raise IPv4IpValidationError(ipaddr) 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._prefixlen = 32 self.netmask = self._ALL_ONES @@ -909,7 +907,7 @@ class IPv6(BaseIP): self._version = 6 # Efficient constructor from integer. - if isinstance(ipaddr, int) or isinstance(ipaddr, int): + if isinstance(ipaddr, int): self.ip = ipaddr self._prefixlen = 128 self.netmask = self._ALL_ONES @@ -917,7 +915,8 @@ class IPv6(BaseIP): raise IPv6IpValidationError(ipaddr) 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) self.ip = (tmp[0] << 64) | tmp[1] self._prefixlen = 128 |