Author: Justin Peel <[email protected]>
Branch: streamio-bufout
Changeset: r45786:a93fcd815003
Date: 2011-07-19 23:25 -0600
http://bitbucket.org/pypy/pypy/changeset/a93fcd815003/

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

diff --git a/pypy/rlib/streamio.py b/pypy/rlib/streamio.py
--- a/pypy/rlib/streamio.py
+++ b/pypy/rlib/streamio.py
@@ -882,9 +882,10 @@
         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
@@ -893,13 +894,13 @@
             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)
 
@@ -926,28 +927,26 @@
 
     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.buf = []
-                self.buflen = 0
-            if L - p > self.bufsize:
-                self.do_write(data)
+                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))
+            assert p >= 0
                 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
+            self.buf = [data[p:]]
+            self.buflen = len(self.buf[0])
 
 
 # ____________________________________________________________
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to