Skip to content

Commit

Permalink
[3.6] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. (G…
Browse files Browse the repository at this point in the history
…H-3201) (#3209)

(cherry picked from commit a5b4ea1)
  • Loading branch information
orenmn authored and serhiy-storchaka committed Aug 26, 2017
1 parent 8e67981 commit 9bcbc6c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lib/test/test_io.py
Expand Up @@ -3163,6 +3163,14 @@ def test_read_nonbytes(self):
t = self.TextIOWrapper(self.StringIO('a'))
self.assertRaises(TypeError, t.read)

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
@@ -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
Expand Up @@ -1331,6 +1331,13 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
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 comments on commit 9bcbc6c

Please sign in to comment.