Skip to content

Commit 2aa975a

Browse files
committed
In _io_TextIOWrapper_read_impl skip decoding if bytes are None
1 parent 901ea41 commit 2aa975a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Lib/test/test_io.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4878,6 +4878,19 @@ def test_interrupted_write_retry_buffered(self):
48784878
def test_interrupted_write_retry_text(self):
48794879
self.check_interrupted_write_retry("x", mode="w", encoding="latin1")
48804880

4881+
def test_read_non_blocking(self):
4882+
import os
4883+
4884+
r, w = os.pipe()
4885+
try:
4886+
os.set_blocking(r, False)
4887+
with open(r, 'r') as textfile:
4888+
r = None
4889+
self.assertIsNone(textfile.read())
4890+
finally:
4891+
if r is not None:
4892+
os.close(r)
4893+
os.close(w)
48814894

48824895
class CSignalsTest(SignalsTest):
48834896
io = io

Modules/_io/textio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,10 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
19921992
if (bytes == NULL)
19931993
goto fail;
19941994

1995+
if (bytes == Py_None) {
1996+
return Py_None;
1997+
}
1998+
19951999
_PyIO_State *state = self->state;
19962000
if (Py_IS_TYPE(self->decoder, state->PyIncrementalNewlineDecoder_Type))
19972001
decoded = _PyIncrementalNewlineDecoder_decode(self->decoder,

0 commit comments

Comments
 (0)