Skip to content

Commit 2a4cbf1

Browse files
jstasiakencukou
andauthored
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 e2fcaf1 commit 2a4cbf1

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

Doc/library/ipaddress.rst

+23-4
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

+45-13
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
@@ -2049,13 +2069,19 @@ def is_site_local(self):
20492069
@property
20502070
@functools.lru_cache()
20512071
def is_private(self):
2052-
"""Test if this address is allocated for private networks.
2072+
"""``True`` if the address is defined as not globally reachable by
2073+
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
2074+
(for IPv6) with the following exceptions:
20532075
2054-
Returns:
2055-
A boolean, True if the address is reserved per
2056-
iana-ipv6-special-registry, or is ipv4_mapped and is
2057-
reserved in the iana-ipv4-special-registry.
2076+
* ``is_private`` is ``False`` for ``100.64.0.0/10``
2077+
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
2078+
semantics of the underlying IPv4 addresses and the following condition holds
2079+
(see :attr:`IPv6Address.ipv4_mapped`)::
2080+
2081+
address.is_private == address.ipv4_mapped.is_private
20582082
2083+
``is_private`` has value opposite to :attr:`is_global`, except for the ``100.64.0.0/10``
2084+
IPv4 range where they are both ``False``.
20592085
"""
20602086
ipv4_mapped = self.ipv4_mapped
20612087
if ipv4_mapped is not None:
@@ -2064,12 +2090,18 @@ def is_private(self):
20642090

20652091
@property
20662092
def is_global(self):
2067-
"""Test if this address is allocated for public networks.
2093+
"""``True`` if the address is defined as globally reachable by
2094+
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
2095+
(for IPv6) with the following exception:
20682096
2069-
Returns:
2070-
A boolean, true if the address is not reserved per
2071-
iana-ipv6-special-registry.
2097+
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
2098+
semantics of the underlying IPv4 addresses and the following condition holds
2099+
(see :attr:`IPv6Address.ipv4_mapped`)::
2100+
2101+
address.is_global == address.ipv4_mapped.is_global
20722102
2103+
``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
2104+
IPv4 range where they are both ``False``.
20732105
"""
20742106
return not self.is_private
20752107

0 commit comments

Comments
 (0)