diff options
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_io/bufferedio.c | 27 | ||||
-rw-r--r-- | Modules/_io/fileio.c | 21 | ||||
-rw-r--r-- | Modules/_io/textio.c | 21 |
3 files changed, 61 insertions, 8 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*/ 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 * diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index b78256e..c8d2833 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -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; } |