From 48d4c22362e958259e5490a5e9544d0cb89dd0f0 Mon Sep 17 00:00:00 2001 From: dvermd <315743+dvermd@users.noreply.github.com> Date: Tue, 26 Sep 2023 19:19:57 +0200 Subject: [PATCH] Update fileinput to CPython 3.11.5 --- Lib/fileinput.py | 26 ++-------------- Lib/test/test_fileinput.py | 64 +++----------------------------------- 2 files changed, 8 insertions(+), 82 deletions(-) diff --git a/Lib/fileinput.py b/Lib/fileinput.py index 2ce2f91143..e234dc9ea6 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -217,15 +217,10 @@ def __init__(self, files=None, inplace=False, backup="", *, EncodingWarning, 2) # restrict mode argument to reading modes - if mode not in ('r', 'rU', 'U', 'rb'): - raise ValueError("FileInput opening mode must be one of " - "'r', 'rU', 'U' and 'rb'") - if 'U' in mode: - import warnings - warnings.warn("'U' mode is deprecated", - DeprecationWarning, 2) + if mode not in ('r', 'rb'): + raise ValueError("FileInput opening mode must be 'r' or 'rb'") self._mode = mode - self._write_mode = mode.replace('r', 'w') if 'U' not in mode else 'w' + self._write_mode = mode.replace('r', 'w') if openhook: if inplace: raise ValueError("FileInput cannot use an opening hook in inplace mode") @@ -262,21 +257,6 @@ def __next__(self): self.nextfile() # repeat with next file - def __getitem__(self, i): - import warnings - warnings.warn( - "Support for indexing FileInput objects is deprecated. " - "Use iterator protocol instead.", - DeprecationWarning, - stacklevel=2 - ) - if i != self.lineno(): - raise RuntimeError("accessing lines out of order") - try: - return self.__next__() - except StopIteration: - raise IndexError("end of input reached") - def nextfile(self): savestdout = self._savestdout self._savestdout = None diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index 270e109eb8..df894c5b2a 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -29,7 +29,6 @@ from test.support.os_helper import TESTFN from test.support.os_helper import unlink as safe_unlink from test.support import os_helper -from test.support import warnings_helper from test import support from unittest import mock @@ -230,22 +229,11 @@ def test_fileno(self): line = list(fi) self.assertEqual(fi.fileno(), -1) - # TODO: RUSTPYTHON - @unittest.expectedFailure - def test_opening_mode(self): - try: - # invalid mode, should raise ValueError - fi = FileInput(mode="w", encoding="utf-8") - self.fail("FileInput should reject invalid mode argument") - except ValueError: - pass - # try opening in universal newline mode - t1 = self.writeTmp(b"A\nB\r\nC\rD", mode="wb") - with warnings_helper.check_warnings(('', DeprecationWarning)): - fi = FileInput(files=t1, mode="U", encoding="utf-8") - with warnings_helper.check_warnings(('', DeprecationWarning)): - lines = list(fi) - self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"]) + def test_invalid_opening_mode(self): + for mode in ('w', 'rU', 'U'): + with self.subTest(mode=mode): + with self.assertRaises(ValueError): + FileInput(mode=mode) def test_stdin_binary_mode(self): with mock.patch('sys.stdin') as m_stdin: @@ -380,44 +368,6 @@ def test_empty_files_list_specified_to_constructor(self): with FileInput(files=[], encoding="utf-8") as fi: self.assertEqual(fi._files, ('-',)) - @warnings_helper.ignore_warnings(category=DeprecationWarning) - def test__getitem__(self): - """Tests invoking FileInput.__getitem__() with the current - line number""" - t = self.writeTmp("line1\nline2\n") - with FileInput(files=[t], encoding="utf-8") as fi: - retval1 = fi[0] - self.assertEqual(retval1, "line1\n") - retval2 = fi[1] - self.assertEqual(retval2, "line2\n") - - def test__getitem___deprecation(self): - t = self.writeTmp("line1\nline2\n") - with self.assertWarnsRegex(DeprecationWarning, - r'Use iterator protocol instead'): - with FileInput(files=[t]) as fi: - self.assertEqual(fi[0], "line1\n") - - @warnings_helper.ignore_warnings(category=DeprecationWarning) - def test__getitem__invalid_key(self): - """Tests invoking FileInput.__getitem__() with an index unequal to - the line number""" - t = self.writeTmp("line1\nline2\n") - with FileInput(files=[t], encoding="utf-8") as fi: - with self.assertRaises(RuntimeError) as cm: - fi[1] - self.assertEqual(cm.exception.args, ("accessing lines out of order",)) - - @warnings_helper.ignore_warnings(category=DeprecationWarning) - def test__getitem__eof(self): - """Tests invoking FileInput.__getitem__() with the line number but at - end-of-input""" - t = self.writeTmp('') - with FileInput(files=[t], encoding="utf-8") as fi: - with self.assertRaises(IndexError) as cm: - fi[0] - self.assertEqual(cm.exception.args, ("end of input reached",)) - def test_nextfile_oserror_deleting_backup(self): """Tests invoking FileInput.nextfile() when the attempt to delete the backup file would raise OSError. This error is expected to be @@ -1031,10 +981,6 @@ def check(mode, expected_lines): self.assertEqual(lines, expected_lines) check('r', ['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'])