]> granicus.if.org Git - python/commitdiff
Issue #17976: Fixed potential problem with file.write() not detecting IO error
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 17 Dec 2013 12:40:06 +0000 (14:40 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 17 Dec 2013 12:40:06 +0000 (14:40 +0200)
by inspecting the return value of fwrite().  Based on patches by Jaakko Moisio
and test by Victor Stinner.

Lib/test/test_file2k.py
Misc/ACKS
Misc/NEWS
Objects/fileobject.c

index 0c633b4e8fa3fd466545ad6598487d3d3c679926..5b90852b070359fc8a5385ad268947580510ea78 100644 (file)
@@ -415,6 +415,14 @@ class OtherFileTests(unittest.TestCase):
         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):
index cb2af182a530d7931ba3dc9f052e7e04aa37b491..35c9cf0e1177eca5621d84182efe211334a5cb2f 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -710,6 +710,7 @@ Dustin J. Mitchell
 Dom Mitchell
 Florian Mladitsch
 Doug Moen
+Jaakko Moisio
 The Dragon De Monsyne
 Skip Montanaro
 Paul Moore
index 5a921c9020e8f0dc7dd5eb4db92cf562862cc9cd..5f7485892ea369ffd2f36a057ae74d2f4052118a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,10 @@ What's New in Python 2.7.7?
 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
index 76cdf747770a4b6898607b5c1dbf50c1322cee03..d3b1cf6f3fa13dc9b161d9ecdd64e1ed5a117357 100644 (file)
@@ -1804,6 +1804,7 @@ file_write(PyFileObject *f, PyObject *args)
     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_write(PyFileObject *f, PyObject *args)
     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;