diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index f9f864bba..247c9675e 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -7,8 +7,10 @@ from weakref import proxy import io import _pyio as pyio -from test.support import os_helper, warnings_helper +from test.support import gc_collect from test.support.os_helper import TESTFN +from test.support import os_helper +from test.support import warnings_helper from collections import UserList class AutoFileTests: @@ -29,6 +31,7 @@ class AutoFileTests: self.assertEqual(self.f.tell(), p.tell()) self.f.close() self.f = None + gc_collect() # For PyPy or other GCs. self.assertRaises(ReferenceError, getattr, p, 'tell') def testAttributes(self): @@ -51,7 +54,7 @@ class AutoFileTests: # verify readinto refuses text files a = array('b', b'x'*10) self.f.close() - self.f = self.open(TESTFN, 'r') + self.f = self.open(TESTFN, encoding="utf-8") if hasattr(self.f, "readinto"): self.assertRaises(TypeError, self.f.readinto, a) @@ -132,7 +135,6 @@ class AutoFileTests: class CAutoFileTests(AutoFileTests, unittest.TestCase): open = io.open -@unittest.skipIf(sys.platform == "win32", "TODO: RUSTPYTHON, test setUp errors on Windows") class PyAutoFileTests(AutoFileTests, unittest.TestCase): open = staticmethod(pyio.open) @@ -154,6 +156,22 @@ class OtherFileTests: f.close() self.fail('%r is an invalid file mode' % mode) + def testStdin(self): + if sys.platform == 'osf1V5': + # This causes the interpreter to exit on OSF1 v5.1. + self.skipTest( + ' sys.stdin.seek(-1) may crash the interpreter on OSF1.' + ' Test manually.') + + if not sys.stdin.isatty(): + # Issue 14853: stdin becomes seekable when redirected to a file + self.skipTest('stdin must be a TTY in this test') + + with self.assertRaises((IOError, ValueError)): + sys.stdin.seek(-1) + with self.assertRaises((IOError, ValueError)): + sys.stdin.truncate() + def testBadModeArgument(self): # verify that we get a sensible error message for bad mode argument bad_mode = "qwerty" @@ -334,5 +352,6 @@ class COtherFileTests(OtherFileTests, unittest.TestCase): class PyOtherFileTests(OtherFileTests, unittest.TestCase): open = staticmethod(pyio.open) + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 59bdd2fe7..8577c48c7 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -9,8 +9,8 @@ from array import array from weakref import proxy from functools import wraps -from test.support import run_unittest, cpython_only, swap_attr -from test.support.os_helper import TESTFN, TESTFN_UNICODE, make_bad_fd +from test.support import cpython_only, swap_attr, gc_collect +from test.support.os_helper import (TESTFN, TESTFN_UNICODE, make_bad_fd) from test.support.warnings_helper import check_warnings from collections import UserList @@ -36,6 +36,7 @@ class AutoFileTests: self.assertEqual(self.f.tell(), p.tell()) self.f.close() self.f = None + gc_collect() # For PyPy or other GCs. self.assertRaises(ReferenceError, getattr, p, 'tell') def testSeekTell(self): @@ -177,7 +178,6 @@ class AutoFileTests: finally: os.close(fd) - @unittest.skipIf(getattr(sys, '_rustpython_debugbuild', False), "TODO: RUSTPYTHON, stack overflow on debug build") def testRecursiveRepr(self): # Issue #25455 with swap_attr(self.f, 'name', self.f): @@ -664,15 +664,12 @@ class PyOtherFileTests(OtherFileTests, unittest.TestCase): self.assertNotEqual(w.warnings, []) -def test_main(): +def tearDownModule(): # Historically, these tests have been sloppy about removing TESTFN. # So get rid of it no matter what. - try: - run_unittest(CAutoFileTests, PyAutoFileTests, - COtherFileTests, PyOtherFileTests) - finally: - if os.path.exists(TESTFN): - os.unlink(TESTFN) + if os.path.exists(TESTFN): + os.unlink(TESTFN) + if __name__ == '__main__': - test_main() + unittest.main()