Kaydet (Commit) 88f64f39 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #23103: Reduced the memory consumption of IPv4Address and IPv6Address.

üst a5f3ad8c
...@@ -386,6 +386,8 @@ class _IPAddressBase: ...@@ -386,6 +386,8 @@ class _IPAddressBase:
"""The mother class.""" """The mother class."""
__slots__ = ()
@property @property
def exploded(self): def exploded(self):
"""Return the longhand version of the IP address as a string.""" """Return the longhand version of the IP address as a string."""
...@@ -543,6 +545,8 @@ class _BaseAddress(_IPAddressBase): ...@@ -543,6 +545,8 @@ class _BaseAddress(_IPAddressBase):
used by single IP addresses. used by single IP addresses.
""" """
__slots__ = ()
def __int__(self): def __int__(self):
return self._ip return self._ip
...@@ -1051,6 +1055,8 @@ class _BaseV4: ...@@ -1051,6 +1055,8 @@ class _BaseV4:
""" """
__slots__ = ()
_version = 4
# Equivalent to 255.255.255.255 or 32 bits of 1's. # Equivalent to 255.255.255.255 or 32 bits of 1's.
_ALL_ONES = (2**IPV4LENGTH) - 1 _ALL_ONES = (2**IPV4LENGTH) - 1
_DECIMAL_DIGITS = frozenset('0123456789') _DECIMAL_DIGITS = frozenset('0123456789')
...@@ -1063,9 +1069,6 @@ class _BaseV4: ...@@ -1063,9 +1069,6 @@ class _BaseV4:
# when constructed (see _make_netmask()). # when constructed (see _make_netmask()).
_netmask_cache = {} _netmask_cache = {}
def __init__(self, address):
self._version = 4
def _explode_shorthand_ip_string(self): def _explode_shorthand_ip_string(self):
return str(self) return str(self)
...@@ -1243,6 +1246,8 @@ class IPv4Address(_BaseV4, _BaseAddress): ...@@ -1243,6 +1246,8 @@ class IPv4Address(_BaseV4, _BaseAddress):
"""Represent and manipulate single IPv4 Addresses.""" """Represent and manipulate single IPv4 Addresses."""
__slots__ = ('_ip', '__weakref__')
def __init__(self, address): def __init__(self, address):
""" """
...@@ -1259,8 +1264,6 @@ class IPv4Address(_BaseV4, _BaseAddress): ...@@ -1259,8 +1264,6 @@ class IPv4Address(_BaseV4, _BaseAddress):
AddressValueError: If ipaddress isn't a valid IPv4 address. AddressValueError: If ipaddress isn't a valid IPv4 address.
""" """
_BaseV4.__init__(self, address)
# Efficient constructor from integer. # Efficient constructor from integer.
if isinstance(address, int): if isinstance(address, int):
self._check_int_address(address) self._check_int_address(address)
...@@ -1485,8 +1488,6 @@ class IPv4Network(_BaseV4, _BaseNetwork): ...@@ -1485,8 +1488,6 @@ class IPv4Network(_BaseV4, _BaseNetwork):
supplied. supplied.
""" """
_BaseV4.__init__(self, address)
_BaseNetwork.__init__(self, address) _BaseNetwork.__init__(self, address)
# Constructing from a packed address or integer # Constructing from a packed address or integer
...@@ -1590,6 +1591,8 @@ class _BaseV6: ...@@ -1590,6 +1591,8 @@ class _BaseV6:
""" """
__slots__ = ()
_version = 6
_ALL_ONES = (2**IPV6LENGTH) - 1 _ALL_ONES = (2**IPV6LENGTH) - 1
_HEXTET_COUNT = 8 _HEXTET_COUNT = 8
_HEX_DIGITS = frozenset('0123456789ABCDEFabcdef') _HEX_DIGITS = frozenset('0123456789ABCDEFabcdef')
...@@ -1599,9 +1602,6 @@ class _BaseV6: ...@@ -1599,9 +1602,6 @@ class _BaseV6:
# when constructed (see _make_netmask()). # when constructed (see _make_netmask()).
_netmask_cache = {} _netmask_cache = {}
def __init__(self, address):
self._version = 6
@classmethod @classmethod
def _make_netmask(cls, arg): def _make_netmask(cls, arg):
"""Make a (netmask, prefix_len) tuple from the given argument. """Make a (netmask, prefix_len) tuple from the given argument.
...@@ -1870,6 +1870,8 @@ class IPv6Address(_BaseV6, _BaseAddress): ...@@ -1870,6 +1870,8 @@ class IPv6Address(_BaseV6, _BaseAddress):
"""Represent and manipulate single IPv6 Addresses.""" """Represent and manipulate single IPv6 Addresses."""
__slots__ = ('_ip', '__weakref__')
def __init__(self, address): def __init__(self, address):
"""Instantiate a new IPv6 address object. """Instantiate a new IPv6 address object.
...@@ -1887,8 +1889,6 @@ class IPv6Address(_BaseV6, _BaseAddress): ...@@ -1887,8 +1889,6 @@ class IPv6Address(_BaseV6, _BaseAddress):
AddressValueError: If address isn't a valid IPv6 address. AddressValueError: If address isn't a valid IPv6 address.
""" """
_BaseV6.__init__(self, address)
# Efficient constructor from integer. # Efficient constructor from integer.
if isinstance(address, int): if isinstance(address, int):
self._check_int_address(address) self._check_int_address(address)
...@@ -2180,7 +2180,6 @@ class IPv6Network(_BaseV6, _BaseNetwork): ...@@ -2180,7 +2180,6 @@ class IPv6Network(_BaseV6, _BaseNetwork):
supplied. supplied.
""" """
_BaseV6.__init__(self, address)
_BaseNetwork.__init__(self, address) _BaseNetwork.__init__(self, address)
# Efficient constructor from integer or packed address # Efficient constructor from integer or packed address
......
...@@ -11,6 +11,7 @@ import functools ...@@ -11,6 +11,7 @@ import functools
import operator import operator
import pickle import pickle
import ipaddress import ipaddress
import weakref
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
...@@ -259,6 +260,9 @@ class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4): ...@@ -259,6 +260,9 @@ class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4):
def test_pickle(self): def test_pickle(self):
self.pickle_test('192.0.2.1') self.pickle_test('192.0.2.1')
def test_weakref(self):
weakref.ref(self.factory('192.0.2.1'))
class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6): class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
factory = ipaddress.IPv6Address factory = ipaddress.IPv6Address
...@@ -394,6 +398,9 @@ class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6): ...@@ -394,6 +398,9 @@ class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
def test_pickle(self): def test_pickle(self):
self.pickle_test('2001:db8::') self.pickle_test('2001:db8::')
def test_weakref(self):
weakref.ref(self.factory('2001:db8::'))
class NetmaskTestMixin_v4(CommonTestMixin_v4): class NetmaskTestMixin_v4(CommonTestMixin_v4):
"""Input validation on interfaces and networks is very similar""" """Input validation on interfaces and networks is very similar"""
......
...@@ -17,6 +17,8 @@ Core and Builtins ...@@ -17,6 +17,8 @@ Core and Builtins
Library Library
------- -------
- Issue #23103: Reduced the memory consumption of IPv4Address and IPv6Address.
- Issue #21793: BaseHTTPRequestHandler again logs response code as numeric, - Issue #21793: BaseHTTPRequestHandler again logs response code as numeric,
not as stringified enum. Patch by Demian Brecht. not as stringified enum. Patch by Demian Brecht.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment