Author: Brian Kearns <[email protected]>
Branch: stdlib-2.7.6
Changeset: r69589:d91c3fa0dd7a
Date: 2014-03-01 19:18 -0500
http://bitbucket.org/pypy/pypy/changeset/d91c3fa0dd7a/

Log:    fix fileio modes

diff --git a/pypy/module/_file/test/test_file.py 
b/pypy/module/_file/test/test_file.py
--- a/pypy/module/_file/test/test_file.py
+++ b/pypy/module/_file/test/test_file.py
@@ -60,7 +60,6 @@
         finally:
             f.close()
 
-
     def test_fdopen(self):
         import os
         f = self.file(self.temppath, "w")
diff --git a/pypy/module/_io/interp_fileio.py b/pypy/module/_io/interp_fileio.py
--- a/pypy/module/_io/interp_fileio.py
+++ b/pypy/module/_io/interp_fileio.py
@@ -60,8 +60,8 @@
                 _bad_mode(space)
             rwa = True
             writable = True
-            flags |= O_CREAT
             append = True
+            flags |= O_APPEND | O_CREAT
         elif s == 'b':
             pass
         elif s == '+':
@@ -85,9 +85,6 @@
 
     flags |= O_BINARY
 
-    if append:
-        flags |= O_APPEND
-
     return readable, writable, append, flags
 
 SMALLCHUNK = 8 * 1024
@@ -123,6 +120,7 @@
         self.fd = -1
         self.readable = False
         self.writable = False
+        self.appending = False
         self.seekable = -1
         self.closefd = True
         self.w_name = None
@@ -148,7 +146,7 @@
                 raise OperationError(space.w_ValueError, space.wrap(
                     "negative file descriptor"))
 
-        self.readable, self.writable, append, flags = decode_mode(space, mode)
+        self.readable, self.writable, self.appending, flags = 
decode_mode(space, mode)
 
         fd_is_own = False
         try:
@@ -181,7 +179,7 @@
             self._dircheck(space, w_name)
             space.setattr(self, space.wrap("name"), w_name)
 
-            if append:
+            if self.appending:
                 # For consistent behaviour, we explicitly seek to the end of 
file
                 # (otherwise, it might be done only on the first write()).
                 try:
@@ -194,7 +192,12 @@
             raise
 
     def _mode(self):
-        if self.readable:
+        if self.appending:
+            if self.readable:
+                return 'ab+'
+            else:
+                return 'ab'
+        elif self.readable:
             if self.writable:
                 return 'rb+'
             else:
diff --git a/pypy/module/_io/test/test_fileio.py 
b/pypy/module/_io/test/test_fileio.py
--- a/pypy/module/_io/test/test_fileio.py
+++ b/pypy/module/_io/test/test_fileio.py
@@ -1,6 +1,7 @@
 from rpython.tool.udir import udir
 import os
 
+
 class AppTestFileIO:
     spaceconfig = dict(usemodules=['_io'] + (['fcntl'] if os.name != 'nt' else 
[]))
 
@@ -17,7 +18,7 @@
         import _io
         f = _io.FileIO(self.tmpfile, 'a')
         assert f.name.endswith('tmpfile')
-        assert f.mode == 'wb'
+        assert f.mode == 'ab'
         assert f.closefd is True
         f.close()
 
@@ -191,6 +192,22 @@
         raises(MyException, MyFileIO, fd)
         os.close(fd)  # should not raise OSError(EBADF)
 
+    def test_mode_strings(self):
+        import _io
+        import os
+        try:
+            for modes in [('w', 'wb'), ('wb', 'wb'), ('wb+', 'rb+'),
+                          ('w+b', 'rb+'), ('a', 'ab'), ('ab', 'ab'),
+                          ('ab+', 'ab+'), ('a+b', 'ab+'), ('r', 'rb'),
+                          ('rb', 'rb'), ('rb+', 'rb+'), ('r+b', 'rb+')]:
+                # read modes are last so that TESTFN will exist first
+                with _io.FileIO(self.tmpfile, modes[0]) as f:
+                    assert f.mode == modes[1]
+        finally:
+            if os.path.exists(self.tmpfile):
+                os.unlink(self.tmpfile)
+
+
 def test_flush_at_exit():
     from pypy import conftest
     from pypy.tool.option import make_config, make_objspace
@@ -209,6 +226,7 @@
     space.finish()
     assert tmpfile.read() == '42'
 
+
 def test_flush_at_exit_IOError_and_ValueError():
     from pypy import conftest
     from pypy.tool.option import make_config, make_objspace
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to