Skip to content

Update fileinput to CPython 3.11.5 #5071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions Lib/fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
64 changes: 5 additions & 59 deletions Lib/test/test_fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'])

Expand Down