From 9a80e2be1467be04453e4bfb755b43ec6f947d2e Mon Sep 17 00:00:00 2001 From: An Long Date: Sun, 23 Jan 2022 14:01:28 +0800 Subject: [PATCH 1/4] bpo-21861: fix __repr__ for io subclasses --- Lib/test/test_fileio.py | 10 ++++++++++ Lib/test/test_io.py | 7 +++++++ Lib/test/test_winconsoleio.py | 9 +++++++++ .../2022-01-23-18-00-10.bpo-21861.N8E1zw.rst | 3 +++ Modules/_io/fileio.c | 17 +++++++++-------- Modules/_io/textio.c | 5 +++-- Modules/_io/winconsoleio.c | 12 +++++++----- 7 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-01-23-18-00-10.bpo-21861.N8E1zw.rst diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index e4984d3cd559e3..99c035076a2e3d 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -168,6 +168,16 @@ def testRepr(self): self.assertEqual(repr(self.f), "<%s.FileIO [closed]>" % (self.modulename,)) + def test_subclass_repr(self): + class TestSubclass(self.FileIO): + pass + + f = TestSubclass(TESTFN) + self.assertIn(TestSubclass.__name__, repr(f)) + + f.close() + self.assertIn(TestSubclass.__name__, repr(f)) + def testReprNoCloseFD(self): fd = os.open(TESTFN, os.O_RDONLY) try: diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 5528c461e58ae6..6222976ee28720 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2677,6 +2677,13 @@ def test_recursive_repr(self): except RuntimeError: pass + def test_subclass_repr(self): + class TestSubclass(self.TextIOWrapper): + pass + + f = TestSubclass(self.StringIO()) + self.assertIn(TestSubclass.__name__, repr(f)) + def test_line_buffering(self): r = self.BytesIO() b = self.BufferedWriter(r, 1000) diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py index 70a85552cc03b0..b43e29f52aaa20 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -98,6 +98,15 @@ def test_open_name(self): self.assertIsInstance(f, ConIO) f.close() + def test_subclass_repr(self): + class TestSubclass(ConIO): + pass + + f = TestSubclass("CON") + self.assertIn(TestSubclass.__name__, repr(f)) + f.close() + self.assertIn(TestSubclass.__name__, repr(f)) + @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1), "test does not work on Windows 7 and earlier") def test_conin_conout_names(self): diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-01-23-18-00-10.bpo-21861.N8E1zw.rst b/Misc/NEWS.d/next/Core and Builtins/2022-01-23-18-00-10.bpo-21861.N8E1zw.rst new file mode 100644 index 00000000000000..5d99845912caf3 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-01-23-18-00-10.bpo-21861.N8E1zw.rst @@ -0,0 +1,3 @@ +Use the object's actual class name in :meth:`_io.FileIO.__repr__`, +:meth:`_io._WindowsConsoleIO` and :meth:`_io.TextIOWrapper.__repr__`, to +make these methods subclass friendly. diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 8b1cff56d75fa3..eb558402f300f7 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -1078,31 +1078,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("<%s [closed]>", type_name); + } if (_PyObject_LookupAttr((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"); + "<%s 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"); + "<%s 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 %s.__repr__", type_name); } Py_DECREF(nameobj); } diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 3cbaca3ef46069..bcdc4794fb3987 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -2854,10 +2854,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("<%s", type_name); if (res == NULL) return NULL; @@ -2866,7 +2867,7 @@ textiowrapper_repr(textio *self) if (status > 0) { PyErr_Format(PyExc_RuntimeError, "reentrant call inside %s.__repr__", - Py_TYPE(self)->tp_name); + type_name); } goto error; } diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index 5c1a6dd86fc54f..d1423c1990aa32 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -1028,15 +1028,17 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b) static PyObject * winconsoleio_repr(winconsoleio *self) { + const char *type_name = (Py_TYPE((PyObject *)self)->tp_name); + if (self->fd == -1) - return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>"); + return PyUnicode_FromFormat("<%s [closed]>", type_name); if (self->readable) - return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>", - self->closefd ? "True" : "False"); + return PyUnicode_FromFormat("<%s mode='rb' closefd=%s>", + type_name, self->closefd ? "True" : "False"); if (self->writable) - return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>", - self->closefd ? "True" : "False"); + return PyUnicode_FromFormat("<%s mode='wb' closefd=%s>", + type_name, self->closefd ? "True" : "False"); PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode"); return NULL; From a27ec5c9f8b33a55325fce50d3cc513ec777451f Mon Sep 17 00:00:00 2001 From: AN Long Date: Wed, 10 Jan 2024 00:07:01 +0800 Subject: [PATCH 2/4] using %.100s as class name format --- Modules/_io/fileio.c | 8 ++++---- Modules/_io/textio.c | 4 ++-- Modules/_io/winconsoleio.c | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 7c0347da6d89fe..af4375c3640679 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -1103,7 +1103,7 @@ fileio_repr(fileio *self) const char *type_name = Py_TYPE((PyObject *) self)->tp_name; if (self->fd < 0) { - return PyUnicode_FromFormat("<%s [closed]>", type_name); + return PyUnicode_FromFormat("<%.100s [closed]>", type_name); } if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) { @@ -1111,7 +1111,7 @@ fileio_repr(fileio *self) } if (nameobj == NULL) { res = PyUnicode_FromFormat( - "<%s fd=%d mode='%s' closefd=%s>", + "<%.100s fd=%d mode='%s' closefd=%s>", type_name, self->fd, mode_string(self), self->closefd ? "True" : "False"); } else { @@ -1119,13 +1119,13 @@ fileio_repr(fileio *self) res = NULL; if (status == 0) { res = PyUnicode_FromFormat( - "<%s name=%R mode='%s' closefd=%s>", + "<%.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__", type_name); + "reentrant call inside %.100s.__repr__", type_name); } Py_DECREF(nameobj); } diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index c5863548050771..855caf1629312f 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -2948,7 +2948,7 @@ textiowrapper_repr(textio *self) CHECK_INITIALIZED(self); - res = PyUnicode_FromFormat("<%s", type_name); + res = PyUnicode_FromFormat("<%.100s", type_name); if (res == NULL) return NULL; @@ -2956,7 +2956,7 @@ textiowrapper_repr(textio *self) if (status != 0) { if (status > 0) { PyErr_Format(PyExc_RuntimeError, - "reentrant call inside %s.__repr__", + "reentrant call inside %.100s.__repr__", type_name); } goto error; diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index e50aa87674b023..ee47c5fe949b37 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -1073,13 +1073,13 @@ winconsoleio_repr(winconsoleio *self) const char *type_name = (Py_TYPE((PyObject *)self)->tp_name); if (self->fd == -1) - return PyUnicode_FromFormat("<%s [closed]>", type_name); + return PyUnicode_FromFormat("<%.100s [closed]>", type_name); if (self->readable) - return PyUnicode_FromFormat("<%s mode='rb' closefd=%s>", + return PyUnicode_FromFormat("<%.100s mode='rb' closefd=%s>", type_name, self->closefd ? "True" : "False"); if (self->writable) - return PyUnicode_FromFormat("<%s mode='wb' closefd=%s>", + return PyUnicode_FromFormat("<%.100s mode='wb' closefd=%s>", type_name, self->closefd ? "True" : "False"); PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode"); From 8268a9a0fb1b38c4726b48888ebd01a3820eeeaa Mon Sep 17 00:00:00 2001 From: AN Long Date: Wed, 10 Jan 2024 00:10:50 +0800 Subject: [PATCH 3/4] ensure test files got closed --- Lib/test/test_fileio.py | 4 ++-- Lib/test/test_winconsoleio.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 9727046441f68f..06d9b454add34c 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -179,9 +179,9 @@ class TestSubclass(self.FileIO): pass f = TestSubclass(TESTFN) - self.assertIn(TestSubclass.__name__, repr(f)) + with f: + self.assertIn(TestSubclass.__name__, repr(f)) - f.close() self.assertIn(TestSubclass.__name__, repr(f)) def testReprNoCloseFD(self): diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py index b43e29f52aaa20..72ff9606908ed5 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -103,8 +103,9 @@ class TestSubclass(ConIO): pass f = TestSubclass("CON") - self.assertIn(TestSubclass.__name__, repr(f)) - f.close() + with f: + self.assertIn(TestSubclass.__name__, repr(f)) + self.assertIn(TestSubclass.__name__, repr(f)) @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1), From 31e305bf331e02950c40302c113b746a9bc576e2 Mon Sep 17 00:00:00 2001 From: AN Long Date: Wed, 10 Jan 2024 01:25:19 +0800 Subject: [PATCH 4/4] follow the PEP7 to format `winconsoleio_repr` function --- Modules/_io/winconsoleio.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index ee47c5fe949b37..fecb3389570780 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -1072,15 +1072,20 @@ winconsoleio_repr(winconsoleio *self) { const char *type_name = (Py_TYPE((PyObject *)self)->tp_name); - if (self->fd == -1) + if (self->fd == -1) { return PyUnicode_FromFormat("<%.100s [closed]>", type_name); + } - if (self->readable) + if (self->readable) { return PyUnicode_FromFormat("<%.100s mode='rb' closefd=%s>", - type_name, self->closefd ? "True" : "False"); - if (self->writable) + type_name, + self->closefd ? "True" : "False"); + } + if (self->writable) { return PyUnicode_FromFormat("<%.100s mode='wb' closefd=%s>", - type_name, self->closefd ? "True" : "False"); + type_name, + self->closefd ? "True" : "False"); + } PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode"); return NULL;