[Python-checkins] r70251 - in python/branches/py3k: Lib/_pyio.py Lib/test/test_io.py Modules/_textio.c

benjamin.peterson python-checkins at python.org
Mon Mar 9 01:07:03 CET 2009


Author: benjamin.peterson
Date: Mon Mar  9 01:07:03 2009
New Revision: 70251

Log:
give TextIOWrapper a repr that tells you the encoding

Modified:
   python/branches/py3k/Lib/_pyio.py
   python/branches/py3k/Lib/test/test_io.py
   python/branches/py3k/Modules/_textio.c

Modified: python/branches/py3k/Lib/_pyio.py
==============================================================================
--- python/branches/py3k/Lib/_pyio.py	(original)
+++ python/branches/py3k/Lib/_pyio.py	Mon Mar  9 01:07:03 2009
@@ -1399,6 +1399,9 @@
     #   - "bytes_..." for integer variables that count input bytes
     #   - "chars_..." for integer variables that count decoded characters
 
+    def __repr__(self):
+        return "<TextIOWrapper encoding={0}>".format(self.encoding)
+
     @property
     def encoding(self):
         return self._encoding

Modified: python/branches/py3k/Lib/test/test_io.py
==============================================================================
--- python/branches/py3k/Lib/test/test_io.py	(original)
+++ python/branches/py3k/Lib/test/test_io.py	Mon Mar  9 01:07:03 2009
@@ -1354,6 +1354,12 @@
         self.assertRaises(TypeError, t.__init__, b, newline=42)
         self.assertRaises(ValueError, t.__init__, b, newline='xyzzy')
 
+    def test_repr(self):
+        raw = self.BytesIO("hello".encode("utf-8"))
+        b = self.BufferedReader(raw)
+        t = self.TextIOWrapper(b, encoding="utf-8")
+        self.assertEqual(repr(t), "<TextIOWrapper encoding=utf-8>")
+
     def test_line_buffering(self):
         r = self.BytesIO()
         b = self.BufferedWriter(r, 1000)

Modified: python/branches/py3k/Modules/_textio.c
==============================================================================
--- python/branches/py3k/Modules/_textio.c	(original)
+++ python/branches/py3k/Modules/_textio.c	Mon Mar  9 01:07:03 2009
@@ -2171,6 +2171,14 @@
     return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, NULL);
 }
 
+static PyObject *
+TextIOWrapper_repr(PyTextIOWrapperObject *self)
+{
+  CHECK_INITIALIZED(self);
+  return PyUnicode_FromFormat("<TextIOWrapper encoding=%S>", self->encoding);
+}
+
+
 /* Inquiries */
 
 static PyObject *
@@ -2372,9 +2380,9 @@
     (destructor)TextIOWrapper_dealloc, /*tp_dealloc*/
     0,                          /*tp_print*/
     0,                          /*tp_getattr*/
-    0,                          /*tp_setattr*/
+    0,                          /*tps_etattr*/
     0,                          /*tp_compare */
-    0,                          /*tp_repr*/
+    (reprfunc)TextIOWrapper_repr,/*tp_repr*/
     0,                          /*tp_as_number*/
     0,                          /*tp_as_sequence*/
     0,                          /*tp_as_mapping*/


More information about the Python-checkins mailing list