summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-01-09 20:38:15 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-01-09 20:38:15 (GMT)
commita4815caa7ccf21aa994d0e0eec66873072f0e352 (patch)
tree5ccb44937ddfdd59ebae4590d2f4cfc05dcced3d /Modules/_io
parenta6c91f5e3b013be1447454b3a479d6fabbf16806 (diff)
downloadcpython-a4815caa7ccf21aa994d0e0eec66873072f0e352.zip
cpython-a4815caa7ccf21aa994d0e0eec66873072f0e352.tar.gz
cpython-a4815caa7ccf21aa994d0e0eec66873072f0e352.tar.bz2
Issue #10872: The repr() of TextIOWrapper objects now includes the mode
if available. (at Georg's request)
Diffstat (limited to 'Modules/_io')
-rw-r--r--Modules/_io/textio.c41
1 files changed, 34 insertions, 7 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 2559714..73d83a1 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2323,25 +2323,52 @@ textiowrapper_truncate(textio *self, PyObject *args)
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;
}