Patchwork [6,of,8] util: chunkbuffer.read() support None size

login
register
mail settings
Submitter Pierre-Yves David
Date April 12, 2014, 10:08 p.m.
Message ID <01a7df5372c949842467.1397340525@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/4308/
State Accepted
Headers show

Comments

Pierre-Yves David - April 12, 2014, 10:08 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1397193026 25200
#      Thu Apr 10 22:10:26 2014 -0700
# Node ID 01a7df5372c9498424670dfe049fa69af0b1a905
# Parent  279e77ed97be1e2b7ed7af7ed71bfd17f43c365a
util: chunkbuffer.read() support None size

When no size is provided, read the whole buffer. This align with the usual
behavior of `read()` in python.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -966,17 +966,19 @@  class chunkbuffer(object):
                 else:
                     yield chunk
         self.iter = splitbig(in_iter)
         self._queue = deque()
 
-    def read(self, l):
+    def read(self, l=None):
         """Read L bytes of data from the iterator of chunks of data.
-        Returns less than L bytes if the iterator runs dry."""
+        Returns less than L bytes if the iterator runs dry.
+
+        If size parameter is ommited, read everything"""
         left = l
         buf = []
         queue = self._queue
-        while left > 0:
+        while left is None or left > 0:
             # refill the queue
             if not queue:
                 target = 2**18
                 for chunk in self.iter:
                     queue.append(chunk)
@@ -985,12 +987,13 @@  class chunkbuffer(object):
                         break
                 if not queue:
                     break
 
             chunk = queue.popleft()
-            left -= len(chunk)
-            if left < 0:
+            if left is not None:
+                left -= len(chunk)
+            if left is not None and left < 0:
                 queue.appendleft(chunk[left:])
                 buf.append(chunk[:left])
             else:
                 buf.append(chunk)