[Python-checkins] cpython: Issue #13393: BufferedReader.read1() now asks the full requested size to

antoine.pitrou python-checkins at python.org
Wed Nov 16 01:02:26 CET 2011


http://hg.python.org/cpython/rev/27bf3d0b8e5f
changeset:   73582:27bf3d0b8e5f
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Wed Nov 16 00:56:10 2011 +0100
summary:
  Issue #13393: BufferedReader.read1() now asks the full requested size to
the raw stream instead of limiting itself to the buffer size.

files:
  Misc/NEWS                |   3 +
  Modules/_io/bufferedio.c |  49 +++++++++------------------
  2 files changed, 19 insertions(+), 33 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #13393: BufferedReader.read1() now asks the full requested size to
+  the raw stream instead of limiting itself to the buffer size.
+
 - Issue #13392: Writing a pyc file should now be atomic under Windows as well.
 
 - Issue #13333: The UTF-7 decoder now accepts lone surrogates (the encoder
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -889,51 +889,34 @@
     if (n == 0)
         return PyBytes_FromStringAndSize(NULL, 0);
 
-    if (!ENTER_BUFFERED(self))
-        return NULL;
-
     /* Return up to n bytes.  If at least one byte is buffered, we
        only return buffered bytes.  Otherwise, we do one raw read. */
 
-    /* XXX: this mimicks the io.py implementation but is probably wrong.
-       If we need to read from the raw stream, then we could actually read
-       all `n` bytes asked by the caller (and possibly more, so as to fill
-       our buffer for the next reads). */
-
     have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
     if (have > 0) {
-        if (n > have)
-            n = have;
-        res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
-        if (res == NULL)
-            goto end;
-        self->pos += n;
-        goto end;
+        n = Py_MIN(have, n);
+        res = _bufferedreader_read_fast(self, n);
+        assert(res != Py_None);
+        return res;
     }
-
-    if (self->writable) {
-        res = buffered_flush_and_rewind_unlocked(self);
-        if (res == NULL)
-            goto end;
+    res = PyBytes_FromStringAndSize(NULL, n);
+    if (res == NULL)
+        return NULL;
+    if (!ENTER_BUFFERED(self)) {
         Py_DECREF(res);
+        return NULL;
     }
-
-    /* Fill the buffer from the raw stream, and copy it to the result. */
     _bufferedreader_reset_buf(self);
-    r = _bufferedreader_fill_buffer(self);
-    if (r == -1)
-        goto end;
+    r = _bufferedreader_raw_read(self, PyBytes_AS_STRING(res), n);
+    LEAVE_BUFFERED(self)
+    if (r == -1) {
+        Py_DECREF(res);
+        return NULL;
+    }
     if (r == -2)
         r = 0;
     if (n > r)
-        n = r;
-    res = PyBytes_FromStringAndSize(self->buffer, n);
-    if (res == NULL)
-        goto end;
-    self->pos = n;
-
-end:
-    LEAVE_BUFFERED(self)
+        _PyBytes_Resize(&res, r);
     return res;
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list