Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d42c1d0

Browse files
committed
Issue #12591: Allow io.TextIOWrapper to work with raw IO objects (without
a read1() method), and add a *write_through* parameter to mandate unbuffered writes.
2 parents f23339a + e96ec68 commit d42c1d0

4 files changed

Lines changed: 45 additions & 8 deletions

File tree

Lib/_pyio.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ class TextIOWrapper(TextIOBase):
14781478
_CHUNK_SIZE = 2048
14791479

14801480
def __init__(self, buffer, encoding=None, errors=None, newline=None,
1481-
line_buffering=False):
1481+
line_buffering=False, write_through=False):
14821482
if newline is not None and not isinstance(newline, str):
14831483
raise TypeError("illegal newline type: %r" % (type(newline),))
14841484
if newline not in (None, "", "\n", "\r", "\r\n"):
@@ -1521,6 +1521,7 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
15211521
self._decoded_chars_used = 0 # offset into _decoded_chars for read()
15221522
self._snapshot = None # info for reconstructing decoder state
15231523
self._seekable = self._telling = self.buffer.seekable()
1524+
self._has_read1 = hasattr(self.buffer, 'read1')
15241525
self._b2cratio = 0.0
15251526

15261527
if self._seekable and self.writable():
@@ -1687,7 +1688,10 @@ def _read_chunk(self):
16871688
# len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
16881689

16891690
# Read a chunk, decode it, and put the result in self._decoded_chars.
1690-
input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1691+
if self._has_read1:
1692+
input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1693+
else:
1694+
input_chunk = self.buffer.read(self._CHUNK_SIZE)
16911695
eof = not input_chunk
16921696
decoded_chars = self._decoder.decode(input_chunk, eof)
16931697
self._set_decoded_chars(decoded_chars)

Lib/test/test_io.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,27 @@ def test_readonly_attributes(self):
23142314
with self.assertRaises(AttributeError):
23152315
txt.buffer = buf
23162316

2317+
def test_rawio(self):
2318+
# Issue #12591: TextIOWrapper must work with raw I/O objects, so
2319+
# that subprocess.Popen() can have the required unbuffered
2320+
# semantics with universal_newlines=True.
2321+
raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n'])
2322+
txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n')
2323+
# Reads
2324+
self.assertEqual(txt.read(4), 'abcd')
2325+
self.assertEqual(txt.readline(), 'efghi\n')
2326+
self.assertEqual(list(txt), ['jkl\n', 'opq\n'])
2327+
2328+
def test_rawio_write_through(self):
2329+
# Issue #12591: with write_through=True, writes don't need a flush
2330+
raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n'])
2331+
txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n',
2332+
write_through=True)
2333+
txt.write('1')
2334+
txt.write('23\n4')
2335+
txt.write('5')
2336+
self.assertEqual(b''.join(raw._write_stack), b'123\n45')
2337+
23172338
class CTextIOWrapperTest(TextIOWrapperTest):
23182339

23192340
def test_initialization(self):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ Core and Builtins
237237
Library
238238
-------
239239

240+
- Issue #12591: Allow io.TextIOWrapper to work with raw IO objects (without
241+
a read1() method), and add a *write_through* parameter to mandate
242+
unbuffered writes.
243+
240244
- Issue #10883: Fix socket leaks in urllib.request when using FTP.
241245

242246
- Issue #12592: Make Python build on OpenBSD 5 (and future major releases).

Modules/_io/textio.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,12 @@ typedef struct
653653
PyObject *errors;
654654
const char *writenl; /* utf-8 encoded, NULL stands for \n */
655655
char line_buffering;
656+
char write_through;
656657
char readuniversal;
657658
char readtranslate;
658659
char writetranslate;
659660
char seekable;
661+
char has_read1;
660662
char telling;
661663
char deallocating;
662664
/* Specialized encoding func (see below) */
@@ -813,23 +815,23 @@ static int
813815
textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
814816
{
815817
char *kwlist[] = {"buffer", "encoding", "errors",
816-
"newline", "line_buffering",
818+
"newline", "line_buffering", "write_through",
817819
NULL};
818820
PyObject *buffer, *raw;
819821
char *encoding = NULL;
820822
char *errors = NULL;
821823
char *newline = NULL;
822-
int line_buffering = 0;
824+
int line_buffering = 0, write_through = 0;
823825
_PyIO_State *state = IO_STATE;
824826

825827
PyObject *res;
826828
int r;
827829

828830
self->ok = 0;
829831
self->detached = 0;
830-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzzi:fileio",
832+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzzii:fileio",
831833
kwlist, &buffer, &encoding, &errors,
832-
&newline, &line_buffering))
834+
&newline, &line_buffering, &write_through))
833835
return -1;
834836

835837
if (newline && newline[0] != '\0'
@@ -935,6 +937,7 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
935937
self->chunk_size = 8192;
936938
self->readuniversal = (newline == NULL || newline[0] == '\0');
937939
self->line_buffering = line_buffering;
940+
self->write_through = write_through;
938941
self->readtranslate = (newline == NULL);
939942
if (newline) {
940943
self->readnl = PyUnicode_FromString(newline);
@@ -1044,6 +1047,8 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
10441047
self->seekable = self->telling = PyObject_IsTrue(res);
10451048
Py_DECREF(res);
10461049

1050+
self->has_read1 = PyObject_HasAttrString(buffer, "read1");
1051+
10471052
self->encoding_start_of_stream = 0;
10481053
if (self->seekable && self->encoder) {
10491054
PyObject *cookieObj;
@@ -1287,7 +1292,9 @@ textiowrapper_write(textio *self, PyObject *args)
12871292
text = newtext;
12881293
}
12891294

1290-
if (self->line_buffering &&
1295+
if (self->write_through)
1296+
needflush = 1;
1297+
else if (self->line_buffering &&
12911298
(haslf ||
12921299
findchar(PyUnicode_AS_UNICODE(text),
12931300
PyUnicode_GET_SIZE(text), '\r')))
@@ -1435,7 +1442,8 @@ textiowrapper_read_chunk(textio *self)
14351442
if (chunk_size == NULL)
14361443
goto fail;
14371444
input_chunk = PyObject_CallMethodObjArgs(self->buffer,
1438-
_PyIO_str_read1, chunk_size, NULL);
1445+
(self->has_read1 ? _PyIO_str_read1: _PyIO_str_read),
1446+
chunk_size, NULL);
14391447
Py_DECREF(chunk_size);
14401448
if (input_chunk == NULL)
14411449
goto fail;

0 commit comments

Comments
 (0)