[pypy-svn] pypy default: Implement TextIOWrapper.__repr__ so it shows the encoding.

alex_gaynor commits-noreply at bitbucket.org
Wed Jan 26 06:51:44 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41333:29cb99312b59
Date: 2011-01-26 00:51 -0500
http://bitbucket.org/pypy/pypy/changeset/29cb99312b59/

Log:	Implement TextIOWrapper.__repr__ so it shows the 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
@@ -11,6 +11,7 @@
 from pypy.module._io.interp_iobase import convert_size
 import sys
 
+
 STATE_ZERO, STATE_OK, STATE_DETACHED = range(3)
 
 SEEN_CR   = 1
@@ -371,6 +372,12 @@
         W_TextIOBase._check_closed(self, space, message)
 
     @unwrap_spec('self', ObjSpace)
+    def descr_repr(self, space):
+        return space.mod(
+            space.wrap("<_io.TextIOWrapper encoding=%r>"), self.w_encoding
+        )
+
+    @unwrap_spec('self', ObjSpace)
     def readable_w(self, space):
         self._check_init(space)
         return space.call_method(self.w_buffer, "readable")
@@ -913,6 +920,7 @@
     'TextIOWrapper', W_TextIOBase.typedef,
     __new__ = generic_new_descr(W_TextIOWrapper),
     __init__  = interp2app(W_TextIOWrapper.descr_init),
+    __repr__ = interp2app(W_TextIOWrapper.descr_repr),
 
     read = interp2app(W_TextIOWrapper.read_w),
     readline = interp2app(W_TextIOWrapper.readline_w),
@@ -929,4 +937,4 @@
     seekable = interp2app(W_TextIOWrapper.seekable_w),
     fileno = interp2app(W_TextIOWrapper.fileno_w),
     closed = GetSetProperty(W_TextIOWrapper.closed_get_w),
-    )
+)

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
@@ -166,6 +166,15 @@
                         assert got_line == exp_line
                     assert len(got_lines) == len(exp_lines)
 
+    def test_repr(self):
+        import _io
+
+        t = _io.TextIOWrapper(_io.BytesIO(""), encoding="utf-8")
+        assert repr(t) == "<_io.TextIOWrapper encoding='utf-8'>"
+        t = _io.TextIOWrapper(_io.BytesIO(""), encoding="ascii")
+        assert repr(t) == "<_io.TextIOWrapper encoding='ascii'>"
+
+
 class AppTestIncrementalNewlineDecoder:
 
     def test_newline_decoder(self):


More information about the Pypy-commit mailing list