File tree 6 files changed +64
-7
lines changed
Misc/NEWS.d/next/Core and Builtins
6 files changed +64
-7
lines changed Original file line number Diff line number Diff line change @@ -154,6 +154,27 @@ def test_none_args(self):
154
154
def test_reject (self ):
155
155
self .assertRaises (TypeError , self .f .write , "Hello!" )
156
156
157
+ def test_subclass_repr (self ):
158
+ class CustomFileIO (self .FileIO ):
159
+ pass
160
+
161
+ custom_file_io = CustomFileIO (TESTFN , 'w' )
162
+
163
+ self .assertIn (
164
+ "CustomFileIO name=%r mode=%r closefd=True>" %
165
+ (custom_file_io .name , custom_file_io .mode ),
166
+ repr (custom_file_io ))
167
+
168
+ del custom_file_io .name
169
+ self .assertIn (
170
+ "CustomFileIO fd=%r mode=%r closefd=True>" %
171
+ (custom_file_io .fileno (), custom_file_io .mode ),
172
+ repr (custom_file_io ))
173
+
174
+ custom_file_io .close ()
175
+ self .assertIn ("CustomFileIO [closed]" , repr (custom_file_io ))
176
+
177
+
157
178
def testRepr (self ):
158
179
self .assertEqual (repr (self .f ),
159
180
"<%s.FileIO name=%r mode=%r closefd=True>" %
Original file line number Diff line number Diff line change @@ -2630,6 +2630,30 @@ def test_repr(self):
2630
2630
t .buffer .detach ()
2631
2631
repr (t ) # Should not raise an exception
2632
2632
2633
+ def test_subclass_repr (self ):
2634
+ class SubTextIOWrapper (self .TextIOWrapper ):
2635
+ pass
2636
+
2637
+ clsname = 'SubTextIOWrapper'
2638
+ raw = self .BytesIO ("hello" .encode ("utf-8" ))
2639
+ b = self .BufferedReader (raw )
2640
+ t = SubTextIOWrapper (b , encoding = "utf-8" )
2641
+ self .assertRegex (repr (t ),
2642
+ r"%s encoding='utf-8'>" % clsname )
2643
+ raw .name = "dummy"
2644
+ self .assertRegex (repr (t ),
2645
+ r"%s name='dummy' encoding='utf-8'>" % clsname )
2646
+ t .mode = "r"
2647
+ self .assertRegex (repr (t ),
2648
+ r"%s name='dummy' mode='r' encoding='utf-8'>" %
2649
+ clsname )
2650
+ raw .name = b"dummy"
2651
+ self .assertRegex (
2652
+ repr (t ),
2653
+ r"%s name=b'dummy' mode='r' encoding='utf-8'>" % clsname )
2654
+ t .buffer .detach ()
2655
+ repr (t ) # Should not raise an exception
2656
+
2633
2657
def test_recursive_repr (self ):
2634
2658
# Issue #25455
2635
2659
raw = self .BytesIO ()
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 @@ -1080,22 +1080,26 @@ fileio_repr(fileio *self)
1080
1080
PyObject * nameobj , * res ;
1081
1081
1082
1082
if (self -> fd < 0 )
1083
- return PyUnicode_FromFormat ("<_io.FileIO [closed]>" );
1083
+ return PyUnicode_FromFormat (
1084
+ "<%s [closed]>" ,
1085
+ Py_TYPE ((PyObject * ) self )-> tp_name );
1084
1086
1085
1087
if (_PyObject_LookupAttrId ((PyObject * ) self , & PyId_name , & nameobj ) < 0 ) {
1086
1088
return NULL ;
1087
1089
}
1088
1090
if (nameobj == NULL ) {
1089
1091
res = PyUnicode_FromFormat (
1090
- "<_io.FileIO fd=%d mode='%s' closefd=%s>" ,
1092
+ "<%s fd=%d mode='%s' closefd=%s>" ,
1093
+ Py_TYPE ((PyObject * ) self )-> tp_name ,
1091
1094
self -> fd , mode_string (self ), self -> closefd ? "True" : "False" );
1092
1095
}
1093
1096
else {
1094
1097
int status = Py_ReprEnter ((PyObject * )self );
1095
1098
res = NULL ;
1096
1099
if (status == 0 ) {
1097
1100
res = PyUnicode_FromFormat (
1098
- "<_io.FileIO name=%R mode='%s' closefd=%s>" ,
1101
+ "<%s name=%R mode='%s' closefd=%s>" ,
1102
+ Py_TYPE ((PyObject * ) self )-> tp_name ,
1099
1103
nameobj , mode_string (self ), self -> closefd ? "True" : "False" );
1100
1104
Py_ReprLeave ((PyObject * )self );
1101
1105
}
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