mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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
This commit is contained in:
parent
1092cfb201
commit
623b338adf
7 changed files with 58 additions and 19 deletions
|
@ -174,6 +174,16 @@ class AutoFileTests:
|
||||||
self.assertEqual(repr(self.f),
|
self.assertEqual(repr(self.f),
|
||||||
"<%s.FileIO [closed]>" % (self.modulename,))
|
"<%s.FileIO [closed]>" % (self.modulename,))
|
||||||
|
|
||||||
|
def test_subclass_repr(self):
|
||||||
|
class TestSubclass(self.FileIO):
|
||||||
|
pass
|
||||||
|
|
||||||
|
f = TestSubclass(TESTFN)
|
||||||
|
with f:
|
||||||
|
self.assertIn(TestSubclass.__name__, repr(f))
|
||||||
|
|
||||||
|
self.assertIn(TestSubclass.__name__, repr(f))
|
||||||
|
|
||||||
def testReprNoCloseFD(self):
|
def testReprNoCloseFD(self):
|
||||||
fd = os.open(TESTFN, os.O_RDONLY)
|
fd = os.open(TESTFN, os.O_RDONLY)
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -2806,6 +2806,13 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
repr(t) # Should not crash
|
repr(t) # Should not crash
|
||||||
|
|
||||||
|
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):
|
def test_line_buffering(self):
|
||||||
r = self.BytesIO()
|
r = self.BytesIO()
|
||||||
b = self.BufferedWriter(r, 1000)
|
b = self.BufferedWriter(r, 1000)
|
||||||
|
|
|
@ -98,6 +98,16 @@ class WindowsConsoleIOTests(unittest.TestCase):
|
||||||
self.assertIsInstance(f, ConIO)
|
self.assertIsInstance(f, ConIO)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
def test_subclass_repr(self):
|
||||||
|
class TestSubclass(ConIO):
|
||||||
|
pass
|
||||||
|
|
||||||
|
f = TestSubclass("CON")
|
||||||
|
with f:
|
||||||
|
self.assertIn(TestSubclass.__name__, repr(f))
|
||||||
|
|
||||||
|
self.assertIn(TestSubclass.__name__, repr(f))
|
||||||
|
|
||||||
@unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1),
|
@unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1),
|
||||||
"test does not work on Windows 7 and earlier")
|
"test does not work on Windows 7 and earlier")
|
||||||
def test_conin_conout_names(self):
|
def test_conin_conout_names(self):
|
||||||
|
|
|
@ -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.
|
|
@ -1100,31 +1100,32 @@ static PyObject *
|
||||||
fileio_repr(fileio *self)
|
fileio_repr(fileio *self)
|
||||||
{
|
{
|
||||||
PyObject *nameobj, *res;
|
PyObject *nameobj, *res;
|
||||||
|
const char *type_name = Py_TYPE((PyObject *) self)->tp_name;
|
||||||
|
|
||||||
if (self->fd < 0)
|
if (self->fd < 0) {
|
||||||
return PyUnicode_FromFormat("<_io.FileIO [closed]>");
|
return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
|
||||||
|
}
|
||||||
|
|
||||||
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
|
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (nameobj == NULL) {
|
if (nameobj == NULL) {
|
||||||
res = PyUnicode_FromFormat(
|
res = PyUnicode_FromFormat(
|
||||||
"<_io.FileIO fd=%d mode='%s' closefd=%s>",
|
"<%.100s fd=%d mode='%s' closefd=%s>",
|
||||||
self->fd, mode_string(self), self->closefd ? "True" : "False");
|
type_name, self->fd, mode_string(self), self->closefd ? "True" : "False");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int status = Py_ReprEnter((PyObject *)self);
|
int status = Py_ReprEnter((PyObject *)self);
|
||||||
res = NULL;
|
res = NULL;
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
res = PyUnicode_FromFormat(
|
res = PyUnicode_FromFormat(
|
||||||
"<_io.FileIO name=%R mode='%s' closefd=%s>",
|
"<%.100s name=%R mode='%s' closefd=%s>",
|
||||||
nameobj, mode_string(self), self->closefd ? "True" : "False");
|
type_name, nameobj, mode_string(self), self->closefd ? "True" : "False");
|
||||||
Py_ReprLeave((PyObject *)self);
|
Py_ReprLeave((PyObject *)self);
|
||||||
}
|
}
|
||||||
else if (status > 0) {
|
else if (status > 0) {
|
||||||
PyErr_Format(PyExc_RuntimeError,
|
PyErr_Format(PyExc_RuntimeError,
|
||||||
"reentrant call inside %s.__repr__",
|
"reentrant call inside %.100s.__repr__", type_name);
|
||||||
Py_TYPE(self)->tp_name);
|
|
||||||
}
|
}
|
||||||
Py_DECREF(nameobj);
|
Py_DECREF(nameobj);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2948,10 +2948,11 @@ textiowrapper_repr(textio *self)
|
||||||
{
|
{
|
||||||
PyObject *nameobj, *modeobj, *res, *s;
|
PyObject *nameobj, *modeobj, *res, *s;
|
||||||
int status;
|
int status;
|
||||||
|
const char *type_name = Py_TYPE(self)->tp_name;
|
||||||
|
|
||||||
CHECK_INITIALIZED(self);
|
CHECK_INITIALIZED(self);
|
||||||
|
|
||||||
res = PyUnicode_FromString("<_io.TextIOWrapper");
|
res = PyUnicode_FromFormat("<%.100s", type_name);
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -2959,8 +2960,8 @@ textiowrapper_repr(textio *self)
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
if (status > 0) {
|
if (status > 0) {
|
||||||
PyErr_Format(PyExc_RuntimeError,
|
PyErr_Format(PyExc_RuntimeError,
|
||||||
"reentrant call inside %s.__repr__",
|
"reentrant call inside %.100s.__repr__",
|
||||||
Py_TYPE(self)->tp_name);
|
type_name);
|
||||||
}
|
}
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1070,15 +1070,22 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
winconsoleio_repr(winconsoleio *self)
|
winconsoleio_repr(winconsoleio *self)
|
||||||
{
|
{
|
||||||
if (self->fd == -1)
|
const char *type_name = (Py_TYPE((PyObject *)self)->tp_name);
|
||||||
return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>");
|
|
||||||
|
|
||||||
if (self->readable)
|
if (self->fd == -1) {
|
||||||
return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>",
|
return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->readable) {
|
||||||
|
return PyUnicode_FromFormat("<%.100s mode='rb' closefd=%s>",
|
||||||
|
type_name,
|
||||||
self->closefd ? "True" : "False");
|
self->closefd ? "True" : "False");
|
||||||
if (self->writable)
|
}
|
||||||
return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>",
|
if (self->writable) {
|
||||||
|
return PyUnicode_FromFormat("<%.100s mode='wb' closefd=%s>",
|
||||||
|
type_name,
|
||||||
self->closefd ? "True" : "False");
|
self->closefd ? "True" : "False");
|
||||||
|
}
|
||||||
|
|
||||||
PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode");
|
PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue