Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r75649:6657cb5a838d
Date: 2015-02-02 18:15 +0100
http://bitbucket.org/pypy/pypy/changeset/6657cb5a838d/

Log:    Support for "with open(...) as f" in RPython.

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -535,3 +535,9 @@
     def isatty(self):
         self._check_closed()
         return os.isatty(c_fileno(self._ll_file))
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *args):
+        self.close()
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -395,6 +395,25 @@
         os.unlink(fname)
         self.interpret(f, [])
 
+    def test_with_statement(self):
+        fname = str(self.tmpdir.join('file_6'))
+
+        def f():
+            with open(fname, "w") as f:
+                f.write("dupa")
+            try:
+                f.write("dupb")
+            except ValueError:
+                pass
+            else:
+                assert False
+
+        f()
+        assert open(fname, "r").read() == "dupa"
+        os.unlink(fname)
+        self.interpret(f, [])
+        assert open(fname, "r").read() == "dupa"
+
 
 class TestDirect:
     def setup_class(cls):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to