]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
give TextIOWrapper a repr that tells you the encoding
authorBenjamin Peterson <benjamin@python.org>
Mon, 9 Mar 2009 00:07:03 +0000 (00:07 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 9 Mar 2009 00:07:03 +0000 (00:07 +0000)
Lib/_pyio.py
Lib/test/test_io.py
Modules/_textio.c

index c6811dbb2b953615fd0ed6e17dd4b5468edd31be..535c9ca433cce3c26cf722d08c386c0774446e0f 100644 (file)
@@ -1399,6 +1399,9 @@ class TextIOWrapper(TextIOBase):
     #   - "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
index 5fc53eac22b439d88fbd2ee16fddc340cc0e1579..ef3fed9dc695ef0df732d8f2c10f5d3f01718a2b 100644 (file)
@@ -1354,6 +1354,12 @@ class TextIOWrapperTest(unittest.TestCase):
         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)
index c70b1dd55b4fa194554649b7508863ad4aeb36de..a61e6bc780e31ae9b1afdccdbbf7ced4367858ca 100644 (file)
@@ -2171,6 +2171,14 @@ TextIOWrapper_truncate(PyTextIOWrapperObject *self, PyObject *args)
     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 @@ PyTypeObject PyTextIOWrapper_Type = {
     (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*/