[pypy-svn] r56309 - in pypy/branch/2.5-features/pypy/module/_file: . test

bgola at codespeak.net bgola at codespeak.net
Fri Jul 4 20:26:40 CEST 2008


Author: bgola
Date: Fri Jul  4 20:26:39 2008
New Revision: 56309

Modified:
   pypy/branch/2.5-features/pypy/module/_file/interp_file.py
   pypy/branch/2.5-features/pypy/module/_file/test/test_file.py
Log:
__enter__ and __exit__ methods for file object

Modified: pypy/branch/2.5-features/pypy/module/_file/interp_file.py
==============================================================================
--- pypy/branch/2.5-features/pypy/module/_file/interp_file.py	(original)
+++ pypy/branch/2.5-features/pypy/module/_file/interp_file.py	Fri Jul  4 20:26:39 2008
@@ -75,6 +75,14 @@
         fd = stream.try_to_find_file_descriptor()
         self.fdopenstream(stream, fd, mode, name)
 
+    def direct___enter__(self):
+        return self
+
+    def direct___exit__(self, excinfo):
+        self.direct_close()
+        # can't return close() value
+        return None
+
     def direct_fdopen(self, fd, mode='r', buffering=-1):
         self.direct_close()
         self.check_mode_ok(mode)
@@ -263,6 +271,11 @@
     _decl(locals(), "__init__", ['self', str, str, int],
           """Opens a file.""")
 
+    _decl(locals(), "__enter__", ['self'], """enter__() -> self.""")
+
+    _decl(locals(), "__exit__", ['self', Arguments], 
+        """exit__(*excinfo) -> None. Closes the file.""")
+
     _decl(locals(), "close", ['self'],
         """close() -> None or (perhaps) an integer.  Close the file.
 

Modified: pypy/branch/2.5-features/pypy/module/_file/test/test_file.py
==============================================================================
--- pypy/branch/2.5-features/pypy/module/_file/test/test_file.py	(original)
+++ pypy/branch/2.5-features/pypy/module/_file/test/test_file.py	Fri Jul  4 20:26:39 2008
@@ -222,6 +222,40 @@
         print 'Passed.'
 
 
+class AppTestFile25:
+    def setup_class(cls):
+        cls.space = gettestobjspace(usemodules=("_file", ), pyversion="2.5")
+        cls.w_temppath = cls.space.wrap(
+            str(py.test.ensuretemp("fileimpl").join("foo.txt")))
+        cls.w_file = getfile(cls.space)
+
+    def test___enter__(self):
+        f = self.file(self.temppath, 'w')
+        assert f.__enter__() is f
+
+    def test___exit__(self):
+        f = self.file(self.temppath, 'w')
+        assert f.__exit__() is None
+        assert f.closed
+
+    def test_file_and_with_statement(self):
+        s1 = """from __future__ import with_statement
+with self.file(self.temppath, 'w') as f:
+    f.write('foo')
+"""
+        exec s1
+        assert f.closed
+        
+        s2 = """from __future__ import with_statement
+with self.file(self.temppath, 'r') as f:
+    s = f.readline()
+"""
+    
+        exec s2
+        assert s == "foo"
+        assert f.closed
+
+
 def test_flush_at_exit():
     from pypy import conftest
     from pypy.tool.option import make_config, make_objspace



More information about the Pypy-commit mailing list