8000 [2.7] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. (GH-3201) by miss-islington · Pull Request #3548 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[2.7] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. (GH-3201) #3548

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
[2.7] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. (G…
…H-3201)

(cherry picked from commit a5b4ea1)
  • Loading branch information
orenmn authored and miss-islington committed Sep 13, 2017
commit 445706ebf086ecb30e362e090be32e485cb0ef8c
8 changes: 8 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2666,6 +2666,14 @@ class NonbytesStream(self.StringIO):
t = self.TextIOWrapper(NonbytesStream('a'))
self.assertEqual(t.read(), u'a')

def test_illegal_encoder(self):
# Issue 31271: Calling write() while the return value of encoder's
# encode() is invalid shouldn't cause an assertion failure.
rot13 = codecs.lookup("rot13")
with support.swap_attr(rot13, '_is_text_encoding', True):
t = io.TextIOWrapper(io.BytesIO(b'foo'), encoding="rot13")
self.assertRaises(TypeError, t.write, 'bar')

def test_illegal_decoder(self):
# Issue #17106
# Bypass the early encoding check added in issue 20404
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an assertion failure in the write() method of `io.TextIOWrapper`, when
the encoder doesn't return a bytes object. Patch by Oren Milman.
7 changes: 7 additions & 0 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,13 @@ textiowrapper_write(textio *self, PyObject *args)
Py_DECREF(text);
if (b == NULL)
return NULL;
if (!PyBytes_Check(b)) {
PyErr_Format(PyExc_TypeError,
"encoder should return a bytes object, not '%.200s'",
Py_TYPE(b)->tp_name);
Py_DECREF(b);
return NULL;
}

if (self->pending_bytes == NULL) {
self->pending_bytes = PyList_New(0);
Expand Down
0