[pypy-svn] r78398 - in pypy/branch/fast-forward/pypy/module/_io: . test

afa at codespeak.net afa at codespeak.net
Thu Oct 28 14:59:11 CEST 2010


Author: afa
Date: Thu Oct 28 14:59:09 2010
New Revision: 78398

Modified:
   pypy/branch/fast-forward/pypy/module/_io/interp_bufferedio.py
   pypy/branch/fast-forward/pypy/module/_io/test/test_bufferedio.py
Log:
BufferedReader.read1


Modified: pypy/branch/fast-forward/pypy/module/_io/interp_bufferedio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/interp_bufferedio.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_bufferedio.py	Thu Oct 28 14:59:09 2010
@@ -27,6 +27,10 @@
         self._unsupportedoperation(space, "read")
 
     @unwrap_spec('self', ObjSpace, W_Root)
+    def read1_w(self, space, w_size):
+        self._unsupportedoperation(space, "read1")
+
+    @unwrap_spec('self', ObjSpace, W_Root)
     def write_w(self, space, w_data):
         self._unsupportedoperation(space, "write")
 
@@ -38,6 +42,7 @@
     '_BufferedIOBase', W_IOBase.typedef,
     __new__ = generic_new_descr(W_BufferedIOBase),
     read = interp2app(W_BufferedIOBase.read_w),
+    read1 = interp2app(W_BufferedIOBase.read1_w),
     write = interp2app(W_BufferedIOBase.write_w),
     detach = interp2app(W_BufferedIOBase.detach_w),
     )
@@ -296,6 +301,45 @@
                     res = self._read_generic(space, size)
         return space.wrap(res)
 
+    @unwrap_spec('self', ObjSpace, int)
+    def read1_w(self, space, size):
+        self._check_init(space)
+        self._check_closed(space, "read of closed file")
+
+        if size < 0:
+            raise OperationError(space.w_ValueError, space.wrap(
+                "read length must be positive"))
+        if size == 0:
+            return space.wrap("")
+
+        with self.lock:
+            if self.writable:
+                self._writer_flush_unlocked(space, restore_pos=True)
+
+            # 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 = self._readahead()
+            if have == 0:
+                # Fill the buffer from the raw stream
+                self._reader_reset_buf()
+                self.pos = 0
+                try:
+                    have = self._fill_buffer(space)
+                except BlockingIOError:
+                    have = 0
+            if size > have:
+                size = have
+            data = rffi.charpsize2str(rffi.ptradd(self.buffer, self.pos),
+                                      size)
+            self.pos += size
+            return space.wrap(data)
+
     def _read_all(self, space):
         "Read all the file, don't update the cache"
         builder = StringBuilder()
@@ -433,6 +477,7 @@
     __init__  = interp2app(W_BufferedReader.descr_init),
 
     read = interp2app(W_BufferedReader.read_w),
+    read1 = interp2app(W_BufferedReader.read1_w),
 
     # from the mixin class
     seek = interp2app(W_BufferedReader.seek_w),

Modified: pypy/branch/fast-forward/pypy/module/_io/test/test_bufferedio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/test/test_bufferedio.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/test/test_bufferedio.py	Thu Oct 28 14:59:09 2010
@@ -32,6 +32,29 @@
         assert f.read(3) == ""
         f.close()
 
+    def test_read1(self):
+        import _io
+        class RecordingFileIO(_io.FileIO):
+            def read(self, size=-1):
+                self.nbreads += 1
+                return _io.FileIO.read(self, size)
+            def readinto(self, buf):
+                self.nbreads += 1
+                return _io.FileIO.readinto(self, buf)
+        raw = RecordingFileIO(self.tmpfile)
+        raw.nbreads = 0
+        f = _io.BufferedReader(raw, buffer_size=3)
+        assert f.read(1) == 'a'
+        assert f.read1(1) == '\n'
+        assert raw.nbreads == 1
+        assert f.read1(100) == 'b'
+        assert raw.nbreads == 1
+        assert f.read1(100) == '\nc'
+        assert raw.nbreads == 2
+        assert f.read1(100) == ''
+        assert raw.nbreads == 3
+        f.close()
+
     def test_seek(self):
         import _io
         raw = _io.FileIO(self.tmpfile)



More information about the Pypy-commit mailing list