Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_io/test_bufferedio.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,28 @@ def test_bad_readinto_type(self):
bufio.readline()
self.assertIsInstance(cm.exception.__cause__, TypeError)

def test_read1_error_does_not_cause_reentrant_failure(self):
Comment thread
sobolevn marked this conversation as resolved.
# 32-bit builds (e.g. win32) can raise OverflowError
# converting huge Python int to Py_ssize_t.
if sys.maxsize <= 2**32:
Comment thread
cmaloney marked this conversation as resolved.
Outdated
self.skipTest("requires 64-bit build")
# Under TSan, the process may abort on huge allocation
# attempts (exit code 66).
if support.check_sanitizer(thread=True):
self.skipTest("TSan aborts on huge allocations")
Comment thread
cmaloney marked this conversation as resolved.
Outdated

self.addCleanup(os_helper.unlink, os_helper.TESTFN)
with self.open(os_helper.TESTFN, "wb") as f:
f.write(b"hello")

with self.open(os_helper.TESTFN, "rb", buffering=0) as raw:
bufio = self.tp(raw, buffer_size=8)
huge = 10**18
Comment thread
sobolevn marked this conversation as resolved.
Outdated
with self.assertRaises(MemoryError):
bufio.read1(huge)

self.assertEqual(bufio.read1(1), b"h")
Comment thread
hyongtao-code marked this conversation as resolved.


class PyBufferedReaderTest(BufferedReaderTest, PyTestCase):
tp = pyio.BufferedReader
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed BufferedReader.read1() leaving the object in a reentrant state after an error.
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
1 change: 1 addition & 0 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ _io__Buffered_read1_impl(buffered *self, Py_ssize_t n)

PyBytesWriter *writer = PyBytesWriter_Create(n);
if (writer == NULL) {
LEAVE_BUFFERED(self)
return NULL;
}

Expand Down
Loading