Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4815ca

Browse files
committed
Issue #10872: The repr() of TextIOWrapper objects now includes the mode
if available. (at Georg's request)
1 parent a6c91f5 commit a4815ca

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

Lib/_pyio.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,13 +1504,20 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
15041504
# - "chars_..." for integer variables that count decoded characters
15051505

15061506
def __repr__(self):
1507+
result = "<_pyio.TextIOWrapper"
15071508
try:
15081509
name = self.name
15091510
except AttributeError:
1510-
return "<_pyio.TextIOWrapper encoding={0!r}>".format(self.encoding)
1511+
pass
1512+
else:
1513+
result += " name={0!r}".format(name)
1514+
try:
1515+
mode = self.mode
1516+
except AttributeError:
1517+
pass
15111518
else:
1512-
return "<_pyio.TextIOWrapper name={0!r} encoding={1!r}>".format(
1513-
name, self.encoding)
1519+
result += " mode={0!r}".format(mode)
1520+
return result + " encoding={0!r}>".format(self.encoding)
15141521

15151522
@property
15161523
def encoding(self):

Lib/test/test_io.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,12 @@ def test_repr(self):
17171717
raw.name = "dummy"
17181718
self.assertEqual(repr(t),
17191719
"<%s.TextIOWrapper name='dummy' encoding='utf-8'>" % modname)
1720+
t.mode = "r"
1721+
self.assertEqual(repr(t),
1722+
"<%s.TextIOWrapper name='dummy' mode='r' encoding='utf-8'>" % modname)
17201723
raw.name = b"dummy"
17211724
self.assertEqual(repr(t),
1722-
"<%s.TextIOWrapper name=b'dummy' encoding='utf-8'>" % modname)
1725+
"<%s.TextIOWrapper name=b'dummy' mode='r' encoding='utf-8'>" % modname)
17231726

17241727
def test_line_buffering(self):
17251728
r = self.BytesIO()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Core and Builtins
4040
Library
4141
-------
4242

43+
- Issue #10872: The repr() of TextIOWrapper objects now includes the mode
44+
if available.
45+
4346
- Issue #10869: Fixed bug where ast.increment_lineno modified the root
4447
node twice.
4548

Modules/_io/textio.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,25 +2323,52 @@ textiowrapper_truncate(textio *self, PyObject *args)
23232323
static PyObject *
23242324
textiowrapper_repr(textio *self)
23252325
{
2326-
PyObject *nameobj, *res;
2326+
PyObject *nameobj, *modeobj, *res, *s;
23272327

23282328
CHECK_INITIALIZED(self);
23292329

2330+
res = PyUnicode_FromString("<_io.TextIOWrapper");
2331+
if (res == NULL)
2332+
return NULL;
23302333
nameobj = PyObject_GetAttrString((PyObject *) self, "name");
23312334
if (nameobj == NULL) {
23322335
if (PyErr_ExceptionMatches(PyExc_AttributeError))
23332336
PyErr_Clear();
23342337
else
2335-
return NULL;
2336-
res = PyUnicode_FromFormat("<_io.TextIOWrapper encoding=%R>",
2337-
self->encoding);
2338+
goto error;
23382339
}
23392340
else {
2340-
res = PyUnicode_FromFormat("<_io.TextIOWrapper name=%R encoding=%R>",
2341-
nameobj, self->encoding);
2341+
s = PyUnicode_FromFormat(" name=%R", nameobj);
23422342
Py_DECREF(nameobj);
2343+
if (s == NULL)
2344+
goto error;
2345+
PyUnicode_AppendAndDel(&res, s);
2346+
if (res == NULL)
2347+
return NULL;
23432348
}
2344-
return res;
2349+
modeobj = PyObject_GetAttrString((PyObject *) self, "mode");
2350+
if (modeobj == NULL) {
2351+
if (PyErr_ExceptionMatches(PyExc_AttributeError))
2352+
PyErr_Clear();
2353+
else
2354+
goto error;
2355+
}
2356+
else {
2357+
s = PyUnicode_FromFormat(" mode=%R", modeobj);
2358+
Py_DECREF(modeobj);
2359+
if (s == NULL)
2360+
goto error;
2361+
PyUnicode_AppendAndDel(&res, s);
2362+
if (res == NULL)
2363+
return NULL;
2364+
}
2365+
s = PyUnicode_FromFormat("%U encoding=%R>",
2366+
res, self->encoding);
2367+
Py_DECREF(res);
2368+
return s;
2369+
error:
2370+
Py_XDECREF(res);
2371+
return NULL;
23452372
}
23462373

23472374

0 commit comments

Comments
 (0)