summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorAN Long <aisk@users.noreply.github.com>2024-01-09 20:39:36 (GMT)
committerGitHub <noreply@github.com>2024-01-09 20:39:36 (GMT)
commit623b338adf2645b09c546e7a17f2648d3a900620 (patch)
tree2c449075d2ce13ac8d6affe944632d1ac94f2339 /Modules
parent1092cfb20179ac7dd6a2c3c6f8a57ecc1732c777 (diff)
downloadcpython-623b338adf2645b09c546e7a17f2648d3a900620.zip
cpython-623b338adf2645b09c546e7a17f2648d3a900620.tar.gz
cpython-623b338adf2645b09c546e7a17f2648d3a900620.tar.bz2
gh-66060: Use actual class name in _io type's __repr__ (#30824)
Use the object's actual class name in the following _io type's __repr__: - FileIO - TextIOWrapper - _WindowsConsoleIO
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_io/fileio.c17
-rw-r--r--Modules/_io/textio.c7
-rw-r--r--Modules/_io/winconsoleio.c25
3 files changed, 29 insertions, 20 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 8a73ea0..af4375c 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -1100,31 +1100,32 @@ static PyObject *
fileio_repr(fileio *self)
{
PyObject *nameobj, *res;
+ const char *type_name = Py_TYPE((PyObject *) self)->tp_name;
- if (self->fd < 0)
- return PyUnicode_FromFormat("<_io.FileIO [closed]>");
+ if (self->fd < 0) {
+ return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
+ }
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
return NULL;
}
if (nameobj == NULL) {
res = PyUnicode_FromFormat(
- "<_io.FileIO fd=%d mode='%s' closefd=%s>",
- self->fd, mode_string(self), self->closefd ? "True" : "False");
+ "<%.100s fd=%d mode='%s' closefd=%s>",
+ type_name, self->fd, mode_string(self), self->closefd ? "True" : "False");
}
else {
int status = Py_ReprEnter((PyObject *)self);
res = NULL;
if (status == 0) {
res = PyUnicode_FromFormat(
- "<_io.FileIO name=%R mode='%s' closefd=%s>",
- nameobj, mode_string(self), self->closefd ? "True" : "False");
+ "<%.100s name=%R mode='%s' closefd=%s>",
+ type_name, nameobj, mode_string(self), self->closefd ? "True" : "False");
Py_ReprLeave((PyObject *)self);
}
else if (status > 0) {
PyErr_Format(PyExc_RuntimeError,
- "reentrant call inside %s.__repr__",
- Py_TYPE(self)->tp_name);
+ "reentrant call inside %.100s.__repr__", type_name);
}
Py_DECREF(nameobj);
}
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index e93c3e0..d794af8 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2948,10 +2948,11 @@ textiowrapper_repr(textio *self)
{
PyObject *nameobj, *modeobj, *res, *s;
int status;
+ const char *type_name = Py_TYPE(self)->tp_name;
CHECK_INITIALIZED(self);
- res = PyUnicode_FromString("<_io.TextIOWrapper");
+ res = PyUnicode_FromFormat("<%.100s", type_name);
if (res == NULL)
return NULL;
@@ -2959,8 +2960,8 @@ textiowrapper_repr(textio *self)
if (status != 0) {
if (status > 0) {
PyErr_Format(PyExc_RuntimeError,
- "reentrant call inside %s.__repr__",
- Py_TYPE(self)->tp_name);
+ "reentrant call inside %.100s.__repr__",
+ type_name);
}
goto error;
}
diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c
index 6680488..fecb338 100644
--- a/Modules/_io/winconsoleio.c
+++ b/Modules/_io/winconsoleio.c
@@ -1070,15 +1070,22 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
static PyObject *
winconsoleio_repr(winconsoleio *self)
{
- if (self->fd == -1)
- return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>");
-
- if (self->readable)
- return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>",
- self->closefd ? "True" : "False");
- if (self->writable)
- return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>",
- self->closefd ? "True" : "False");
+ const char *type_name = (Py_TYPE((PyObject *)self)->tp_name);
+
+ if (self->fd == -1) {
+ return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
+ }
+
+ if (self->readable) {
+ return PyUnicode_FromFormat("<%.100s mode='rb' closefd=%s>",
+ type_name,
+ self->closefd ? "True" : "False");
+ }
+ if (self->writable) {
+ return PyUnicode_FromFormat("<%.100s mode='wb' closefd=%s>",
+ type_name,
+ self->closefd ? "True" : "False");
+ }
PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode");
return NULL;