Skip to content

Commit 6e5055e

Browse files
gh-152753: Fix memory leak in ipaddress properties by switching to cached_property
1 parent b52bc56 commit 6e5055e

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

Lib/ipaddress.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
12641264

12651265
"""Represent and manipulate single IPv4 Addresses."""
12661266

1267-
__slots__ = ('_ip', '__weakref__')
1267+
__slots__ = ('_ip', '__weakref__', '__dict__')
12681268

12691269
def __init__(self, address):
12701270

@@ -1317,8 +1317,7 @@ def is_reserved(self):
13171317
"""
13181318
return self in self._constants._reserved_network
13191319

1320-
@property
1321-
@functools.lru_cache()
1320+
@functools.cached_property
13221321
def is_private(self):
13231322
"""``True`` if the address is defined as not globally reachable by
13241323
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
@@ -1339,8 +1338,7 @@ def is_private(self):
13391338
and all(self not in net for net in self._constants._private_networks_exceptions)
13401339
)
13411340

1342-
@property
1343-
@functools.lru_cache()
1341+
@functools.cached_property
13441342
def is_global(self):
13451343
"""``True`` if the address is defined as globally reachable by
13461344
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
@@ -1548,8 +1546,7 @@ def __init__(self, address, strict=True):
15481546
elif self._prefixlen == (self.max_prefixlen):
15491547
self.hosts = lambda: iter((IPv4Address(addr),))
15501548

1551-
@property
1552-
@functools.lru_cache()
1549+
@functools.cached_property
15531550
def is_global(self):
15541551
"""Test if this address is allocated for public networks.
15551552
@@ -1912,7 +1909,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
19121909

19131910
"""Represent and manipulate single IPv6 Addresses."""
19141911

1915-
__slots__ = ('_ip', '_scope_id', '__weakref__')
1912+
__slots__ = ('_ip', '_scope_id', '__weakref__', '__dict__')
19161913

19171914
def __init__(self, address):
19181915
"""Instantiate a new IPv6 address object.
@@ -2085,8 +2082,7 @@ def is_site_local(self):
20852082
"""
20862083
return self in self._constants._sitelocal_network
20872084

2088-
@property
2089-
@functools.lru_cache()
2085+
@functools.cached_property
20902086
def is_private(self):
20912087
"""``True`` if the address is defined as not globally reachable by
20922088
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
@@ -2424,4 +2420,4 @@ def __getattr__(name):
24242420

24252421
_deprecated("__version__", remove=(3, 20))
24262422
return "1.0" # Do not change
2427-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
2423+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

0 commit comments

Comments
 (0)