##// END OF EJS Templates
pycompat: wrap xrange for py2 to provide efficient __contains__...
Joerg Sonnenberger -
r39234:45e05d39 default
parent child Browse files
Show More
@@ -556,8 +556,8 b' class changelog(revlog.revlog):'
556 556 if revs is not None:
557 557 if revs:
558 558 assert revs[-1] + 1 == rev
559 revs = pycompat.xrange(revs[0], rev + 1)
559 revs = pycompat.membershiprange(revs[0], rev + 1)
560 560 else:
561 revs = pycompat.xrange(rev, rev + 1)
561 revs = pycompat.membershiprange(rev, rev + 1)
562 562 transaction.changes['revs'] = revs
563 563 return node
@@ -278,6 +278,7 b' if ispy3:'
278 278 hasattr = _wrapattrfunc(builtins.hasattr)
279 279 setattr = _wrapattrfunc(builtins.setattr)
280 280 xrange = builtins.range
281 membershiprange = builtins.range
281 282 unicode = str
282 283
283 284 def open(name, mode='r', buffering=-1, encoding=None):
@@ -343,6 +344,25 b' else:'
343 344 strurl = identity
344 345 bytesurl = identity
345 346
347 class membershiprange(object):
348 "Like xrange(a,b) but with constant-time membership test"
349 def __init__(self, a, b):
350 self._range = xrange(a, b)
351 def __getitem__(self, n):
352 return self._range[n]
353 def __hash__(self):
354 return hash(self._range)
355 def __iter__(self):
356 return iter(self._range)
357 def __len__(self):
358 return len(self._range)
359 def __reversed__(self):
360 return reversed(self._range)
361 def __contains__(self, n):
362 if not self._range:
363 return False
364 return n >= self._range[0] and n <= self._range[-1]
365
346 366 # this can't be parsed on Python 3
347 367 exec('def raisewithtb(exc, tb):\n'
348 368 ' raise exc, None, tb\n')
General Comments 0
You need to be logged in to leave comments. Login now