[pypy-commit] pypy py3.3: TextIOWrapper: Ensure the constructor complains if passed a codec that isn't

amauryfa pypy.commits at gmail.com
Tue Jan 26 18:10:09 EST 2016


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3.3
Changeset: r81960:5ddd2d347061
Date: 2016-01-27 00:03 +0100
http://bitbucket.org/pypy/pypy/changeset/5ddd2d347061/

Log:	TextIOWrapper: Ensure the constructor complains if passed a codec
	that isn't marked as a text encoding.

diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -413,10 +413,16 @@
         else:
             self.writenl = None
 
+        w_codec = interp_codecs.lookup_codec(space,
+                                             space.str_w(self.w_encoding))
+        if not space.is_true(space.getattr(w_codec,
+                                           space.wrap('_is_text_encoding'))):
+            msg = ("%R is not a text encoding; "
+                   "use codecs.open() to handle arbitrary codecs")
+            raise oefmt(space.w_LookupError, msg, self.w_encoding)
+
         # build the decoder object
         if space.is_true(space.call_method(w_buffer, "readable")):
-            w_codec = interp_codecs.lookup_codec(space,
-                                                 space.str_w(self.w_encoding))
             self.w_decoder = space.call_method(w_codec,
                                                "incrementaldecoder", w_errors)
             if self.readuniversal:
@@ -426,8 +432,6 @@
 
         # build the encoder object
         if space.is_true(space.call_method(w_buffer, "writable")):
-            w_codec = interp_codecs.lookup_codec(space,
-                                                 space.str_w(self.w_encoding))
             self.w_encoder = space.call_method(w_codec,
                                                "incrementalencoder", w_errors)
 
diff --git a/pypy/module/_io/test/test_textio.py b/pypy/module/_io/test/test_textio.py
--- a/pypy/module/_io/test/test_textio.py
+++ b/pypy/module/_io/test/test_textio.py
@@ -367,15 +367,8 @@
 
     def test_illegal_decoder(self):
         import _io
-        t = _io.TextIOWrapper(_io.BytesIO(b'aaaaaa'), newline='\n',
-                             encoding='quopri_codec')
-        raises(TypeError, t.read, 1)
-        t = _io.TextIOWrapper(_io.BytesIO(b'aaaaaa'), newline='\n',
-                             encoding='quopri_codec')
-        raises(TypeError, t.readline)
-        t = _io.TextIOWrapper(_io.BytesIO(b'aaaaaa'), newline='\n',
-                             encoding='quopri_codec')
-        raises(TypeError, t.read)
+        raises(LookupError, _io.TextIOWrapper, _io.BytesIO(),
+               encoding='quopri_codec')
 
     def test_read_nonbytes(self):
         import _io


More information about the pypy-commit mailing list