summaryrefslogtreecommitdiffstats
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-01-18 20:36:33 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-01-18 20:36:33 (GMT)
commit5f38f5c5028d5c015e67ffa8463bf42c66ad2e05 (patch)
tree1e06dd83a544f25e81de0c1d562efa1e65df786e /Lib/ipaddress.py
parent3b225d8bfb748a02d4bada14d4498e9b6aa4e162 (diff)
downloadcpython-5f38f5c5028d5c015e67ffa8463bf42c66ad2e05.zip
cpython-5f38f5c5028d5c015e67ffa8463bf42c66ad2e05.tar.gz
cpython-5f38f5c5028d5c015e67ffa8463bf42c66ad2e05.tar.bz2
Issue #23133: Pickling of ipaddress objects now produces more compact and
portable representation.
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py21
1 files changed, 14 insertions, 7 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 8d369e3..ce6bf0c 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -567,6 +567,9 @@ class _IPAddressBase(_TotalOrderingMixin):
except ValueError:
cls._report_invalid_netmask(ip_str)
+ def __reduce__(self):
+ return self.__class__, (str(self),)
+
class _BaseAddress(_IPAddressBase):
@@ -576,11 +579,6 @@ class _BaseAddress(_IPAddressBase):
used by single IP addresses.
"""
- def __init__(self, address):
- if (not isinstance(address, bytes)
- and '/' in str(address)):
- raise AddressValueError("Unexpected '/' in %r" % address)
-
def __int__(self):
return self._ip
@@ -626,6 +624,9 @@ class _BaseAddress(_IPAddressBase):
def _get_address_key(self):
return (self._version, self)
+ def __reduce__(self):
+ return self.__class__, (self._ip,)
+
class _BaseNetwork(_IPAddressBase):
@@ -1295,7 +1296,6 @@ class IPv4Address(_BaseV4, _BaseAddress):
AddressValueError: If ipaddress isn't a valid IPv4 address.
"""
- _BaseAddress.__init__(self, address)
_BaseV4.__init__(self, address)
# Efficient constructor from integer.
@@ -1313,6 +1313,8 @@ class IPv4Address(_BaseV4, _BaseAddress):
# Assume input argument to be string or any object representation
# which converts into a formatted IP string.
addr_str = str(address)
+ if '/' in addr_str:
+ raise AddressValueError("Unexpected '/' in %r" % address)
self._ip = self._ip_int_from_string(addr_str)
@property
@@ -1446,6 +1448,8 @@ class IPv4Interface(IPv4Address):
def __hash__(self):
return self._ip ^ self._prefixlen ^ int(self.network.network_address)
+ __reduce__ = _IPAddressBase.__reduce__
+
@property
def ip(self):
return IPv4Address(self._ip)
@@ -1920,7 +1924,6 @@ class IPv6Address(_BaseV6, _BaseAddress):
AddressValueError: If address isn't a valid IPv6 address.
"""
- _BaseAddress.__init__(self, address)
_BaseV6.__init__(self, address)
# Efficient constructor from integer.
@@ -1938,6 +1941,8 @@ class IPv6Address(_BaseV6, _BaseAddress):
# Assume input argument to be string or any object representation
# which converts into a formatted IP string.
addr_str = str(address)
+ if '/' in addr_str:
+ raise AddressValueError("Unexpected '/' in %r" % address)
self._ip = self._ip_int_from_string(addr_str)
@property
@@ -2134,6 +2139,8 @@ class IPv6Interface(IPv6Address):
def __hash__(self):
return self._ip ^ self._prefixlen ^ int(self.network.network_address)
+ __reduce__ = _IPAddressBase.__reduce__
+
@property
def ip(self):
return IPv6Address(self._ip)