Rev 3150: Review feedback, simpler loops. in file:///v/home/vila/src/bzr/bugs/179368/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu Jan 3 16:26:36 GMT 2008


At file:///v/home/vila/src/bzr/bugs/179368/

------------------------------------------------------------
revno: 3150
revision-id:v.ladeuil+lp at free.fr-20080103162632-6a0mxvbq22hd1pvo
parent: v.ladeuil+lp at free.fr-20080103151258-idpzfox078f80vhe
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 179368
timestamp: Thu 2008-01-03 17:26:32 +0100
message:
  Review feedback, simpler loops.
modified:
  bzrlib/transport/http/_urllib2_wrappers.py _urllib2_wrappers.py-20060913231729-ha9ugi48ktx481ao-1
  bzrlib/transport/http/response.py _response.py-20060613154423-a2ci7hd4iw5c7fnt-1
-------------- next part --------------
=== modified file 'bzrlib/transport/http/_urllib2_wrappers.py'
--- a/bzrlib/transport/http/_urllib2_wrappers.py	2008-01-02 14:13:55 +0000
+++ b/bzrlib/transport/http/_urllib2_wrappers.py	2008-01-03 16:26:32 +0000
@@ -75,6 +75,12 @@
     # Some responses have bodies in which we have no interest
     _body_ignored_responses = [301,302, 303, 307, 401, 403, 404]
 
+    # in finish() below, we may have to discard several MB in the worst
+    # case. To avoid buffering that much, we read and discard by chunks
+    # instead. The underlying file is either a socket or a StringIO, so reading
+    # 8k chunks should be fine.
+    _discarded_buf_size = 8192
+
     def begin(self):
         """Begin to read the response from the server.
 
@@ -113,12 +119,6 @@
             # below we keep the socket with the server opened.
             self.will_close = False
 
-    # in finish() below, we may have to discard several MB in the worst
-    # case. To avoid buffering that much, we read and discard by chunks
-    # instead. The underlying file is either a socket or a StringIO, so reading
-    # 8k chunks should be fine.
-    _discarded_buf_size = 8192
-
     def finish(self):
         """Finish reading the body.
 
@@ -135,12 +135,9 @@
             # Make sure nothing was left to be read on the socket
             pending = 0
             data = True
-            while (data and self.length
-                   and self.length > self._discarded_buf_size):
-                data = self.read(self._discarded_buf_size)
-                pending += len(data)
-            if data and self.length:
-                data = self.read(self.length)
+            while data and self.length:
+                # read() will update self.length
+                data = self.read(min(self.length, self._discarded_buf_size))
                 pending += len(data)
             if pending:
                 trace.mutter("%s bytes left on the HTTP socket", pending)

=== modified file 'bzrlib/transport/http/response.py'
--- a/bzrlib/transport/http/response.py	2008-01-02 14:13:55 +0000
+++ b/bzrlib/transport/http/response.py	2008-01-03 16:26:32 +0000
@@ -55,6 +55,12 @@
     should happen with monotonically increasing offsets.
     """
 
+    # in _checked_read() below, we may have to discard several MB in the worst
+    # case. To avoid buffering that much, we read and discard by chunks
+    # instead. The underlying file is either a socket or a StringIO, so reading
+    # 8k chunks should be fine.
+    _discarded_buf_size = 8192
+
     def __init__(self, path, infile):
         """Constructor.
 
@@ -144,28 +150,20 @@
                                           'Invalid range, size <= 0')
         self.set_range(start, size)
 
-    # in _checked_read() below, we may have to discard several MB in the worst
-    # case. To avoid buffering that much, we read and discard by chunks
-    # instead. The underlying file is either a socket or a StringIO, so reading
-    # 8k chunks should be fine.
-    _discarded_buf_size = 8192
-
     def _checked_read(self, size):
         """Read the file checking for short reads.
 
         The data read is discarded along the way.
         """
         pos = self._pos
-        data_len = 0
-        while size - data_len > self._discarded_buf_size:
-            data = self._file.read(self._discarded_buf_size)
-            data_len += len(data)
-        if size - data_len > 0:
-            data = self._file.read(size - data_len)
-            data_len += len(data)
-        if size > 0 and data_len != size:
-            raise errors.ShortReadvError(self._path, pos, size, data_len)
-        self._pos += data_len
+        remaining = size
+        while remaining > 0:
+            data = self._file.read(min(remaining, self._discarded_buf_size))
+            remaining -= len(data)
+            if not data:
+                raise errors.ShortReadvError(self._path, pos, size,
+                                             size - remaining)
+        self._pos += size
 
     def _seek_to_next_range(self):
         # We will cross range boundaries



More information about the bazaar-commits mailing list