[Python-checkins] cpython (2.7): Issue #17976: Fixed potential problem with file.write() not detecting IO error

serhiy.storchaka python-checkins at python.org
Tue Dec 17 13:40:53 CET 2013


http://hg.python.org/cpython/rev/33c27b76a4d0
changeset:   88019:33c27b76a4d0
branch:      2.7
parent:      88002:4de09cbd3b97
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Tue Dec 17 14:40:06 2013 +0200
summary:
  Issue #17976: Fixed potential problem with file.write() not detecting IO error
by inspecting the return value of fwrite().  Based on patches by Jaakko Moisio
and test by Victor Stinner.

files:
  Lib/test/test_file2k.py |  8 ++++++++
  Misc/ACKS               |  1 +
  Misc/NEWS               |  4 ++++
  Objects/fileobject.c    |  6 +++++-
  4 files changed, 18 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_file2k.py b/Lib/test/test_file2k.py
--- a/Lib/test/test_file2k.py
+++ b/Lib/test/test_file2k.py
@@ -415,6 +415,14 @@
         finally:
             os.unlink(TESTFN)
 
+    @unittest.skipUnless(os.name == 'posix', 'test requires a posix system.')
+    def test_write_full(self):
+        # Issue #17976
+        with open('/dev/full', 'w', 1) as f:
+            with self.assertRaises(IOError):
+                f.write('hello')
+                f.write('\n')
+
 class FileSubclassTests(unittest.TestCase):
 
     def testExit(self):
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -710,6 +710,7 @@
 Dom Mitchell
 Florian Mladitsch
 Doug Moen
+Jaakko Moisio
 The Dragon De Monsyne
 Skip Montanaro
 Paul Moore
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,10 @@
 Core and Builtins
 -----------------
 
+- Issue #17976: Fixed potential problem with file.write() not detecting IO error
+  by inspecting the return value of fwrite().  Based on patches by Jaakko Moisio
+  and Victor Stinner.
+
 - Issue #14432: Generator now clears the borrowed reference to the thread
   state. Fix a crash when a generator is created in a C thread that is
   destroyed while the generator is still used. The issue was that a generator
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1804,6 +1804,7 @@
     const char *s;
     Py_ssize_t n, n2;
     PyObject *encoded = NULL;
+    int err = 0;
 
     if (f->f_fp == NULL)
         return err_closed();
@@ -1849,11 +1850,14 @@
     FILE_BEGIN_ALLOW_THREADS(f)
     errno = 0;
     n2 = fwrite(s, 1, n, f->f_fp);
+    if (n2 != n || ferror(f->f_fp))
+        err = errno;
     FILE_END_ALLOW_THREADS(f)
     Py_XDECREF(encoded);
     if (f->f_binary)
         PyBuffer_Release(&pbuf);
-    if (n2 != n) {
+    if (err) {
+        errno = err;
         PyErr_SetFromErrno(PyExc_IOError);
         clearerr(f->f_fp);
         return NULL;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list