Issue #5761: Add the name of the underlying file to the repr() of various IO objects.

This commit is contained in:
Antoine Pitrou 2009-05-23 19:04:03 +00:00
parent 744af44064
commit 716c444edc
7 changed files with 107 additions and 14 deletions

View file

@ -736,6 +736,15 @@ class _BufferedIOMixin(BufferedIOBase):
def mode(self):
return self.raw.mode
def __repr__(self):
clsname = self.__class__.__name__
try:
name = self.name
except AttributeError:
return "<_pyio.{0}>".format(clsname)
else:
return "<_pyio.{0} name={1!r}>".format(clsname, name)
### Lower-level APIs ###
def fileno(self):
@ -1455,7 +1464,13 @@ class TextIOWrapper(TextIOBase):
# - "chars_..." for integer variables that count decoded characters
def __repr__(self):
return "<TextIOWrapper encoding={0}>".format(self.encoding)
try:
name = self.name
except AttributeError:
return "<_pyio.TextIOWrapper encoding={0!r}>".format(self.encoding)
else:
return "<_pyio.TextIOWrapper name={0!r} encoding={1!r}>".format(
name, self.encoding)
@property
def encoding(self):