Justin Peel avatar Justin Peel committed a93fcd8

Fixed BufferedOutputStream and LineBufferedOutputStream so that they fully pass all tests.

Comments (0)

Files changed (1)

pypy/rlib/streamio.py

         if self.buf:
             self.do_write(''.join(self.buf))
             self.buf = []
+            self.buflen = 0
 
     def tell(self):
-        return self.do_tell() + len(self.buf)
+        return self.do_tell() + self.buflen
 
     def write(self, data):
         buflen = self.buflen
             self.buf.append(data)
             self.buflen += datalen
         elif buflen:
-            slice = self.bufsize - buflen
-            assert slice >= 0
-            self.buf.append(data[:slice])
+            i = self.bufsize - buflen
+            assert i >= 0
+            self.buf.append(data[:i])
             self.do_write(''.join(self.buf))
             self.buf = []
             self.buflen = 0
-            self.write(data[slice:])
+            self.write(data[i:])
         else:
             self.do_write(data)
 
 
     def write(self, data):
         p = data.rfind('\n') + 1
-        L = len(data)
-        if p >= 0:
-            if self.buf:
+        assert p >= 0
+        if self.buflen + len(data) < self.bufsize:
+            if p == 0:
+                self.buf.append(data)
+                self.buflen += len(data)
+            else:
+                if self.buflen:
+                    self.do_write(''.join(self.buf))
+                self.do_write(data[:p])
+                self.buf = [data[p:]]
+                self.buflen = len(self.buf[0])
+        else:
+            if self.buflen + p < self.bufsize:
+                p = self.bufsize - self.buflen
+            if self.buflen:
                 self.do_write(''.join(self.buf))
-                self.buf = []
-                self.buflen = 0
-            if L - p > self.bufsize:
-                self.do_write(data)
-            else:
-                self.do_write(data[:p])
-                if p != L:
-                    self.buf.append(data[p:])
-                    self.buflen = L - p
-        else:
-            if L + self.buflen > self.bufsize:
-                self.do_write(''.join(self.buf))
-                self.do_write(data)
-                self.buf = []
-                self.buflen = 0
-            else:
-                self.buf.append(data)
-                self.buflen += L
+            assert p >= 0
+            self.do_write(data[:p])
+            self.buf = [data[p:]]
+            self.buflen = len(self.buf[0])
 
 
 # ____________________________________________________________
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.