]> granicus.if.org Git - python/commitdiff
Merged revisions 74582 via svnmerge from
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sat, 29 Aug 2009 23:19:16 +0000 (23:19 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sat, 29 Aug 2009 23:19:16 +0000 (23:19 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r74582 | amaury.forgeotdarc | 2009-08-30 01:00:38 +0200 (dim., 30 août 2009) | 10 lines

  Merged revisions 74581 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r74581 | amaury.forgeotdarc | 2009-08-29 20:14:40 +0200 (sam., 29 août 2009) | 3 lines

    #6750: TextIOWrapped could duplicate output when several threads write to it.
    this affect text files opened with io.open(), and the print() function of py3k
  ........
................

Lib/test/test_io.py
Misc/NEWS
Modules/_io/textio.c

index a9094d9431638ed2bc894719174497a9c5c24f43..150eff48bbc79efe566daa5c415a70aac8d79a94 100644 (file)
@@ -2051,6 +2051,27 @@ class TextIOWrapperTest(unittest.TestCase):
             self.assertEqual(f.errors, "replace")
 
 
+    def test_threads_write(self):
+        # Issue6750: concurrent writes could duplicate data
+        event = threading.Event()
+        with self.open(support.TESTFN, "w", buffering=1) as f:
+            def run(n):
+                text = "Thread%03d\n" % n
+                event.wait()
+                f.write(text)
+            threads = [threading.Thread(target=lambda n=x: run(n))
+                       for x in range(20)]
+            for t in threads:
+                t.start()
+            time.sleep(0.02)
+            event.set()
+            for t in threads:
+                t.join()
+        with self.open(support.TESTFN) as f:
+            content = f.read()
+            for n in range(20):
+                self.assertEquals(content.count("Thread%03d\n" % n), 1)
+
 class CTextIOWrapperTest(TextIOWrapperTest):
 
     def test_initialization(self):
index da8cdf500c782b1d8ed15efcc6a5a54cbbc56af5..b60d8e57bce18afcfd6159f53b5bc625ced2cd03 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.1.1?
 Core and Builtins
 -----------------
 
+- Issue #6750: A text file opened with io.open() could duplicate its output
+  when writing from multiple threads at the same time.
+
 - Issue #6707: dir() on an uninitialized module caused a crash.
 
 - Issue #6540: Fixed crash for bytearray.translate() with invalid parameters.
index 0f5a73d4203b3974a8b70744b05b7c1d316a38e6..b91852e3516439a4c12fb3e35c8206a563a4933d 100644 (file)
@@ -1213,11 +1213,18 @@ findchar(const Py_UNICODE *s, Py_ssize_t size, Py_UNICODE ch)
 static int
 _textiowrapper_writeflush(textio *self)
 {
-    PyObject *b, *ret;
+    PyObject *pending, *b, *ret;
 
     if (self->pending_bytes == NULL)
         return 0;
-    b = _PyBytes_Join(_PyIO_empty_bytes, self->pending_bytes);
+
+    pending = self->pending_bytes;
+    Py_INCREF(pending);
+    self->pending_bytes_count = 0;
+    Py_CLEAR(self->pending_bytes);
+
+    b = _PyBytes_Join(_PyIO_empty_bytes, pending);
+    Py_DECREF(pending);
     if (b == NULL)
         return -1;
     ret = PyObject_CallMethodObjArgs(self->buffer,
@@ -1226,8 +1233,6 @@ _textiowrapper_writeflush(textio *self)
     if (ret == NULL)
         return -1;
     Py_DECREF(ret);
-    Py_CLEAR(self->pending_bytes);
-    self->pending_bytes_count = 0;
     return 0;
 }