mirror of
https://github.com/python/cpython.git
synced 2025-09-14 04:37:29 +00:00
#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
This commit is contained in:
parent
3092ed977f
commit
fff896b309
3 changed files with 33 additions and 4 deletions
|
@ -2061,6 +2061,27 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
self.assertEqual(f.errors, "replace")
|
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):
|
class CTextIOWrapperTest(TextIOWrapperTest):
|
||||||
|
|
||||||
def test_initialization(self):
|
def test_initialization(self):
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
|
||||||
Core and Builtins
|
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 #6704: Improve the col_offset in AST for "for" statements with
|
- Issue #6704: Improve the col_offset in AST for "for" statements with
|
||||||
a target of tuple unpacking.
|
a target of tuple unpacking.
|
||||||
|
|
||||||
|
|
|
@ -1189,11 +1189,18 @@ findchar(const Py_UNICODE *s, Py_ssize_t size, Py_UNICODE ch)
|
||||||
static int
|
static int
|
||||||
_textiowrapper_writeflush(textio *self)
|
_textiowrapper_writeflush(textio *self)
|
||||||
{
|
{
|
||||||
PyObject *b, *ret;
|
PyObject *pending, *b, *ret;
|
||||||
|
|
||||||
if (self->pending_bytes == NULL)
|
if (self->pending_bytes == NULL)
|
||||||
return 0;
|
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)
|
if (b == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
ret = PyObject_CallMethodObjArgs(self->buffer,
|
ret = PyObject_CallMethodObjArgs(self->buffer,
|
||||||
|
@ -1202,8 +1209,6 @@ _textiowrapper_writeflush(textio *self)
|
||||||
if (ret == NULL)
|
if (ret == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
Py_DECREF(ret);
|
Py_DECREF(ret);
|
||||||
Py_CLEAR(self->pending_bytes);
|
|
||||||
self->pending_bytes_count = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue