Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(18)

Side by Side Diff: Lib/ipaddress.py

Issue 21487: Assorted ipaddress performance improvements
Patch Set: Created 5 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2007 Google Inc. 1 # Copyright 2007 Google Inc.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """A fast, lightweight IPv4/IPv6 manipulation library in Python. 4 """A fast, lightweight IPv4/IPv6 manipulation library in Python.
5 5
6 This library is used to create/poke/manipulate IPv4 and IPv6 addresses 6 This library is used to create/poke/manipulate IPv4 and IPv6 addresses
7 and networks. 7 and networks.
8 8
9 """ 9 """
10 10
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 Args: 188 Args:
189 number: an integer. 189 number: an integer.
190 bits: maximum number of bits to count. 190 bits: maximum number of bits to count.
191 191
192 Returns: 192 Returns:
193 The number of zero bits on the right hand side of the number. 193 The number of zero bits on the right hand side of the number.
194 194
195 """ 195 """
196 if number == 0: 196 if number == 0:
197 return bits 197 return bits
198 for i in range(bits): 198 return min(bits, (~number & (number-1)).bit_length())
199 if (number >> i) & 1:
200 return i
201 # All bits of interest were zero, even if there are more in the number
202 return bits
203 199
204 200
205 def summarize_address_range(first, last): 201 def summarize_address_range(first, last):
206 """Summarize a network range given the first and last IP addresses. 202 """Summarize a network range given the first and last IP addresses.
207 203
208 Example: 204 Example:
209 >>> list(summarize_address_range(IPv4Address('192.0.2.0'), 205 >>> list(summarize_address_range(IPv4Address('192.0.2.0'),
210 ... IPv4Address('192.0.2.130'))) 206 ... IPv4Address('192.0.2.130')))
211 ... #doctest: +NORMALIZE_WHITESPACE 207 ... #doctest: +NORMALIZE_WHITESPACE
212 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), 208 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'),
(...skipping 30 matching lines...) Expand all
243 ip = IPv6Network 239 ip = IPv6Network
244 else: 240 else:
245 raise ValueError('unknown IP version') 241 raise ValueError('unknown IP version')
246 242
247 ip_bits = first._max_prefixlen 243 ip_bits = first._max_prefixlen
248 first_int = first._ip 244 first_int = first._ip
249 last_int = last._ip 245 last_int = last._ip
250 while first_int <= last_int: 246 while first_int <= last_int:
251 nbits = min(_count_righthand_zero_bits(first_int, ip_bits), 247 nbits = min(_count_righthand_zero_bits(first_int, ip_bits),
252 (last_int - first_int + 1).bit_length() - 1) 248 (last_int - first_int + 1).bit_length() - 1)
253 net = ip('%s/%d' % (first, ip_bits - nbits)) 249 net = ip((first_int, ip_bits - nbits))
254 yield net 250 yield net
255 first_int += 1 << nbits 251 first_int += 1 << nbits
256 if first_int - 1 == ip._ALL_ONES: 252 if first_int - 1 == ip._ALL_ONES:
257 break 253 break
258 first = first.__class__(first_int)
259 254
260 255
261 def _collapse_addresses_recursive(addresses): 256 def _collapse_addresses_recursive(addresses):
262 """Loops through the addresses, collapsing concurrent netblocks. 257 """Loops through the addresses, collapsing concurrent netblocks.
263 258
264 Example: 259 Example:
265 260
266 ip1 = IPv4Network('192.0.2.0/26') 261 ip1 = IPv4Network('192.0.2.0/26')
267 ip2 = IPv4Network('192.0.2.64/26') 262 ip2 = IPv4Network('192.0.2.64/26')
268 ip3 = IPv4Network('192.0.2.128/26') 263 ip3 = IPv4Network('192.0.2.128/26')
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 932
938 if prefixlen_diff < 0: 933 if prefixlen_diff < 0:
939 raise ValueError('prefix length diff must be > 0') 934 raise ValueError('prefix length diff must be > 0')
940 new_prefixlen = self._prefixlen + prefixlen_diff 935 new_prefixlen = self._prefixlen + prefixlen_diff
941 936
942 if new_prefixlen > self._max_prefixlen: 937 if new_prefixlen > self._max_prefixlen:
943 raise ValueError( 938 raise ValueError(
944 'prefix length diff %d is invalid for netblock %s' % ( 939 'prefix length diff %d is invalid for netblock %s' % (
945 new_prefixlen, self)) 940 new_prefixlen, self))
946 941
947 first = self.__class__('%s/%s' % 942 start = int(self.network_address)
948 (self.network_address, 943 end = int(self.broadcast_address)
949 self._prefixlen + prefixlen_diff)) 944 step = (int(self.hostmask) + 1) >> prefixlen_diff
950 945 for new_addr in range(start, end, step):
951 yield first 946 current = self.__class__((new_addr, new_prefixlen))
952 current = first
953 while True:
954 broadcast = current.broadcast_address
955 if broadcast == self.broadcast_address:
956 return
957 new_addr = self._address_class(int(broadcast) + 1)
958 current = self.__class__('%s/%s' % (new_addr,
959 new_prefixlen))
960
961 yield current 947 yield current
962 948
963 def supernet(self, prefixlen_diff=1, new_prefix=None): 949 def supernet(self, prefixlen_diff=1, new_prefix=None):
964 """The supernet containing the current network. 950 """The supernet containing the current network.
965 951
966 Args: 952 Args:
967 prefixlen_diff: An integer, the amount the prefix length of 953 prefixlen_diff: An integer, the amount the prefix length of
968 the network should be decreased by. For example, given a 954 the network should be decreased by. For example, given a
969 /24 network and a prefixlen_diff of 3, a supernet with a 955 /24 network and a prefixlen_diff of 3, a supernet with a
970 /21 netmask is returned. 956 /21 netmask is returned.
(...skipping 1285 matching lines...) Expand 10 before | Expand all | Expand 10 after
2256 Note that the site-local address space has been deprecated by RFC 3879. 2242 Note that the site-local address space has been deprecated by RFC 3879.
2257 Use is_private to test if this address is in the space of unique local 2243 Use is_private to test if this address is in the space of unique local
2258 addresses as defined by RFC 4193. 2244 addresses as defined by RFC 4193.
2259 2245
2260 Returns: 2246 Returns:
2261 A boolean, True if the address is reserved per RFC 3513 2.5.6. 2247 A boolean, True if the address is reserved per RFC 3513 2.5.6.
2262 2248
2263 """ 2249 """
2264 return (self.network_address.is_site_local and 2250 return (self.network_address.is_site_local and
2265 self.broadcast_address.is_site_local) 2251 self.broadcast_address.is_site_local)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+