[pypy-svn] pypy default: Make sure BytesIO.__{get, set}state__ raise the right exceptions.

alex_gaynor commits-noreply at bitbucket.org
Fri Feb 4 18:57:24 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41615:9d967a690db0
Date: 2011-02-04 12:52 -0500
http://bitbucket.org/pypy/pypy/changeset/9d967a690db0/

Log:	Make sure BytesIO.__{get,set}state__ raise the right exceptions.

diff --git a/pypy/module/_io/test/test_bytesio.py b/pypy/module/_io/test/test_bytesio.py
--- a/pypy/module/_io/test/test_bytesio.py
+++ b/pypy/module/_io/test/test_bytesio.py
@@ -67,6 +67,10 @@
         assert f.read() == "ld"
         assert f.a == 1
         assert f.__getstate__() == ("world", 5, {"a": 1})
+        raises(TypeError, f.__setstate__, ("", 0))
+        f.close()
+        raises(ValueError, f.__getstate__)
+        raises(ValueError, f.__setstate__, ("world", 3, {"a": 1}))
 
     def test_readinto(self):
         import _io

diff --git a/pypy/module/_io/interp_bytesio.py b/pypy/module/_io/interp_bytesio.py
--- a/pypy/module/_io/interp_bytesio.py
+++ b/pypy/module/_io/interp_bytesio.py
@@ -189,6 +189,7 @@
 
     @unwrap_spec('self', ObjSpace)
     def getstate_w(self, space):
+        self._check_closed(space)
         w_content = space.wrap(buffer2string(self.buf, 0, self.string_size))
         return space.newtuple([
             w_content,
@@ -197,6 +198,14 @@
 
     @unwrap_spec('self', ObjSpace, W_Root)
     def setstate_w(self, space, w_state):
+        self._check_closed(space)
+
+        if space.int_w(space.len(w_state)) != 3:
+            raise operationerrfmt(space.w_TypeError,
+                "%s.__setstate__ argument should be 3-tuple, got %s",
+                space.type(self).getname(space),
+                space.type(w_state).getname(space)
+            )
         w_content, w_pos, w_dict = space.unpackiterable(w_state, 3)
         pos = space.int_w(w_pos)
         self.buf = []


More information about the Pypy-commit mailing list