Author: Matti Picus <[email protected]>
Branch: unicode-utf8-py3
Changeset: r95702:8e00f53ea94c
Date: 2019-01-23 16:28 +0200
http://bitbucket.org/pypy/pypy/changeset/8e00f53ea94c/

Log:    test, fix for pickling StringIO with pos != 0

diff --git a/pypy/module/_io/interp_stringio.py 
b/pypy/module/_io/interp_stringio.py
--- a/pypy/module/_io/interp_stringio.py
+++ b/pypy/module/_io/interp_stringio.py
@@ -134,8 +134,11 @@
 
         if space.is_w(w_newline, space.w_None):
             newline = None
+        elif space.isinstance_w(w_newline, space.w_unicode):
+            newline = space.utf8_w(w_newline)
         else:
-            newline = space.utf8_w(w_newline)
+            raise oefmt(space.w_TypeError,
+                 "newline must be str or None, not %T", w_newline)
 
         if (newline is not None and newline != "" and newline != "\n" and
                 newline != "\r" and newline != "\r\n"):
@@ -202,7 +205,8 @@
         if pos < 0:
             raise oefmt(space.w_ValueError,
                         "position value cannot be negative")
-        self.buf = UnicodeIO(initval, pos)
+        self.buf = UnicodeIO(initval)
+        self.buf.seek(pos)
         if not space.is_w(w_dict, space.w_None):
             if not space.isinstance_w(w_dict, space.w_dict):
                 raise oefmt(
diff --git a/pypy/module/_io/test/test_stringio.py 
b/pypy/module/_io/test/test_stringio.py
--- a/pypy/module/_io/test/test_stringio.py
+++ b/pypy/module/_io/test/test_stringio.py
@@ -248,6 +248,8 @@
         assert sio.newlines == ("\n", "\r\n")
         sio.write("c\rd")
         assert sio.newlines == ("\r", "\n", "\r\n")
+        exc = raises(TypeError, io.StringIO, newline=b'\n')
+        assert 'bytes' in str(exc.value)
 
     def test_iterator(self):
         import io
@@ -305,3 +307,18 @@
         raises(TypeError, sio.__setstate__, 0)
         sio.close()
         raises(ValueError, sio.__setstate__, ("closed", "", 0, None))
+
+    def test_roundtrip_state(self):
+        import io
+        s = '12345678'
+        sio1 = io.StringIO(s)
+        sio1.foo = 42
+        sio1.seek(2)
+        assert sio1.getvalue() == s
+        state = sio1.__getstate__()
+        sio2 = io.StringIO()
+        sio2.__setstate__(state)
+        assert sio2.getvalue() == s
+        assert sio2.foo == 42
+        assert sio2.tell() == 2
+          
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to