summaryrefslogtreecommitdiffstats
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-01-18 22:41:32 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-01-18 22:41:32 (GMT)
commitb53f0fbf96ec0b1f9ad7ddad5e9480d17e48e27a (patch)
tree80bcc8ef0106a6379e3a646984ca622771cacfe7 /Lib/ipaddress.py
parent1202a4733e6ffbf3149425aae14f7c4eeeb115c0 (diff)
downloadcpython-b53f0fbf96ec0b1f9ad7ddad5e9480d17e48e27a.zip
cpython-b53f0fbf96ec0b1f9ad7ddad5e9480d17e48e27a.tar.gz
cpython-b53f0fbf96ec0b1f9ad7ddad5e9480d17e48e27a.tar.bz2
Issue #23266: Restore the performance of ipaddress.collapse_addresses() whith
duplicated addresses and simplify the code.
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py36
1 files changed, 16 insertions, 20 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index ce6bf0c..2ba98d8 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -164,25 +164,23 @@ def _split_optional_netmask(address):
def _find_address_range(addresses):
- """Find a sequence of IPv#Address.
+ """Find a sequence of sorted deduplicated IPv#Address.
Args:
addresses: a list of IPv#Address objects.
- Returns:
- A tuple containing the first and last IP addresses in the sequence,
- and the number of distinct IP addresses in the sequence.
+ Yields:
+ A tuple containing the first and last IP addresses in the sequence.
"""
- first = last = addresses[0]
- i = 1
- for ip in addresses[1:]:
- if ip._ip == last._ip + 1:
- last = ip
- i += 1
- else:
- break
- return (first, last, i)
+ it = iter(addresses)
+ first = last = next(it)
+ for ip in it:
+ if ip._ip != last._ip + 1:
+ yield first, last
+ first = ip
+ last = ip
+ yield first, last
def _count_righthand_zero_bits(number, bits):
@@ -323,7 +321,6 @@ def collapse_addresses(addresses):
TypeError: If passed a list of mixed version objects.
"""
- i = 0
addrs = []
ips = []
nets = []
@@ -349,14 +346,13 @@ def collapse_addresses(addresses):
ip, nets[-1]))
nets.append(ip)
- # sort
- ips = sorted(ips)
+ # sort and dedup
+ ips = sorted(set(ips))
# find consecutive address ranges in the sorted sequence and summarize them
- while i < len(ips):
- (first, last, items) = _find_address_range(ips[i:])
- i = items + i
- addrs.extend(summarize_address_range(first, last))
+ if ips:
+ for first, last in _find_address_range(ips):
+ addrs.extend(summarize_address_range(first, last))
return _collapse_addresses_internal(addrs + nets)