[pypy-svn] pypy default: Expose _CHUNK_SIZE on TextIOWrapper and don't allow telling while iterating.

alex_gaynor commits-noreply at bitbucket.org
Mon Jan 31 02:46:10 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41481:e9b2ba3f32e5
Date: 2011-01-30 20:45 -0500
http://bitbucket.org/pypy/pypy/changeset/e9b2ba3f32e5/

Log:	Expose _CHUNK_SIZE on TextIOWrapper and don't allow telling while
	iterating.

diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -496,6 +496,16 @@
 
         return not eof
 
+    @unwrap_spec('self', ObjSpace)
+    def next_w(self, space):
+        self.telling = False
+        try:
+            return W_TextIOBase.next_w(self, space)
+        except OperationError, e:
+            if e.match(space, space.w_StopIteration):
+                self.telling = self.seekable
+            raise
+
     @unwrap_spec('self', ObjSpace, W_Root)
     def read_w(self, space, w_size=None):
         self._check_closed(space)
@@ -927,6 +937,19 @@
         cookie.chars_to_skip = chars_to_skip
         return space.wrap(cookie.pack())
 
+    def chunk_size_get_w(space, self):
+        self._check_init(space)
+        return space.wrap(self.chunk_size)
+
+    def chunk_size_set_w(space, self, w_size):
+        self._check_init(space)
+        size = space.int_w(w_size)
+        if size <= 0:
+            raise OperationError(space.w_ValueError,
+                space.wrap("a strictly positive integer is required")
+            )
+        self.chunk_size = size
+
 W_TextIOWrapper.typedef = TypeDef(
     'TextIOWrapper', W_TextIOBase.typedef,
     __new__ = generic_new_descr(W_TextIOWrapper),
@@ -934,6 +957,7 @@
     __repr__ = interp2app(W_TextIOWrapper.descr_repr),
     __module__ = "_io",
 
+    next = interp2app(W_TextIOWrapper.next_w),
     read = interp2app(W_TextIOWrapper.read_w),
     readline = interp2app(W_TextIOWrapper.readline_w),
     write = interp2app(W_TextIOWrapper.write_w),
@@ -951,4 +975,7 @@
     name = GetSetProperty(W_TextIOWrapper.name_get_w),
     buffer = interp_attrproperty_w("w_buffer", cls=W_TextIOWrapper),
     closed = GetSetProperty(W_TextIOWrapper.closed_get_w),
+    _CHUNK_SIZE = GetSetProperty(
+        W_TextIOWrapper.chunk_size_get_w, W_TextIOWrapper.chunk_size_set_w
+    ),
 )

diff --git a/pypy/module/_io/test/test_io.py b/pypy/module/_io/test/test_io.py
--- a/pypy/module/_io/test/test_io.py
+++ b/pypy/module/_io/test/test_io.py
@@ -220,3 +220,37 @@
                     f.seek(cookie)
                     res = f.read()
                     assert res == decoded[i:]
+
+    def test_telling(self):
+        import _io
+
+        with _io.open(self.tmpfile, "w+", encoding="utf8") as f:
+            p0 = f.tell()
+            f.write(u"\xff\n")
+            p1 = f.tell()
+            f.write(u"\xff\n")
+            p2 = f.tell()
+            f.seek(0)
+
+            assert f.tell() == p0
+            res = f.readline()
+            assert res == u"\xff\n"
+            assert f.tell() == p1
+            res = f.readline()
+            assert res == u"\xff\n"
+            assert f.tell() == p2
+            f.seek(0)
+
+            for line in f:
+                assert line == u"\xff\n"
+                raises(IOError, f.tell)
+            assert f.tell() == p2
+
+    def test_chunk_size(self):
+        import _io
+
+        with _io.open(self.tmpfile) as f:
+            assert f._CHUNK_SIZE >= 1
+            f._CHUNK_SIZE = 4096
+            assert f._CHUNK_SIZE == 4096
+            raises(ValueError, setattr, f, "_CHUNK_SIZE", 0)


More information about the Pypy-commit mailing list