Issue #5761: Add the name of the underlying file to the repr() of various IO objects.

This commit is contained in:
Antoine Pitrou 2009-05-23 19:04:03 +00:00
parent 744af44064
commit 716c444edc
7 changed files with 107 additions and 14 deletions

View file

@ -2308,8 +2308,25 @@ TextIOWrapper_truncate(PyTextIOWrapperObject *self, PyObject *args)
static PyObject *
TextIOWrapper_repr(PyTextIOWrapperObject *self)
{
CHECK_INITIALIZED(self);
return PyUnicode_FromFormat("<TextIOWrapper encoding=%S>", self->encoding);
PyObject *nameobj, *res;
CHECK_INITIALIZED(self);
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);
}
else {
res = PyUnicode_FromFormat("<_io.TextIOWrapper name=%R encoding=%R>",
nameobj, self->encoding);
Py_DECREF(nameobj);
}
return res;
}