##// END OF EJS Templates
pycompat: wrap xrange for py2 to provide efficient __contains__...
Joerg Sonnenberger -
r39474:45e05d39 default
parent child Browse files
Show More
@@ -556,8 +556,8 b' class changelog(revlog.revlog):'
556 if revs is not None:
556 if revs is not None:
557 if revs:
557 if revs:
558 assert revs[-1] + 1 == rev
558 assert revs[-1] + 1 == rev
559 revs = pycompat.xrange(revs[0], rev + 1)
559 revs = pycompat.membershiprange(revs[0], rev + 1)
560 else:
560 else:
561 revs = pycompat.xrange(rev, rev + 1)
561 revs = pycompat.membershiprange(rev, rev + 1)
562 transaction.changes['revs'] = revs
562 transaction.changes['revs'] = revs
563 return node
563 return node
@@ -278,6 +278,7 b' if ispy3:'
278 hasattr = _wrapattrfunc(builtins.hasattr)
278 hasattr = _wrapattrfunc(builtins.hasattr)
279 setattr = _wrapattrfunc(builtins.setattr)
279 setattr = _wrapattrfunc(builtins.setattr)
280 xrange = builtins.range
280 xrange = builtins.range
281 membershiprange = builtins.range
281 unicode = str
282 unicode = str
282
283
283 def open(name, mode='r', buffering=-1, encoding=None):
284 def open(name, mode='r', buffering=-1, encoding=None):
@@ -343,6 +344,25 b' else:'
343 strurl = identity
344 strurl = identity
344 bytesurl = identity
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 # this can't be parsed on Python 3
366 # this can't be parsed on Python 3
347 exec('def raisewithtb(exc, tb):\n'
367 exec('def raisewithtb(exc, tb):\n'
348 ' raise exc, None, tb\n')
368 ' raise exc, None, tb\n')
General Comments 0
You need to be logged in to leave comments. Login now