FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159739: Validate newline replacement in TextIOWrapper.write by jelle-openai · Pull Request #160191 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions Lib/test/test_io/test_textio.py
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
Expand Up @@ -1448,6 +1448,15 @@ def _to_memoryview(buf):
class CTextIOWrapperTest(TextIOWrapperTest, CTestCase):
shutdown_error = "LookupError: unknown encoding: ascii"

def test_write_newline_replace_returns_non_string(self):
class Text(str):
def replace(self, *args):
return None

with self.TextIOWrapper(self.BytesIO(), newline='\r\n') as stream:
with self.assertRaisesRegex(TypeError, 'replace.*str'):
stream.write(Text('a\n'))

def test_chunk_size(self):
t = self.TextIOWrapper(self.BytesIO(), encoding="utf-8")
self.assertGreater(t._CHUNK_SIZE, 0)
Expand Down
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 @@
Raise :exc:`TypeError` if a string subclass returns a non-string during newline translation in :class:`io.TextIOWrapper`.
6 changes: 6 additions & 0 deletions Modules/_io/textio.c
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
Expand Up @@ -1768,6 +1768,12 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
Py_DECREF(text);
if (newtext == NULL)
return NULL;
if (!PyUnicode_Check(newtext)) {
PyErr_Format(PyExc_TypeError,
"replace() should return a str, not %T", newtext);
Py_DECREF(newtext);
return NULL;
}
text = newtext;
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL