File tree 4 files changed +19
-7
lines changed
Misc/NEWS.d/next/Core and Builtins 4 files changed +19
-7
lines changed Original file line number Diff line number Diff line change
1
+ Remove the hardcoded classes names from ``_io `` module classes' ``__repr__ `` in
2
+ favor of the actual object type name to make it more subclass friendly.
Original file line number Diff line number Diff line change @@ -1074,22 +1074,26 @@ fileio_repr(fileio *self)
1074
1074
PyObject * nameobj , * res ;
1075
1075
1076
1076
if (self -> fd < 0 )
1077
- return PyUnicode_FromFormat ("<_io.FileIO [closed]>" );
1077
+ return PyUnicode_FromFormat (
1078
+ "<%s [closed]>" ,
1079
+ Py_TYPE ((PyObject * ) self )-> tp_name );
1078
1080
1079
1081
if (_PyObject_LookupAttrId ((PyObject * ) self , & PyId_name , & nameobj ) < 0 ) {
1080
1082
return NULL ;
1081
1083
}
1082
1084
if (nameobj == NULL ) {
1083
1085
res = PyUnicode_FromFormat (
1084
- "<_io.FileIO fd=%d mode='%s' closefd=%s>" ,
1086
+ "<%s fd=%d mode='%s' closefd=%s>" ,
1087
+ Py_TYPE ((PyObject * ) self )-> tp_name ,
1085
1088
self -> fd , mode_string (self ), self -> closefd ? "True" : "False" );
1086
1089
}
1087
1090
else {
1088
1091
int status = Py_ReprEnter ((PyObject * )self );
1089
1092
res = NULL ;
1090
1093
if (status == 0 ) {
1091
1094
res = PyUnicode_FromFormat (
1092
- "<_io.FileIO name=%R mode='%s' closefd=%s>" ,
1095
+ "<%s name=%R mode='%s' closefd=%s>" ,
1096
+ Py_TYPE ((PyObject * ) self )-> tp_name ,
1093
1097
nameobj , mode_string (self ), self -> closefd ? "True" : "False" );
1094
1098
Py_ReprLeave ((PyObject * )self );
1095
1099
}
Original file line number Diff line number Diff line change @@ -2886,7 +2886,7 @@ textiowrapper_repr(textio *self)
2886
2886
2887
2887
CHECK_INITIALIZED (self );
2888
2888
2889
- res = PyUnicode_FromString ("<_io.TextIOWrapper" );
2889
+ res = PyUnicode_FromFormat ("<%s" , Py_TYPE (( PyObject * ) self ) -> tp_name );
2890
2890
if (res == NULL )
2891
2891
return NULL ;
2892
2892
Original file line number Diff line number Diff line change @@ -1034,13 +1034,19 @@ static PyObject *
1034
1034
winconsoleio_repr (winconsoleio * self )
1035
1035
{
1036
1036
if (self -> handle == INVALID_HANDLE_VALUE )
1037
- return PyUnicode_FromFormat ("<_io._WindowsConsoleIO [closed]>" );
1037
+ return PyUnicode_FromFormat (
1038
+ "<%s [closed]>" ,
1039
+ Py_TYPE ((PyObject * ) self )-> tp_name );
1038
1040
1039
1041
if (self -> readable )
1040
- return PyUnicode_FromFormat ("<_io._WindowsConsoleIO mode='rb' closefd=%s>" ,
1042
+ return PyUnicode_FromFormat (
1043
+ "<%s mode='rb' closefd=%s>" ,
1044
+ Py_TYPE ((PyObject * ) self )-> tp_name ,
1041
1045
self -> closehandle ? "True" : "False" );
1042
1046
if (self -> writable )
1043
- return PyUnicode_FromFormat ("<_io._WindowsConsoleIO mode='wb' closefd=%s>" ,
1047
+ return PyUnicode_FromFormat (
1048
+ "<%s mode='wb' closefd=%s>" ,
1049
+ Py_TYPE ((PyObject * ) self )-> tp_name ,
1044
1050
self -> closehandle ? "True" : "False" );
1045
1051
1046
1052
PyErr_SetString (PyExc_SystemError , "_WindowsConsoleIO has invalid mode" );
You can’t perform that action at this time.
0 commit comments