mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #10872: The repr() of TextIOWrapper objects now includes the mode
if available. (at Georg's request)
This commit is contained in:
parent
a6c91f5e3b
commit
a4815caa7c
4 changed files with 51 additions and 11 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue