diff options
Diffstat (limited to 'Lib/test/test_fileinput.py')
| -rw-r--r-- | Lib/test/test_fileinput.py | 61 | 
1 files changed, 36 insertions, 25 deletions
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index b9523cc..4765a05 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -19,11 +19,12 @@ try:  except ImportError:      gzip = None -from io import StringIO +from io import BytesIO, StringIO  from fileinput import FileInput, hook_encoded -from test.support import verbose, TESTFN, run_unittest +from test.support import verbose, TESTFN, run_unittest, check_warnings  from test.support import unlink as safe_unlink +from unittest import mock  # The fileinput module has 2 interfaces: the FileInput class which does @@ -224,12 +225,21 @@ class FileInputTests(unittest.TestCase):          try:              # try opening in universal newline mode              t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb") -            fi = FileInput(files=t1, mode="U") -            lines = list(fi) +            with check_warnings(('', DeprecationWarning)): +                fi = FileInput(files=t1, mode="U") +            with check_warnings(('', DeprecationWarning)): +                lines = list(fi)              self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])          finally:              remove_tempfiles(t1) +    def test_stdin_binary_mode(self): +        with mock.patch('sys.stdin') as m_stdin: +            m_stdin.buffer = BytesIO(b'spam, bacon, sausage, and spam') +            fi = FileInput(files=['-'], mode='rb') +            lines = list(fi) +            self.assertEqual(lines, [b'spam, bacon, sausage, and spam']) +      def test_file_opening_hook(self):          try:              # cannot use openhook and inplace mode @@ -278,6 +288,21 @@ class FileInputTests(unittest.TestCase):              with self.assertRaises(UnicodeDecodeError):                  # Read to the end of file.                  list(fi) +            self.assertEqual(fi.readline(), '') +            self.assertEqual(fi.readline(), '') + +    def test_readline_binary_mode(self): +        with open(TESTFN, 'wb') as f: +            f.write(b'A\nB\r\nC\rD') +        self.addCleanup(safe_unlink, TESTFN) + +        with FileInput(files=TESTFN, mode='rb') as fi: +            self.assertEqual(fi.readline(), b'A\n') +            self.assertEqual(fi.readline(), b'B\r\n') +            self.assertEqual(fi.readline(), b'C\rD') +            # Read to the end of file. +            self.assertEqual(fi.readline(), b'') +            self.assertEqual(fi.readline(), b'')      def test_context_manager(self):          try: @@ -296,8 +321,8 @@ class FileInputTests(unittest.TestCase):          try:              t1 = writeTmp(1, [""])              with FileInput(files=t1) as fi: -                raise IOError -        except IOError: +                raise OSError +        except OSError:              self.assertEqual(fi._files, ())          finally:              remove_tempfiles(t1) @@ -869,27 +894,13 @@ class Test_hook_encoded(unittest.TestCase):              self.assertEqual(lines, expected_lines)          check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac']) -        check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac']) -        check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac']) +        with self.assertWarns(DeprecationWarning): +            check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac']) +        with self.assertWarns(DeprecationWarning): +            check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac'])          with self.assertRaises(ValueError):              check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac']) -def test_main(): -    run_unittest( -        BufferSizesTests, -        FileInputTests, -        Test_fileinput_input, -        Test_fileinput_close, -        Test_fileinput_nextfile, -        Test_fileinput_filename, -        Test_fileinput_lineno, -        Test_fileinput_filelineno, -        Test_fileinput_fileno, -        Test_fileinput_isfirstline, -        Test_fileinput_isstdin, -        Test_hook_compressed, -        Test_hook_encoded, -    )  if __name__ == "__main__": -    test_main() +    unittest.main()  | 
