Issue 31775: Support unbuffered TextIOWrapper (original) (raw)

Created on 2017-10-12 15:09 by vstinner, last changed 2022-04-11 14:58 by admin.

Messages (7)
msg304250 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-12 15:09
It seems like that some methods of the io.TextIOWrapper class requires that its buffer object has the read1() method, whereas the constructor checks if the buffer has a read1() method and the TextIOWrapper _read_chunk() method is able to call buffer.read() if buffer doesn't have read1(). This issue may help to get fully unbuffered sys.stdin, at least when replaced manually: stdin = sys.stdin sys.stdin = open(0, "r", buffering=0, encoding=stdin.encoding, errors=stdin.errors, newline=stdin.newline)
msg304252 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-12 15:12
I created this issue because of this comment in create_stdio() of Python/pylifecycle.c: --- /* stdin is always opened in buffered mode, first because it shouldn't make a difference in common use cases, second because TextIOWrapper depends on the presence of a read1() method which only exists on buffered streams. */ if (Py_UnbufferedStdioFlag && write_mode) buffering = 0; else buffering = -1; ---
msg304253 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-12 15:25
Oh, currently it's not possible to create an unbuffered read-only TextIOWrapper object: haypo@selma$ python3 Python 3.6.2 (default, Oct 2 2017, 16:51:32) >>> open("/etc/issue", "r", 0) Traceback (most recent call last): File "", line 1, in ValueError: can't have unbuffered text I/O
msg304258 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-12 15:30
The falling back to read() was implemented in . What methods still require read1()?
msg304260 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-12 15:46
> The falling back to read() was implemented in . What methods still require read1()? I read the Python implementation of TextIOWrapper in _pyio: * seek() calls buffer.read() "if chars_to_skip:"... not sure that it can happen if buffer is a FileIO? * read(None) and read(-1) to read "everything" (until EOF): buffer.read() It seems like read(None) also calls buffer.read() in the C implementation, _io_TextIOWrapper_read_impl(): /* Read everything */ PyObject *bytes = _PyObject_CallMethodId(self->buffer, &PyId_read, NULL); PyObject *decoded; if (bytes == NULL) goto fail; and in _io_TextIOWrapper_seek_impl(): /* Just like _read_chunk, feed the decoder and save a snapshot. */ PyObject *input_chunk = _PyObject_CallMethodId( self->buffer, &PyId_read, "i", cookie.bytes_to_feed);
msg304262 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-12 15:48
Context: I created this issue as a follow-up of the discussion on the -u command line option, bpo-28647, which still leaves sys.stdin *buffered* in the current master branch.
msg304266 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-12 16:19
What methods still require read1()?
History
Date User Action Args
2022-04-11 14:58:53 admin set status: pending -> opengithub: 75956
2017-10-28 12:09:57 serhiy.storchaka set status: open -> pending
2017-10-12 16:19:18 serhiy.storchaka set messages: +
2017-10-12 15:48:40 vstinner set messages: +
2017-10-12 15:46:21 vstinner set messages: +
2017-10-12 15:30:50 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2017-10-12 15:25:04 vstinner set messages: +
2017-10-12 15:12:26 vstinner set messages: +
2017-10-12 15:09:32 vstinner create