8000 bpo-21861: Improve _io classes __repr__ · python/cpython@47ed9a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47ed9a3

Browse files
author
Michal Kuffa
committed
bpo-21861: Improve _io classes __repr__
Instead of hardcoding the class name in the __repr__ string, resolve the actuall one which makes the __repr__ more subclass friendly.
1 parent 40d2226 commit 47ed9a3

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove the hardcoded classes names from ``_io`` module classes' ``__repr__`` in
2+
favor of the actual object type name to make it more subclass friendly.

Modules/_io/fileio.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,22 +1074,26 @@ fileio_repr(fileio *self)
10741074
PyObject *nameobj, *res;
10751075

10761076
if (self->fd < 0)
1077-
return PyUnicode_FromFormat("<_io.FileIO [closed]>");
1077+
return PyUnicode_FromFormat(
1078+
"<%s [closed]>",
1079+
Py_TYPE((PyObject *) self)->tp_name);
10781080

10791081
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
10801082
return NULL;
10811083
}
10821084
if (nameobj == NULL) {
10831085
res = PyUnicode_FromFormat(
1084-
"<_io.FileIO fd=%d mode='%s' closefd=%s>",
1086+
"<%s fd=%d mode='%s' closefd=%s>",
1087+
Py_TYPE((PyObject *) self)->tp_name,
10851088
self->fd, mode_string(self), self->closefd ? "True" : "False");
10861089
}
10871090
else {
10881091
int status = Py_ReprEnter((PyObject *)self);
10891092
res = NULL;
10901093
if (status == 0) {
10911094
res = PyUnicode_FromFormat(
1092-
"<_io.FileIO name=%R mode='%s' closefd=%s>",
1095+
"<%s name=%R mode='%s' closefd=%s>",
1096+
Py_TYPE((PyObject *) self)->tp_name,
10931097
nameobj, mode_string(self), self->closefd ? "True" : "False");
10941098
Py_ReprLeave((PyObject *)self);
10951099
}

Modules/_io/textio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,7 @@ textiowrapper_repr(textio *self)
28862886

28872887
CHECK_INITIALIZED(self);
28882888

2889-
res = PyUnicode_FromString("<_io.TextIOWrapper");
2889+
res = PyUnicode_FromFormat("<%s", Py_TYPE((PyObject *) self)->tp_name);
28902890
if (res == NULL)
28912891
return NULL;
28922892

Modules/_io/winconsoleio.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,13 +1034,19 @@ static PyObject *
10341034
winconsoleio_repr(winconsoleio *self)
10351035
{
10361036
if (self->handle == INVALID_HANDLE_VALUE)
1037-
return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>");
1037+
return PyUnicode_FromFormat(
1038+
"<%s [closed]>",
1039+
Py_TYPE((PyObject *) self)->tp_name);
10381040

10391041
if (self->readable)
1040-
return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>",
1042+
return PyUnicode_FromFormat(
1043+
"<%s mode='rb' closefd=%s>",
1044+
Py_TYPE((PyObject *) self)->tp_name,
10411045
self->closehandle ? "True" : "False");
10421046
if (self->writable)
1043-
return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>",
1047+
return PyUnicode_FromFormat(
1048+
"<%s mode='wb' closefd=%s>",
1049+
Py_TYPE((PyObject *) self)->tp_name,
10441050
self->closehandle ? "True" : "False");
10451051

10461052
PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode");

0 commit comments

Comments
 (0)
0