Skip to content

Commit d698b00

Browse files
authored
Cast input to IpRangeList (#15)
Reduce the overhead of testing for membership in a long IpRangeList by ensuring that the provided input has been cast to an integer before checking each contained range. Closes #14
1 parent 3b5f802 commit d698b00

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

iptools/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def _cast(self, item):
249249
if ipv4 == self._ipver and item > ipv4.MAX_IP:
250250
# casting an ipv6 in an ipv4 range
251251
# downcast to ipv4 iff address is in the IPv4 mapped block
252-
if item in IpRange(ipv6.IPV4_MAPPED):
252+
if item in _IPV6_MAPPED_IPV4:
253253
item = item & ipv4.MAX_IP
254254
# end if
255255

@@ -395,6 +395,9 @@ def __iter__(self):
395395
# end class IpRange
396396

397397

398+
_IPV6_MAPPED_IPV4 = IpRange(ipv6.IPV4_MAPPED)
399+
400+
398401
class IpRangeList (object):
399402
"""
400403
List of IpRange objects.
@@ -464,6 +467,11 @@ def __contains__(self, item):
464467
:type item: str
465468
:returns: ``True`` if address is in list, ``False`` otherwise.
466469
"""
470+
if isinstance(item, basestring):
471+
item = _address2long(item)
472+
if type(item) not in (type(1), type(ipv4.MAX_IP), type(ipv6.MAX_IP)):
473+
raise TypeError(
474+
"expected ip address, 32-bit integer or 128-bit integer")
467475
for r in self.ips:
468476
if item in r:
469477
return True

0 commit comments

Comments
 (0)