comparison mercurial/util.py @ 21018:c848bfd02366

util: support None size in chunkbuffer.read() When no size is provided, read the whole buffer. This aligns with the usual behavior of `read()` in python.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Thu, 10 Apr 2014 22:10:26 -0700
parents 0e8417131a29
children cc13addbd62b
comparison
equal deleted inserted replaced
21017:8de8187e6f48 21018:c848bfd02366
966 else: 966 else:
967 yield chunk 967 yield chunk
968 self.iter = splitbig(in_iter) 968 self.iter = splitbig(in_iter)
969 self._queue = deque() 969 self._queue = deque()
970 970
971 def read(self, l): 971 def read(self, l=None):
972 """Read L bytes of data from the iterator of chunks of data. 972 """Read L bytes of data from the iterator of chunks of data.
973 Returns less than L bytes if the iterator runs dry.""" 973 Returns less than L bytes if the iterator runs dry.
974
975 If size parameter is ommited, read everything"""
974 left = l 976 left = l
975 buf = [] 977 buf = []
976 queue = self._queue 978 queue = self._queue
977 while left > 0: 979 while left is None or left > 0:
978 # refill the queue 980 # refill the queue
979 if not queue: 981 if not queue:
980 target = 2**18 982 target = 2**18
981 for chunk in self.iter: 983 for chunk in self.iter:
982 queue.append(chunk) 984 queue.append(chunk)
985 break 987 break
986 if not queue: 988 if not queue:
987 break 989 break
988 990
989 chunk = queue.popleft() 991 chunk = queue.popleft()
990 left -= len(chunk) 992 if left is not None:
991 if left < 0: 993 left -= len(chunk)
994 if left is not None and left < 0:
992 queue.appendleft(chunk[left:]) 995 queue.appendleft(chunk[left:])
993 buf.append(chunk[:left]) 996 buf.append(chunk[:left])
994 else: 997 else:
995 buf.append(chunk) 998 buf.append(chunk)
996 999