Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 10 Lib/test/test_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,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)
with f:
self.assertIn(TestSubclass.__name__, repr(f))

self.assertIn(TestSubclass.__name__, repr(f))

def testReprNoCloseFD(self):
fd = os.open(TESTFN, os.O_RDONLY)
try:
Expand Down
7 changes: 7 additions & 0 deletions 7 Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2806,6 +2806,13 @@ def test_recursive_repr(self):
with self.assertRaises(RuntimeError):
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):
r = self.BytesIO()
b = self.BufferedWriter(r, 1000)
Expand Down
10 changes: 10 additions & 0 deletions 10 Lib/test/test_winconsoleio.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def test_open_name(self):
self.assertIsInstance(f, ConIO)
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),
"test does not work on Windows 7 and earlier")
def test_conin_conout_names(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 9 additions & 8 deletions 17 Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions 7 Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2944,19 +2944,20 @@ 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;

status = Py_ReprEnter((PyObject *)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;
}
Expand Down
25 changes: 16 additions & 9 deletions 25 Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
}

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;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.