]> granicus.if.org Git - python/commitdiff
Issue #7079: Fix a possible crash when closing a file object while using
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 17 May 2010 19:56:59 +0000 (19:56 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 17 May 2010 19:56:59 +0000 (19:56 +0000)
it from another thread.  Patch by Daniel Stutzbach.

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

index accdc9570313d231b3b6ca49838e7b106f02e0f2..da2ab6745099d7a2965464905153ea0c6a7f5926 100644 (file)
@@ -429,6 +429,7 @@ class FileThreadingTests(unittest.TestCase):
         self._count_lock = threading.Lock()
         self.close_count = 0
         self.close_success_count = 0
+        self.use_buffering = False
 
     def tearDown(self):
         if self.f:
@@ -443,7 +444,10 @@ class FileThreadingTests(unittest.TestCase):
         test_support.threading_cleanup(*self._threads)
 
     def _create_file(self):
-        self.f = open(self.filename, "w+")
+        if self.use_buffering:
+            self.f = open(self.filename, "w+", buffering=1024*16)
+        else:
+            self.f = open(self.filename, "w+")
 
     def _close_file(self):
         with self._count_lock:
@@ -530,6 +534,12 @@ class FileThreadingTests(unittest.TestCase):
             print >> self.f, ''
         self._test_close_open_io(io_func)
 
+    def test_close_open_print_buffered(self):
+        self.use_buffering = True
+        def io_func():
+            print >> self.f, ''
+        self._test_close_open_io(io_func)
+
     def test_close_open_read(self):
         def io_func():
             self.f.read(0)
index d2ceb22efecf799866d55095333546dd75611e5f..5cbc7670c3b48a1e620d0c7ca8f21c12e9e4761e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.7 Release Candidate 1?
 Core and Builtins
 -----------------
 
+- Issue #7079: Fix a possible crash when closing a file object while using
+  it from another thread.  Patch by Daniel Stutzbach.
+
 Library
 -------
 
index 6b95a0c212d10ce72ec350eff349220044d82496..d83c054cf3e0ebc9bf6163ff8820cba1e6b07e93 100644 (file)
@@ -649,8 +649,10 @@ static PyObject *
 file_close(PyFileObject *f)
 {
     PyObject *sts = close_the_file(f);
-    PyMem_Free(f->f_setbuf);
-    f->f_setbuf = NULL;
+    if (sts) {
+        PyMem_Free(f->f_setbuf);
+        f->f_setbuf = NULL;
+    }
     return sts;
 }