Skip to content

Commit 12cf493

Browse files
jstasiakencukou
andcommitted
pythonGH-65056: Improve the IP address' is_global/is_private documentation (pythonGH-113186)
* pythonGH-65056: Improve the IP address' is_global/is_private documentation It wasn't clear what the semantics of is_global/is_private are and, when one gets to the bottom of it, it's not quite so simple (hence the exceptions listed). Co-authored-by: Petr Viktorin <[email protected]>
1 parent 30725f0 commit 12cf493

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

Doc/library/ipaddress.rst

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,34 @@ write code that handles both IP versions correctly. Address objects are
178178

179179
.. attribute:: is_private
180180

181-
``True`` if the address is allocated for private networks. See
181+
``True`` if the address is defined as not globally reachable by
182182
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
183-
(for IPv6).
183+
(for IPv6) with the following exceptions:
184+
185+
* ``is_private`` is ``False`` for the shared address space (``100.64.0.0/10``)
186+
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
187+
semantics of the underlying IPv4 addresses and the following condition holds
188+
(see :attr:`IPv6Address.ipv4_mapped`)::
189+
190+
address.is_private == address.ipv4_mapped.is_private
191+
192+
``is_private`` has value opposite to :attr:`is_global`, except for the shared address space
193+
(``100.64.0.0/10`` range) where they are both ``False``.
184194

185195
.. attribute:: is_global
186196

187-
``True`` if the address is allocated for public networks. See
197+
``True`` if the address is defined as globally reachable by
188198
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
189-
(for IPv6).
199+
(for IPv6) with the following exception:
200+
201+
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
202+
semantics of the underlying IPv4 addresses and the following condition holds
203+
(see :attr:`IPv6Address.ipv4_mapped`)::
204+
205+
address.is_global == address.ipv4_mapped.is_global
206+
207+
``is_global`` has value opposite to :attr:`is_private`, except for the shared address space
208+
(``100.64.0.0/10`` range) where they are both ``False``.
190209

191210
.. versionadded:: 3.4
192211

Lib/ipaddress.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,18 +1333,38 @@ def is_reserved(self):
13331333
@property
13341334
@functools.lru_cache()
13351335
def is_private(self):
1336-
"""Test if this address is allocated for private networks.
1336+
"""``True`` if the address is defined as not globally reachable by
1337+
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
1338+
(for IPv6) with the following exceptions:
13371339
1338-
Returns:
1339-
A boolean, True if the address is reserved per
1340-
iana-ipv4-special-registry.
1340+
* ``is_private`` is ``False`` for ``100.64.0.0/10``
1341+
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
1342+
semantics of the underlying IPv4 addresses and the following condition holds
1343+
(see :attr:`IPv6Address.ipv4_mapped`)::
1344+
1345+
address.is_private == address.ipv4_mapped.is_private
13411346
1347+
``is_private`` has value opposite to :attr:`is_global`, except for the ``100.64.0.0/10``
1348+
IPv4 range where they are both ``False``.
13421349
"""
13431350
return any(self in net for net in self._constants._private_networks)
13441351

13451352
@property
13461353
@functools.lru_cache()
13471354
def is_global(self):
1355+
"""``True`` if the address is defined as globally reachable by
1356+
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
1357+
(for IPv6) with the following exception:
1358+
1359+
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
1360+
semantics of the underlying IPv4 addresses and the following condition holds
1361+
(see :attr:`IPv6Address.ipv4_mapped`)::
1362+
1363+
address.is_global == address.ipv4_mapped.is_global
1364+
1365+
``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
1366+
IPv4 range where they are both ``False``.
1367+
"""
13481368
return self not in self._constants._public_network and not self.is_private
13491369

13501370
@property
@@ -2007,13 +2027,19 @@ def is_site_local(self):
20072027
@property
20082028
@functools.lru_cache()
20092029
def is_private(self):
2010-
"""Test if this address is allocated for private networks.
2030+
"""``True`` if the address is defined as not globally reachable by
2031+
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
2032+
(for IPv6) with the following exceptions:
20112033
2012-
Returns:
2013-
A boolean, True if the address is reserved per
2014-
iana-ipv6-special-registry, or is ipv4_mapped and is
2015-
reserved in the iana-ipv4-special-registry.
2034+
* ``is_private`` is ``False`` for ``100.64.0.0/10``
2035+
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
2036+
semantics of the underlying IPv4 addresses and the following condition holds
2037+
(see :attr:`IPv6Address.ipv4_mapped`)::
2038+
2039+
address.is_private == address.ipv4_mapped.is_private
20162040
2041+
``is_private`` has value opposite to :attr:`is_global`, except for the ``100.64.0.0/10``
2042+
IPv4 range where they are both ``False``.
20172043
"""
20182044
ipv4_mapped = self.ipv4_mapped
20192045
if ipv4_mapped is not None:
@@ -2022,12 +2048,18 @@ def is_private(self):
20222048

20232049
@property
20242050
def is_global(self):
2025-
"""Test if this address is allocated for public networks.
2051+
"""``True`` if the address is defined as globally reachable by
2052+
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
2053+
(for IPv6) with the following exception:
20262054
2027-
Returns:
2028-
A boolean, true if the address is not reserved per
2029-
iana-ipv6-special-registry.
2055+
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
2056+
semantics of the underlying IPv4 addresses and the following condition holds
2057+
(see :attr:`IPv6Address.ipv4_mapped`)::
2058+
2059+
address.is_global == address.ipv4_mapped.is_global
20302060
2061+
``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
2062+
IPv4 range where they are both ``False``.
20312063
"""
20322064
return not self.is_private
20332065

0 commit comments

Comments
 (0)