[Python-checkins] r68548 - sandbox/trunk/io-c/_bufferedio.c

antoine.pitrou python-checkins at python.org
Mon Jan 12 20:03:05 CET 2009


Author: antoine.pitrou
Date: Mon Jan 12 20:03:05 2009
New Revision: 68548

Log:
Rewrite read1 so as not to call peek then read.



Modified:
   sandbox/trunk/io-c/_bufferedio.c

Modified: sandbox/trunk/io-c/_bufferedio.c
==============================================================================
--- sandbox/trunk/io-c/_bufferedio.c	(original)
+++ sandbox/trunk/io-c/_bufferedio.c	Mon Jan 12 20:03:05 2009
@@ -424,6 +424,8 @@
 /* Forward decls */
 static PyObject *
 _BufferedWriter_flush_unlocked(BufferedObject *, int);
+static Py_ssize_t
+_BufferedReader_fill_buffer(BufferedObject *self);
 static void
 _BufferedReader_reset_buf(BufferedObject *self);
 static void
@@ -604,7 +606,7 @@
 static PyObject *
 Buffered_read1(BufferedObject *self, PyObject *args)
 {
-    Py_ssize_t n, have;
+    Py_ssize_t n, have, r;
     PyObject *res;
 
     CHECK_INITIALIZED(self)
@@ -633,16 +635,30 @@
        all `n` bytes asked by the caller (and possibly more, so as to fill
        our buffer for the next reads). */
 
-    res = _BufferedReader_peek_unlocked(self, 1);
-    if (res == NULL)
+    have = READAHEAD(self);
+    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;
-    Py_CLEAR(res);
-
-    have = self->read_end - self->pos;
-    if (n > have)
-        n = have;
+    }
 
-    res = _BufferedReader_read_unlocked(self, n);
+    /* 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)
+        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)


More information about the Python-checkins mailing list