cpython: 258558e36d8a (original) (raw)
Mercurial > cpython
changeset 78661:258558e36d8a
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky). [#14814]
Nick Coghlan ncoghlan@gmail.com | |
---|---|
date | Mon, 20 Aug 2012 10:04:26 +1000 |
parents | 4feb10457c13 |
children | 811d91591f73 |
files | Doc/library/ipaddress.rst Lib/ipaddress.py Lib/test/test_ipaddress.py |
diffstat | 3 files changed, 72 insertions(+), 30 deletions(-)[+] [-] Doc/library/ipaddress.rst 77 Lib/ipaddress.py 9 Lib/test/test_ipaddress.py 16 |
line wrap: on
line diff
--- a/Doc/library/ipaddress.rst +++ b/Doc/library/ipaddress.rst @@ -642,32 +642,73 @@ Interface objects .. class:: IPv4Interface(address)
- Construct an IPv4 interface. address is a string or integer representing
- the IP interface. An :exc:
AddressValueError
is raised if address is not - a valid IPv4 address.
- Construct an IPv4 interface. The meaning of address is as in the
- constructor of :class:
IPv4Network
, except that arbitrary host addresses - are always accepted. +
- :class:
IPv4Interface
is a subclass of :class:IPv4Address
, so it inherits - all the attributes from that class. In addition, the following attributes
- are available: +
- .. attribute:: ip +
The address (:class:`IPv4Address`) without network information.[](#l1.20)
>>> interface = IPv4Interface('192.0.2.5/24')[](#l1.22)
>>> interface.ip[](#l1.23)
IPv4Address('192.0.2.5')[](#l1.24)
>>> interface = IPv4Interface('192.0.2.5/24')[](#l1.30)
>>> interface.network[](#l1.31)
IPv4Network('192.0.2.0/24')[](#l1.32)
- .. attribute:: with_prefixlen +
A string representation of the interface with the mask in prefix notation.[](#l1.38)
>>> interface = IPv4Interface('192.0.2.5/24')[](#l1.40)
>>> interface.with_prefixlen[](#l1.41)
'192.0.2.5/24'[](#l1.42)
- .. attribute:: with_netmask +
A string representation of the interface with the network as a net mask.[](#l1.46)
>>> interface = IPv4Interface('192.0.2.5/24')[](#l1.52)
>>> interface.with_netmask[](#l1.53)
'192.0.2.5/255.255.255.0'[](#l1.54)
- .. attribute:: with_hostmask +
A string representation of the interface with the network as a host mask.[](#l1.58)
>>> interface = IPv4Interface('192.0.2.5/24')[](#l1.60)
>>> interface.with_hostmask[](#l1.61)
'192.0.2.5/0.0.0.255'[](#l1.62)
.. class:: IPv6Interface(address)
- Construct an IPv6 interface. address is a string or integer representing
- the IP interface. An :exc:
AddressValueError
is raised if address is not - a valid IPv6 address.
- Construct an IPv6 interface. The meaning of address is as in the
- constructor of :class:
IPv6Network
, except that arbitrary host addresses - are always accepted. +
- :class:
IPv6Interface
is a subclass of :class:IPv6Address
, so it inherits - all the attributes from that class. In addition, the following attributes
- are available:
- .. attribute:: ip
- .. attribute:: network
- .. attribute:: with_prefixlen
- .. attribute:: with_netmask
- .. attribute:: with_hostmask
Refer to the corresponding attribute documentation in[](#l1.90)
:class:`IPv4Interface`.[](#l1.91)
--- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1336,7 +1336,8 @@ class IPv4Interface(IPv4Address): @property def with_prefixlen(self):
return self[](#l2.7)
return '%s/%s' % (self._string_from_ip_int(self._ip),[](#l2.8)
self._prefixlen)[](#l2.9)
@property def with_netmask(self): @@ -1948,11 +1949,13 @@ class IPv6Interface(IPv6Address): @property def with_prefixlen(self):
return self[](#l2.17)
return '%s/%s' % (self._string_from_ip_int(self._ip),[](#l2.18)
self._prefixlen)[](#l2.19)
@property def with_netmask(self):
return self.with_prefixlen[](#l2.23)
return '%s/%s' % (self._string_from_ip_int(self._ip),[](#l2.24)
self.netmask)[](#l2.25)
@property def with_hostmask(self):
--- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1558,21 +1558,19 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEqual(ipaddress.IPv6Network(1).version, 6) def testWithStar(self):
self.assertEqual(str(self.ipv4_interface.with_prefixlen), "1.2.3.4/24")[](#l3.7)
self.assertEqual(str(self.ipv4_interface.with_netmask),[](#l3.8)
self.assertEqual(self.ipv4_interface.with_prefixlen, "1.2.3.4/24")[](#l3.9)
self.assertEqual(self.ipv4_interface.with_netmask,[](#l3.10) "1.2.3.4/255.255.255.0")[](#l3.11)
self.assertEqual(str(self.ipv4_interface.with_hostmask),[](#l3.12)
self.assertEqual(self.ipv4_interface.with_hostmask,[](#l3.13) "1.2.3.4/0.0.0.255")[](#l3.14)
self.assertEqual(str(self.ipv6_interface.with_prefixlen),[](#l3.16)
self.assertEqual(self.ipv6_interface.with_prefixlen,[](#l3.17) '2001:658:22a:cafe:200::1/64')[](#l3.18)
# rfc3513 sec 2.3 says that ipv6 only uses cidr notation for[](#l3.19)
# subnets[](#l3.20)
self.assertEqual(str(self.ipv6_interface.with_netmask),[](#l3.21)
'2001:658:22a:cafe:200::1/64')[](#l3.22)
self.assertEqual(self.ipv6_interface.with_netmask,[](#l3.23)
'2001:658:22a:cafe:200::1/ffff:ffff:ffff:ffff::')[](#l3.24) # this probably don't make much sense, but it's included for[](#l3.25) # compatibility with ipv4[](#l3.26)
self.assertEqual(str(self.ipv6_interface.with_hostmask),[](#l3.27)
self.assertEqual(self.ipv6_interface.with_hostmask,[](#l3.28) '2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff')[](#l3.29)