Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-43260: io: Prevent large data remains in textio buffer.#24592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
3ba68b0 bpo-43260: io: Prevent large data remains in textio.
methane 5ffbe8f Add news entry
methane ccda9bc Use chunk_size instead of heuristic 1MiB
methane a17a9f4 Use Eryk's idea
methane 54ff77e fix comment
methane 960b928 <=
methane 9572c1e concatenate
methane 05ab6e0 Add test
methane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2021-02-20-12-15-29.bpo-43260.6znAas.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix TextIOWrapper can not flush internal buffer forever after very large | ||
| text is written. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1585,6 +1585,8 @@ _textiowrapper_writeflush(textio *self) | ||
| ret = PyObject_CallMethodOneArg(self->buffer, _PyIO_str_write, b); | ||
| } while (ret == NULL && _PyIO_trap_eintr()); | ||
| Py_DECREF(b); | ||
| // NOTE: We cleared buffer but we don't know how many bytes are actually written | ||
| // when an error occurred. | ||
| if (ret == NULL) | ||
| return -1; | ||
| Py_DECREF(ret); | ||
| @@ -1642,7 +1644,10 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) | ||
| /* XXX What if we were just reading? */ | ||
| if (self->encodefunc != NULL){ | ||
| if (PyUnicode_IS_ASCII(text) && is_asciicompat_encoding(self->encodefunc)){ | ||
| if (PyUnicode_IS_ASCII(text) && | ||
| // See bpo-43260 | ||
| PyUnicode_GET_LENGTH(text) <= self->chunk_size && | ||
| is_asciicompat_encoding(self->encodefunc)){ | ||
| b = text; | ||
| Py_INCREF(b); | ||
| } | ||
| @@ -1651,8 +1656,9 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) | ||
| } | ||
| self->encoding_start_of_stream = 0; | ||
| } | ||
| else | ||
| else{ | ||
| b = PyObject_CallMethodOneArg(self->encoder, _PyIO_str_encode, text); | ||
| } | ||
| Py_DECREF(text); | ||
| if (b == NULL) | ||
| @@ -1677,6 +1683,14 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) | ||
| self->pending_bytes_count = 0; | ||
| self->pending_bytes = b; | ||
| } | ||
| else if (self->pending_bytes_count + bytes_len > self->chunk_size){ | ||
| // Prevent to concatenate more than chunk_size data. | ||
| if (_textiowrapper_writeflush(self) < 0){ | ||
methane marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| Py_DECREF(b); | ||
| return NULL; | ||
| } | ||
| self->pending_bytes = b; | ||
| } | ||
| else if (!PyList_CheckExact(self->pending_bytes)){ | ||
| PyObject *list = PyList_New(2); | ||
| if (list == NULL){ | ||
| @@ -1696,7 +1710,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) | ||
| } | ||
| self->pending_bytes_count += bytes_len; | ||
| if (self->pending_bytes_count > self->chunk_size || needflush || | ||
| if (self->pending_bytes_count >= self->chunk_size || needflush || | ||
methane marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| text_needflush){ | ||
| if (_textiowrapper_writeflush(self) < 0) | ||
| return NULL; | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.