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

Skip to content

Commit 948af23

Browse files
committed
Issue #15888: fixing problems in ipaddress doctests. Patch by Chris Jerdonek
1 parent f4c2757 commit 948af23

3 files changed

Lines changed: 34 additions & 21 deletions

File tree

Doc/howto/ipaddress.rst

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _ipaddress-howto:
22

33
***************************************
4-
An Introduction to the ipaddress module
4+
An introduction to the ipaddress module
55
***************************************
66

77
:author: Peter Moody
@@ -47,7 +47,12 @@ Addresses, often referred to as "host addresses" are the most basic unit
4747
when working with IP addressing. The simplest way to create addresses is
4848
to use the :func:`ipaddress.ip_address` factory function, which automatically
4949
determines whether to create an IPv4 or IPv6 address based on the passed in
50-
value::
50+
value:
51+
52+
.. testsetup::
53+
>>> import ipaddress
54+
55+
::
5156

5257
>>> ipaddress.ip_address('192.0.2.1')
5358
IPv4Address('192.0.2.1')
@@ -142,7 +147,7 @@ address.
142147

143148
>>> ipaddress.ip_interface('192.0.2.1/24')
144149
IPv4Interface('192.0.2.1/24')
145-
>>> ipaddress.ip_network('2001:db8::1/96')
150+
>>> ipaddress.ip_interface('2001:db8::1/96')
146151
IPv6Interface('2001:db8::1/96')
147152

148153
Integer inputs are accepted (as with networks), and use of a particular IP
@@ -177,22 +182,22 @@ Obtaining the network from an interface::
177182
Finding out how many individual addresses are in a network::
178183

179184
>>> net4 = ipaddress.ip_network('192.0.2.0/24')
180-
>>> net4.numhosts
185+
>>> net4.num_addresses
181186
256
182187
>>> net6 = ipaddress.ip_network('2001:db8::0/96')
183-
>>> net6.numhosts
188+
>>> net6.num_addresses
184189
4294967296
185190

186191
Iterating through the "usable" addresses on a network::
187192

188193
>>> net4 = ipaddress.ip_network('192.0.2.0/24')
189194
>>> for x in net4.hosts():
190-
print(x)
195+
... print(x) # doctest: +ELLIPSIS
191196
192.0.2.1
192197
192.0.2.2
193198
192.0.2.3
194199
192.0.2.4
195-
<snip>
200+
...
196201
192.0.2.252
197202
192.0.2.253
198203
192.0.2.254
@@ -216,9 +221,9 @@ the hostmask (any bits that are not part of the netmask):
216221
Exploding or compressing the address::
217222

218223
>>> addr6.exploded
219-
'2001:0db8:0000:0000:0000:0000:0000:0000'
224+
'2001:0db8:0000:0000:0000:0000:0000:0001'
220225
>>> addr6.compressed
221-
'2001:db8::'
226+
'2001:db8::1'
222227
>>> net6.exploded
223228
'2001:0db8:0000:0000:0000:0000:0000:0000/96'
224229
>>> net6.compressed
@@ -241,9 +246,9 @@ to index them like this::
241246
>>> net4[-1]
242247
IPv4Address('192.0.2.255')
243248
>>> net6[1]
244-
IPv6Address('2001::1')
249+
IPv6Address('2001:db8::1')
245250
>>> net6[-1]
246-
IPv6Address('2001::ffff:ffff')
251+
IPv6Address('2001:db8::ffff:ffff')
247252

248253

249254
It also means that network objects lend themselves to using the list

Doc/library/ipaddress.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ IP addresses, networks and interfaces:
4242
Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on
4343
the IP address passed as argument. Either IPv4 or IPv6 addresses may be
4444
supplied; integers less than 2**32 will be considered to be IPv4 by default.
45-
A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
46-
IPv6 address.
45+
A :exc:`ValueError` is raised if *address* does not represent a valid IPv4
46+
or IPv6 address.
47+
48+
.. testsetup::
49+
>>> import ipaddress
50+
>>> from ipaddress import (ip_network, IPv4Address, IPv4Interface,
51+
... IPv4Network)
52+
53+
::
4754

4855
>>> ipaddress.ip_address('192.168.0.1')
4956
IPv4Address('192.168.0.1')
@@ -111,7 +118,7 @@ write code that handles both IP versions correctly.
111118

112119
>>> ipaddress.IPv4Address('192.168.0.1')
113120
IPv4Address('192.168.0.1')
114-
>>> ipaddress.IPv4Address(3221225985)
121+
>>> ipaddress.IPv4Address(3232235521)
115122
IPv4Address('192.168.0.1')
116123
>>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
117124
IPv4Address('192.168.0.1')
@@ -437,7 +444,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
437444
hosts are all the IP addresses that belong to the network, except the
438445
network address itself and the network broadcast address.
439446

440-
>>> list(ip_network('192.0.2.0/29').hosts())
447+
>>> list(ip_network('192.0.2.0/29').hosts()) #doctest: +NORMALIZE_WHITESPACE
441448
[IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
442449
IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
443450
IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
@@ -456,7 +463,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
456463

457464
>>> n1 = ip_network('192.0.2.0/28')
458465
>>> n2 = ip_network('192.0.2.1/32')
459-
>>> list(n1.address_exclude(n2))
466+
>>> list(n1.address_exclude(n2)) #doctest: +NORMALIZE_WHITESPACE
460467
[IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
461468
IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
462469

@@ -471,10 +478,10 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
471478

472479
>>> list(ip_network('192.0.2.0/24').subnets())
473480
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
474-
>>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2))
481+
>>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2)) #doctest: +NORMALIZE_WHITESPACE
475482
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
476483
IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
477-
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26))
484+
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26)) #doctest: +NORMALIZE_WHITESPACE
478485
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
479486
IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
480487
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))

Lib/ipaddress.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,11 @@ def summarize_address_range(first, last):
206206
"""Summarize a network range given the first and last IP addresses.
207207
208208
Example:
209-
>>> summarize_address_range(IPv4Address('192.0.2.0'),
210-
IPv4Address('192.0.2.130'))
209+
>>> list(summarize_address_range(IPv4Address('192.0.2.0'),
210+
... IPv4Address('192.0.2.130')))
211+
... #doctest: +NORMALIZE_WHITESPACE
211212
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'),
212-
IPv4Network('192.0.2.130/32')]
213+
IPv4Network('192.0.2.130/32')]
213214
214215
Args:
215216
first: the first IPv4Address or IPv6Address in the range.

0 commit comments

Comments
 (0)