From 445706ebf086ecb30e362e090be32e485cb0ef8c Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 25 Aug 2017 21:14:54 +0300 Subject: [PATCH] [2.7] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. (GH-3201) (cherry picked from commit a5b4ea15b61e3f3985f4f0748a18f8b888a63532) --- Lib/test/test_io.py | 8 ++++++++ .../2017-08-25-20-43-22.bpo-31271.YMduKF.rst | 2 ++ Modules/_io/textio.c | 7 +++++++ 3 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-08-25-20-43-22.bpo-31271.YMduKF.rst diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 3dc6e9ffcac157..dd102c42e231bb 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -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 diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-08-25-20-43-22.bpo-31271.YMduKF.rst b/Misc/NEWS.d/next/Core and Builtins/2017-08-25-20-43-22.bpo-31271.YMduKF.rst new file mode 100644 index 00000000000000..7bb7880105781e --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-08-25-20-43-22.bpo-31271.YMduKF.rst @@ -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. diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index d9e036db386d3b..6d24755085a479 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -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);