Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8517ad

Browse files
committed
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky).
1 parent 749bd42 commit a8517ad

3 files changed

Lines changed: 72 additions & 30 deletions

File tree

Doc/library/ipaddress.rst

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -642,32 +642,73 @@ Interface objects
642642

643643
.. class:: IPv4Interface(address)
644644

645-
Construct an IPv4 interface. *address* is a string or integer representing
646-
the IP interface. An :exc:`AddressValueError` is raised if *address* is not
647-
a valid IPv4 address.
645+
Construct an IPv4 interface. The meaning of *address* is as in the
646+
constructor of :class:`IPv4Network`, except that arbitrary host addresses
647+
are always accepted.
648648

649-
The network address for the interface is determined by calling
650-
``IPv4Network(address, strict=False)``.
649+
:class:`IPv4Interface` is a subclass of :class:`IPv4Address`, so it inherits
650+
all the attributes from that class. In addition, the following attributes
651+
are available:
651652

652-
>>> ipaddress.IPv4Interface('192.168.0.0/24')
653-
IPv4Interface('192.168.0.0/24')
654-
>>> ipaddress.IPv4Interface('192.168.0.0/24').network
655-
IPv4Network('192.168.0.0/24')
653+
.. attribute:: ip
654+
655+
The address (:class:`IPv4Address`) without network information.
656+
657+
>>> interface = IPv4Interface('192.0.2.5/24')
658+
>>> interface.ip
659+
IPv4Address('192.0.2.5')
660+
661+
.. attribute:: network
662+
663+
The network (:class:`IPv4Network`) this interface belongs to.
664+
665+
>>> interface = IPv4Interface('192.0.2.5/24')
666+
>>> interface.network
667+
IPv4Network('192.0.2.0/24')
668+
669+
.. attribute:: with_prefixlen
670+
671+
A string representation of the interface with the mask in prefix notation.
672+
673+
>>> interface = IPv4Interface('192.0.2.5/24')
674+
>>> interface.with_prefixlen
675+
'192.0.2.5/24'
676+
677+
.. attribute:: with_netmask
678+
679+
A string representation of the interface with the network as a net mask.
680+
681+
>>> interface = IPv4Interface('192.0.2.5/24')
682+
>>> interface.with_netmask
683+
'192.0.2.5/255.255.255.0'
684+
685+
.. attribute:: with_hostmask
686+
687+
A string representation of the interface with the network as a host mask.
688+
689+
>>> interface = IPv4Interface('192.0.2.5/24')
690+
>>> interface.with_hostmask
691+
'192.0.2.5/0.0.0.255'
656692

657693

658694
.. class:: IPv6Interface(address)
659695

660-
Construct an IPv6 interface. *address* is a string or integer representing
661-
the IP interface. An :exc:`AddressValueError` is raised if *address* is not
662-
a valid IPv6 address.
696+
Construct an IPv6 interface. The meaning of *address* is as in the
697+
constructor of :class:`IPv6Network`, except that arbitrary host addresses
698+
are always accepted.
663699

664-
The network address for the interface is determined by calling
665-
``IPv6Network(address, strict=False)``.
700+
:class:`IPv6Interface` is a subclass of :class:`IPv6Address`, so it inherits
701+
all the attributes from that class. In addition, the following attributes
702+
are available:
666703

667-
>>> ipaddress.IPv6Interface('2001:db8::1000/96')
668-
IPv6Interface('2001:db8::1000/96')
669-
>>> ipaddress.IPv6Interface('2001:db8::1000/96').network
670-
IPv6Network('2001:db8::/96')
704+
.. attribute:: ip
705+
.. attribute:: network
706+
.. attribute:: with_prefixlen
707+
.. attribute:: with_netmask
708+
.. attribute:: with_hostmask
709+
710+
Refer to the corresponding attribute documentation in
711+
:class:`IPv4Interface`.
671712

672713

673714
Other Module Level Functions

Lib/ipaddress.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,8 @@ def ip(self):
13361336

13371337
@property
13381338
def with_prefixlen(self):
1339-
return self
1339+
return '%s/%s' % (self._string_from_ip_int(self._ip),
1340+
self._prefixlen)
13401341

13411342
@property
13421343
def with_netmask(self):
@@ -1948,11 +1949,13 @@ def ip(self):
19481949

19491950
@property
19501951
def with_prefixlen(self):
1951-
return self
1952+
return '%s/%s' % (self._string_from_ip_int(self._ip),
1953+
self._prefixlen)
19521954

19531955
@property
19541956
def with_netmask(self):
1955-
return self.with_prefixlen
1957+
return '%s/%s' % (self._string_from_ip_int(self._ip),
1958+
self.netmask)
19561959

19571960
@property
19581961
def with_hostmask(self):

Lib/test/test_ipaddress.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,21 +1558,19 @@ def testForceVersion(self):
15581558
self.assertEqual(ipaddress.IPv6Network(1).version, 6)
15591559

15601560
def testWithStar(self):
1561-
self.assertEqual(str(self.ipv4_interface.with_prefixlen), "1.2.3.4/24")
1562-
self.assertEqual(str(self.ipv4_interface.with_netmask),
1561+
self.assertEqual(self.ipv4_interface.with_prefixlen, "1.2.3.4/24")
1562+
self.assertEqual(self.ipv4_interface.with_netmask,
15631563
"1.2.3.4/255.255.255.0")
1564-
self.assertEqual(str(self.ipv4_interface.with_hostmask),
1564+
self.assertEqual(self.ipv4_interface.with_hostmask,
15651565
"1.2.3.4/0.0.0.255")
15661566

1567-
self.assertEqual(str(self.ipv6_interface.with_prefixlen),
1568-
'2001:658:22a:cafe:200::1/64')
1569-
# rfc3513 sec 2.3 says that ipv6 only uses cidr notation for
1570-
# subnets
1571-
self.assertEqual(str(self.ipv6_interface.with_netmask),
1567+
self.assertEqual(self.ipv6_interface.with_prefixlen,
15721568
'2001:658:22a:cafe:200::1/64')
1569+
self.assertEqual(self.ipv6_interface.with_netmask,
1570+
'2001:658:22a:cafe:200::1/ffff:ffff:ffff:ffff::')
15731571
# this probably don't make much sense, but it's included for
15741572
# compatibility with ipv4
1575-
self.assertEqual(str(self.ipv6_interface.with_hostmask),
1573+
self.assertEqual(self.ipv6_interface.with_hostmask,
15761574
'2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff')
15771575

15781576
def testNetworkElementCaching(self):

0 commit comments

Comments
 (0)