diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2009-05-23 19:04:03 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2009-05-23 19:04:03 (GMT) |
commit | 716c444edcac0f32c6d82d530db2e6495e3d2be9 (patch) | |
tree | f63a8b6c4839ed622022eb5f1a8fab988b1847bc /Modules/_io/bufferedio.c | |
parent | 744af4406406d8c96a5a368efbf6f377a7d79095 (diff) | |
download | cpython-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/bufferedio.c')
-rw-r--r-- | Modules/_io/bufferedio.c | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 2c65207..034fe51 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -1123,6 +1123,27 @@ Buffered_iternext(BufferedObject *self) return line; } +static PyObject * +Buffered_repr(BufferedObject *self) +{ + PyObject *nameobj, *res; + + nameobj = PyObject_GetAttrString((PyObject *) self, "name"); + if (nameobj == NULL) { + if (PyErr_ExceptionMatches(PyExc_AttributeError)) + PyErr_Clear(); + else + return NULL; + res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name); + } + else { + res = PyUnicode_FromFormat("<%s name=%R>", + Py_TYPE(self)->tp_name, nameobj); + Py_DECREF(nameobj); + } + return res; +} + /* * class BufferedReader */ @@ -1472,7 +1493,7 @@ PyTypeObject PyBufferedReader_Type = { 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare */ - 0, /*tp_repr*/ + (reprfunc)Buffered_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ @@ -1828,7 +1849,7 @@ PyTypeObject PyBufferedWriter_Type = { 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare */ - 0, /*tp_repr*/ + (reprfunc)Buffered_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ @@ -2219,7 +2240,7 @@ PyTypeObject PyBufferedRandom_Type = { 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare */ - 0, /*tp_repr*/ + (reprfunc)Buffered_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ |