# HG changeset patch # User Søren Løvborg # Date 1416933862 -3600 # Tue Nov 25 17:44:22 2014 +0100 # Node ID a79d4a814ccf2db2281d769f69c6acef8a176f97 # Parent 32d08aacffe063f0d11ac2dcdec48678858eeea9 Issue #22941: Sensible IPv[46]Interface arithmetic diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1074,6 +1074,24 @@ self.broadcast_address.is_loopback) +class _BaseInterface: + + """ Base Interface object. + + The following methods apply to both IPv4 and IPv6 interfaces. + """ + + def __add__(self, other): + if not isinstance(other, int): + return NotImplemented + return self.__class__((int(self) + other, self._prefixlen)) + + def __sub__(self, other): + if not isinstance(other, int): + return NotImplemented + return self.__class__((int(self) - other, self._prefixlen)) + + class _BaseV4: """Base IPv4 object. @@ -1382,7 +1400,7 @@ return self in self._constants._linklocal_network -class IPv4Interface(IPv4Address): +class IPv4Interface(_BaseInterface, IPv4Address): def __init__(self, address): if isinstance(address, (bytes, int)): @@ -2074,7 +2092,7 @@ return IPv4Address((self._ip >> 80) & 0xFFFFFFFF) -class IPv6Interface(IPv6Address): +class IPv6Interface(_BaseInterface, IPv6Address): def __init__(self, address): if isinstance(address, (bytes, int)):