Skip to content

Commit e5019d5

Browse files
author
Peter Moody
committed
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
1 parent a46079e commit e5019d5

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

Doc/library/ipaddress.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ write code that handles both IP versions correctly.
158158
``True`` if the address is reserved for multicast use. See
159159
:RFC:`3171` (for IPv4) or :RFC:`2373` (for IPv6).
160160

161+
.. attribute:: is_private
162+
163+
``True`` if the address is allocated for private networks. See
164+
iana-ipv4-special-registry (for IPv4) or iana-ipv6-special-registry
165+
(for IPv6).
166+
161167
.. attribute:: is_global
162168

163169
``True`` if the address is allocated for public networks. See

Lib/ipaddress.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,6 @@ def is_private(self):
12441244
"""
12451245
return (self in IPv4Network('0.0.0.0/8') or
12461246
self in IPv4Network('10.0.0.0/8') or
1247-
self in IPv4Network('100.64.0.0/10') or
12481247
self in IPv4Network('127.0.0.0/8') or
12491248
self in IPv4Network('169.254.0.0/16') or
12501249
self in IPv4Network('172.16.0.0/12') or
@@ -1258,17 +1257,6 @@ def is_private(self):
12581257
self in IPv4Network('240.0.0.0/4') or
12591258
self in IPv4Network('255.255.255.255/32'))
12601259

1261-
@property
1262-
def is_global(self):
1263-
"""Test if this address is allocated for public networks.
1264-
1265-
Returns:
1266-
A boolean, True if the address is not reserved per
1267-
iana-ipv4-special-registry.
1268-
1269-
"""
1270-
return self in IPv4Network('100.64.0.0/10') or not self.is_private
1271-
12721260

12731261
@property
12741262
def is_multicast(self):
@@ -1501,6 +1489,21 @@ def __init__(self, address, strict=True):
15011489
if self._prefixlen == (self._max_prefixlen - 1):
15021490
self.hosts = self.__iter__
15031491

1492+
@property
1493+
@functools.lru_cache()
1494+
def is_global(self):
1495+
"""Test if this address is allocated for public networks.
1496+
1497+
Returns:
1498+
A boolean, True if the address is not reserved per
1499+
iana-ipv4-special-registry.
1500+
1501+
"""
1502+
return (not (self.network_address in IPv4Network('100.64.0.0/10') and
1503+
self.broadcast_address in IPv4Network('100.64.0.0/10')) and
1504+
not self.is_private)
1505+
1506+
15041507

15051508
class _BaseV6:
15061509

Lib/test/test_ipaddress.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,10 @@ def testReservedIpv4(self):
13191319
self.assertEqual(True, ipaddress.ip_network(
13201320
'127.42.0.0/16').is_loopback)
13211321
self.assertEqual(False, ipaddress.ip_network('128.0.0.0').is_loopback)
1322-
self.assertEqual(True, ipaddress.ip_network('100.64.0.0/10').is_private)
1322+
self.assertEqual(False,
1323+
ipaddress.ip_network('100.64.0.0/10').is_private)
13231324
self.assertEqual(False, ipaddress.ip_network('100.64.0.0/10').is_global)
1325+
13241326
self.assertEqual(True,
13251327
ipaddress.ip_network('192.0.2.128/25').is_private)
13261328
self.assertEqual(True,

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Core and Builtins
1919
Library
2020
-------
2121

22+
- Issue #17400: New 'is_global' attribute for ipaddress to tell if an address
23+
is allocated by IANA for global or private networks.
24+
2225
- Issue #19350: Increasing the test coverage of macurl2path. Patch by Colin
2326
Williams.
2427

0 commit comments

Comments
 (0)