You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2014-06-24.17:36:58.710>labels= ['extension-modules', 'easy', 'type-bug', 'expert-IO']
title='io class name are hardcoded in reprs'updated_at=<Date2022-01-23.10:01:52.325>user='https://github.com/serhiy-storchaka'
>>> import io
>>> classF(io.FileIO): pass
...
>>> f = F('/dev/null', 'r')
>>> f
<_io.FileIO name='/dev/null' mode='rb'>
>>> classB(io.BufferedReader): pass
...
>>> b = B(f)
>>> b
<B name='/dev/null'>
>>> classT(io.TextIOBufferedReader): pass
io.TextIOBase( io.TextIOWrapper(
>>> classT(io.TextIOWrapper): pass
...
>>> t = T(b)
>>> t
<_io.TextIOWrapper name='/dev/null' encoding='UTF-8'>
Expected results are "<main.F name='/dev/null' mode='rb'>", "<main.B name='/dev/null'>" and "<main.T name='/dev/null' encoding='UTF-8'>". Usually reprs of subclass instances substitute actual module and class names.
Is it common for C implementations to introspect to figure out their "real" name? I do this manually for reprs of my user defined classes, but I haven't noticed many built-ins that consider extensibility for the repr. Maybe I'm just not using the classes that do it or I'm overriding the repr without checking?
I just tested, and it looks like frozenset has a subclass friendly repr, while bytearray does not as of 3.4.0. Seems like it might make sense to first determine if introspection should be the default; it would mean a little code bloat every time it's done.
I now see that this issue is not so easy from C side.
And more, it is very uncommon for either Python or C implementations (especial C implementations) to introspect to figure out their "real" name. Existing cases are rather exceptions. And in many case this is only a side effect of sharing an implementation between several classes (set and frozenset, three buffered file classes in io, etc). In many cases repr's flexibility is limited: hardcoded or omitted module name, used class's __name__ instead of __qualname__, etc.
This should be discussed on Python-Dev maillist.
Windsooon commented on May 17, 2019
Windsooonmannequin
Mannequin
More actions
IIUC, in the c code we just hardcode the name "_io.FileIO" for "PyFileIO_Type" in https://github.com/python/cpython/blob/master/Modules/_io/fileio.c#L1180. If we want to get a dynamic name, we should replace the hardcode name with the module name (like "__main__"). I found this also happens on other modules like collections.
Activity
serhiy-storchaka commentedon Jun 24, 2014
Expected results are "<main.F name='/dev/null' mode='rb'>", "<main.B name='/dev/null'>" and "<main.T name='/dev/null' encoding='UTF-8'>". Usually reprs of subclass instances substitute actual module and class names.
PCManticore commentedon Jun 30, 2014
The same should be done for _pyio?
MojoVampire commentedon Jun 30, 2014
Is it common for C implementations to introspect to figure out their "real" name? I do this manually for reprs of my user defined classes, but I haven't noticed many built-ins that consider extensibility for the repr. Maybe I'm just not using the classes that do it or I'm overriding the repr without checking?
I just tested, and it looks like frozenset has a subclass friendly repr, while bytearray does not as of 3.4.0. Seems like it might make sense to first determine if introspection should be the default; it would mean a little code bloat every time it's done.
rhettinger commentedon Jul 1, 2014
This is a sensible idea that makes life easier for people writing subclasses.
+1 for your suggestion.
serhiy-storchaka commentedon Jul 13, 2014
I now see that this issue is not so easy from C side.
And more, it is very uncommon for either Python or C implementations (especial C implementations) to introspect to figure out their "real" name. Existing cases are rather exceptions. And in many case this is only a side effect of sharing an implementation between several classes (set and frozenset, three buffered file classes in io, etc). In many cases repr's flexibility is limited: hardcoded or omitted module name, used class's __name__ instead of __qualname__, etc.
This should be discussed on Python-Dev maillist.
Windsooon commentedon May 17, 2019
IIUC, in the c code we just hardcode the name "_io.FileIO" for "PyFileIO_Type" in https://github.com/python/cpython/blob/master/Modules/_io/fileio.c#L1180. If we want to get a dynamic name, we should replace the hardcode name with the module name (like "__main__"). I found this also happens on other modules like collections.
gh-66060: Use actual class name in _io type's __repr__ (#30824)
pythongh-66060: Use actual class name in _io type's __repr__ (python#…
pythongh-66060: Use actual class name in _io type's __repr__ (python#…
pythongh-66060: Use actual class name in _io type's __repr__ (python#…