1010from functools import wraps
1111
1212from test .support import (
13- cpython_only , swap_attr , gc_collect , is_emscripten , is_wasi
13+ cpython_only , swap_attr , gc_collect , is_emscripten , is_wasi ,
14+ infinite_recursion ,
1415)
15- from test .support .os_helper import (TESTFN , TESTFN_UNICODE , make_bad_fd )
16+ from test .support .os_helper import (
17+ TESTFN , TESTFN_ASCII , TESTFN_UNICODE , make_bad_fd ,
18+ )
1619from test .support .warnings_helper import check_warnings
20+ from test .support .import_helper import import_module
1721from collections import UserList
1822
1923import _io # C implementation of io
@@ -171,6 +175,16 @@ def testRepr(self):
171175 self .assertEqual (repr (self .f ),
172176 "<%s.FileIO [closed]>" % (self .modulename ,))
173177
178+ def test_subclass_repr (self ):
179+ class TestSubclass (self .FileIO ):
180+ pass
181+
182+ f = TestSubclass (TESTFN )
183+ with f :
184+ self .assertIn (TestSubclass .__name__ , repr (f ))
185+
186+ self .assertIn (TestSubclass .__name__ , repr (f ))
187+
174188 def testReprNoCloseFD (self ):
175189 fd = os .open (TESTFN , os .O_RDONLY )
176190 try :
@@ -181,6 +195,7 @@ def testReprNoCloseFD(self):
181195 finally :
182196 os .close (fd )
183197
198+ @infinite_recursion (25 )
184199 def testRecursiveRepr (self ):
185200 # Issue #25455
186201 with swap_attr (self .f , 'name' , self .f ):
@@ -348,40 +363,34 @@ class CAutoFileTests(AutoFileTests, unittest.TestCase):
348363 FileIO = _io .FileIO
349364 modulename = '_io'
350365
351- # TODO: RUSTPYTHON
352- @unittest .expectedFailure
366+ @unittest .expectedFailure # TODO: RUSTPYTHON
353367 def testBlksize (self ):
354- super ().testBlksize ()
368+ return super ().testBlksize ()
355369
356- # TODO: RUSTPYTHON
357- if sys .platform == "win32" :
358- @unittest .expectedFailure
359- def testErrnoOnClosedTruncate (self ):
360- super ().testErrnoOnClosedTruncate ()
370+ @unittest .expectedFailureIfWindows ("TODO: RUSTPYTHON" )
371+ def testErrnoOnClosedTruncate (self ):
372+ return super ().testErrnoOnClosedTruncate ()
361373
362- # TODO: RUSTPYTHON
363- @unittest .expectedFailure
374+ @unittest .expectedFailure # TODO: RUSTPYTHON
364375 def testMethods (self ):
365- super ().testMethods ()
366-
367- # TODO: RUSTPYTHON
368- @unittest .expectedFailure
376+ return super ().testMethods ()
377+
378+ @unittest .expectedFailure # TODO: RUSTPYTHON
369379 def testOpenDirFD (self ):
370- super ().testOpenDirFD ()
380+ return super ().testOpenDirFD ()
371381
382+ @unittest .expectedFailure # TODO: RUSTPYTHON
383+ def test_subclass_repr (self ):
384+ return super ().test_subclass_repr ()
372385
373386@unittest .skipIf (sys .platform == "win32" , "TODO: RUSTPYTHON, test setUp errors on Windows" )
374387class PyAutoFileTests (AutoFileTests , unittest .TestCase ):
375388 FileIO = _pyio .FileIO
376389 modulename = '_pyio'
377390
378- def testOpendir (self ):
379- super ().testOpendir ()
380-
381391
382392class OtherFileTests :
383393
384- @unittest .skip ("TODO: non-deterministic failures, FileIO.seekable()?" )
385394 def testAbles (self ):
386395 try :
387396 f = self .FileIO (TESTFN , "w" )
@@ -458,18 +467,15 @@ def testUnicodeOpen(self):
458467
459468 def testBytesOpen (self ):
460469 # Opening a bytes filename
461- try :
462- fn = TESTFN .encode ("ascii" )
463- except UnicodeEncodeError :
464- self .skipTest ('could not encode %r to ascii' % TESTFN )
470+ fn = TESTFN_ASCII .encode ("ascii" )
465471 f = self .FileIO (fn , "w" )
466472 try :
467473 f .write (b"abc" )
468474 f .close ()
469- with open (TESTFN , "rb" ) as f :
475+ with self . open (TESTFN_ASCII , "rb" ) as f :
470476 self .assertEqual (f .read (), b"abc" )
471477 finally :
472- os .unlink (TESTFN )
478+ os .unlink (TESTFN_ASCII )
473479
474480 @unittest .skipIf (sys .getfilesystemencoding () != 'utf-8' ,
475481 "test only works for utf-8 filesystems" )
@@ -483,7 +489,7 @@ def testUtf8BytesOpen(self):
483489 try :
484490 f .write (b"abc" )
485491 f .close ()
486- with open (TESTFN_UNICODE , "rb" ) as f :
492+ with self . open (TESTFN_UNICODE , "rb" ) as f :
487493 self .assertEqual (f .read (), b"abc" )
488494 finally :
489495 os .unlink (TESTFN_UNICODE )
@@ -500,6 +506,15 @@ def testInvalidFd(self):
500506 import msvcrt
501507 self .assertRaises (OSError , msvcrt .get_osfhandle , make_bad_fd ())
502508
509+ @unittest .expectedFailure # TODO: RUSTPYTHON
510+ def testBooleanFd (self ):
511+ for fd in False , True :
512+ with self .assertWarnsRegex (RuntimeWarning ,
513+ 'bool is used as a file descriptor' ) as cm :
514+ f = self .FileIO (fd , closefd = False )
515+ f .close ()
516+ self .assertEqual (cm .filename , __file__ )
517+
503518 def testBadModeArgument (self ):
504519 # verify that we get a sensible error message for bad mode argument
505520 bad_mode = "qwerty"
@@ -530,7 +545,7 @@ def testTruncate(self):
530545
531546 def testTruncateOnWindows (self ):
532547 def bug801631 ():
533- # SF bug <http ://www .python.org/sf/801631 >
548+ # SF bug <https ://bugs .python.org/issue801631 >
534549 # "file.truncate fault on windows"
535550 f = self .FileIO (TESTFN , 'w' )
536551 f .write (bytes (range (11 )))
@@ -559,13 +574,13 @@ def bug801631():
559574
560575 def testAppend (self ):
561576 try :
562- f = open (TESTFN , 'wb' )
577+ f = self . FileIO (TESTFN , 'wb' )
563578 f .write (b'spam' )
564579 f .close ()
565- f = open (TESTFN , 'ab' )
580+ f = self . FileIO (TESTFN , 'ab' )
566581 f .write (b'eggs' )
567582 f .close ()
568- f = open (TESTFN , 'rb' )
583+ f = self . FileIO (TESTFN , 'rb' )
569584 d = f .read ()
570585 f .close ()
571586 self .assertEqual (d , b'spameggs' )
@@ -601,16 +616,12 @@ def __setattr__(self, name, value):
601616class COtherFileTests (OtherFileTests , unittest .TestCase ):
602617 FileIO = _io .FileIO
603618 modulename = '_io'
604-
605- # TODO: RUSTPYTHON
606- @unittest .expectedFailure
607- def testUnclosedFDOnException (self ):
608- super ().testUnclosedFDOnException ()
619+ open = _io .open
609620
610621 @cpython_only
611622 def testInvalidFd_overflow (self ):
612623 # Issue 15989
613- import _testcapi
624+ _testcapi = import_module ( " _testcapi" )
614625 self .assertRaises (TypeError , self .FileIO , _testcapi .INT_MAX + 1 )
615626 self .assertRaises (TypeError , self .FileIO , _testcapi .INT_MIN - 1 )
616627
@@ -623,10 +634,15 @@ def test_open_code(self):
623634 actual = f .read ()
624635 self .assertEqual (expected , actual )
625636
637+ @unittest .expectedFailure # TODO: RUSTPYTHON
638+ def testUnclosedFDOnException (self ):
639+ return super ().testUnclosedFDOnException ()
640+
626641
627642class PyOtherFileTests (OtherFileTests , unittest .TestCase ):
628643 FileIO = _pyio .FileIO
629644 modulename = '_pyio'
645+ open = _pyio .open
630646
631647 def test_open_code (self ):
632648 # Check that the default behaviour of open_code matches
0 commit comments