[pypy-svn] pypy fast-forward: Check closed status of io.StringIO
amauryfa
commits-noreply at bitbucket.org
Fri Jan 7 17:56:05 CET 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40461:fbb725a2c0bd
Date: 2011-01-07 14:52 +0100
http://bitbucket.org/pypy/pypy/changeset/fbb725a2c0bd/
Log: Check closed status of io.StringIO
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
@@ -7,3 +7,10 @@
assert sio.getvalue() == u'Hello world'
assert io.StringIO(u"hello").read() == u'hello'
+
+ def test_closed(self):
+ import io
+ sio = io.StringIO()
+ sio.close()
+ raises(ValueError, sio.read, 1)
+ raises(ValueError, sio.write, u"text")
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
@@ -1,6 +1,7 @@
-from pypy.interpreter.typedef import TypeDef, generic_new_descr
+from pypy.interpreter.typedef import (
+ TypeDef, generic_new_descr, GetSetProperty)
from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.error import operationerrfmt
+from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.baseobjspace import ObjSpace, W_Root
from pypy.module._io.interp_textio import W_TextIOBase
from pypy.module._io.interp_iobase import convert_size
@@ -11,11 +12,6 @@
self.buf = []
self.pos = 0
- def _check_closed(self, space, message=None):
- pass
- def _check_initialized(self):
- pass
-
@unwrap_spec('self', ObjSpace, W_Root)
def descr_init(self, space, w_initvalue=None):
# In case __init__ is called multiple times
@@ -26,6 +22,12 @@
self.write_w(space, w_initvalue)
self.pos = 0
+ def _check_closed(self, space, message=None):
+ if self.buf is None:
+ if message is None:
+ message = "I/O operation on closed file"
+ raise OperationError(space.w_ValueError, space.wrap(message))
+
def resize_buffer(self, newlength):
if len(self.buf) > newlength:
self.buf = self.buf[:newlength]
@@ -47,7 +49,6 @@
@unwrap_spec('self', ObjSpace, W_Root)
def write_w(self, space, w_obj):
- self._check_initialized()
if not space.isinstance_w(w_obj, space.w_unicode):
raise operationerrfmt(space.w_TypeError,
"string argument expected, got '%s'",
@@ -61,6 +62,7 @@
@unwrap_spec('self', ObjSpace, W_Root)
def read_w(self, space, w_size=None):
+ self._check_closed(space)
size = convert_size(space, w_size)
start = self.pos
if size >= 0:
@@ -72,10 +74,16 @@
@unwrap_spec('self', ObjSpace)
def getvalue_w(self, space):
- self._check_initialized()
self._check_closed(space)
return space.wrap(u''.join(self.buf))
+ @unwrap_spec('self', ObjSpace)
+ def close_w(self, space):
+ self.buf = None
+
+ def closed_get_w(space, self):
+ return space.wrap(self.buf is None)
+
W_StringIO.typedef = TypeDef(
'StringIO', W_TextIOBase.typedef,
__new__ = generic_new_descr(W_StringIO),
@@ -83,5 +91,7 @@
write=interp2app(W_StringIO.write_w),
read=interp2app(W_StringIO.read_w),
getvalue=interp2app(W_StringIO.getvalue_w),
+ close = interp2app(W_StringIO.close_w),
+ closed = GetSetProperty(W_StringIO.closed_get_w),
)
More information about the Pypy-commit
mailing list