summaryrefslogtreecommitdiffstats
path: root/Modules/_io/fileio.c
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2009-05-23 19:04:03 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2009-05-23 19:04:03 (GMT)
commit716c444edcac0f32c6d82d530db2e6495e3d2be9 (patch)
treef63a8b6c4839ed622022eb5f1a8fab988b1847bc /Modules/_io/fileio.c
parent744af4406406d8c96a5a368efbf6f377a7d79095 (diff)
downloadcpython-716c444edcac0f32c6d82d530db2e6495e3d2be9.zip
cpython-716c444edcac0f32c6d82d530db2e6495e3d2be9.tar.gz
cpython-716c444edcac0f32c6d82d530db2e6495e3d2be9.tar.bz2
Issue #5761: Add the name of the underlying file to the repr() of various IO objects.
Diffstat (limited to 'Modules/_io/fileio.c')
-rw-r--r--Modules/_io/fileio.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 9400c91..d063fbf 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -846,11 +846,26 @@ mode_string(PyFileIOObject *self)
static PyObject *
fileio_repr(PyFileIOObject *self)
{
+ PyObject *nameobj, *res;
+
if (self->fd < 0)
- return PyUnicode_FromFormat("io.FileIO(-1)");
+ return PyUnicode_FromFormat("<_io.FileIO [closed]>");
- return PyUnicode_FromFormat("io.FileIO(%d, '%s')",
- self->fd, mode_string(self));
+ nameobj = PyObject_GetAttrString((PyObject *) self, "name");
+ if (nameobj == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_AttributeError))
+ PyErr_Clear();
+ else
+ return NULL;
+ res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
+ self->fd, mode_string(self));
+ }
+ else {
+ res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
+ nameobj, mode_string(self));
+ Py_DECREF(nameobj);
+ }
+ return res;
}
static PyObject *