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
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);