[Python-checkins] r87897 - in python/branches/py3k: Lib/_pyio.py Lib/test/test_io.py Misc/NEWS Modules/_io/textio.c

antoine.pitrou python-checkins at python.org
Sun Jan 9 21:38:15 CET 2011


Author: antoine.pitrou
Date: Sun Jan  9 21:38:15 2011
New Revision: 87897

Log:
Issue #10872: The repr() of TextIOWrapper objects now includes the mode
if available.

(at Georg's request)



Modified:
   python/branches/py3k/Lib/_pyio.py
   python/branches/py3k/Lib/test/test_io.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/_io/textio.c

Modified: python/branches/py3k/Lib/_pyio.py
==============================================================================
--- python/branches/py3k/Lib/_pyio.py	(original)
+++ python/branches/py3k/Lib/_pyio.py	Sun Jan  9 21:38:15 2011
@@ -1504,13 +1504,20 @@
     #   - "chars_..." for integer variables that count decoded characters
 
     def __repr__(self):
+        result = "<_pyio.TextIOWrapper"
         try:
             name = self.name
         except AttributeError:
-            return "<_pyio.TextIOWrapper encoding={0!r}>".format(self.encoding)
+            pass
+        else:
+            result += " name={0!r}".format(name)
+        try:
+            mode = self.mode
+        except AttributeError:
+            pass
         else:
-            return "<_pyio.TextIOWrapper name={0!r} encoding={1!r}>".format(
-                name, self.encoding)
+            result += " mode={0!r}".format(mode)
+        return result + " encoding={0!r}>".format(self.encoding)
 
     @property
     def encoding(self):

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	Sun Jan  9 21:38:15 2011
@@ -1717,9 +1717,12 @@
         raw.name = "dummy"
         self.assertEqual(repr(t),
                          "<%s.TextIOWrapper name='dummy' encoding='utf-8'>" % modname)
+        t.mode = "r"
+        self.assertEqual(repr(t),
+                         "<%s.TextIOWrapper name='dummy' mode='r' encoding='utf-8'>" % modname)
         raw.name = b"dummy"
         self.assertEqual(repr(t),
-                         "<%s.TextIOWrapper name=b'dummy' encoding='utf-8'>" % modname)
+                         "<%s.TextIOWrapper name=b'dummy' mode='r' encoding='utf-8'>" % modname)
 
     def test_line_buffering(self):
         r = self.BytesIO()

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sun Jan  9 21:38:15 2011
@@ -40,6 +40,9 @@
 Library
 -------
 
+- Issue #10872: The repr() of TextIOWrapper objects now includes the mode
+  if available.
+
 - Issue #10869: Fixed bug where ast.increment_lineno modified the root
   node twice.
 

Modified: python/branches/py3k/Modules/_io/textio.c
==============================================================================
--- python/branches/py3k/Modules/_io/textio.c	(original)
+++ python/branches/py3k/Modules/_io/textio.c	Sun Jan  9 21:38:15 2011
@@ -2323,25 +2323,52 @@
 static PyObject *
 textiowrapper_repr(textio *self)
 {
-    PyObject *nameobj, *res;
+    PyObject *nameobj, *modeobj, *res, *s;
 
     CHECK_INITIALIZED(self);
 
+    res = PyUnicode_FromString("<_io.TextIOWrapper");
+    if (res == NULL)
+        return NULL;
     nameobj = PyObject_GetAttrString((PyObject *) self, "name");
     if (nameobj == NULL) {
         if (PyErr_ExceptionMatches(PyExc_AttributeError))
             PyErr_Clear();
         else
-            return NULL;
-        res = PyUnicode_FromFormat("<_io.TextIOWrapper encoding=%R>",
-                                   self->encoding);
+            goto error;
     }
     else {
-        res = PyUnicode_FromFormat("<_io.TextIOWrapper name=%R encoding=%R>",
-                                   nameobj, self->encoding);
+        s = PyUnicode_FromFormat(" name=%R", nameobj);
         Py_DECREF(nameobj);
+        if (s == NULL)
+            goto error;
+        PyUnicode_AppendAndDel(&res, s);
+        if (res == NULL)
+            return NULL;
     }
-    return res;
+    modeobj = PyObject_GetAttrString((PyObject *) self, "mode");
+    if (modeobj == NULL) {
+        if (PyErr_ExceptionMatches(PyExc_AttributeError))
+            PyErr_Clear();
+        else
+            goto error;
+    }
+    else {
+        s = PyUnicode_FromFormat(" mode=%R", modeobj);
+        Py_DECREF(modeobj);
+        if (s == NULL)
+            goto error;
+        PyUnicode_AppendAndDel(&res, s);
+        if (res == NULL)
+            return NULL;
+    }
+    s = PyUnicode_FromFormat("%U encoding=%R>",
+                             res, self->encoding);
+    Py_DECREF(res);
+    return s;
+error:
+    Py_XDECREF(res);
+    return NULL;
 }
 
 


More information about the Python-checkins mailing list