Skip to content

Commit 80e7186

Browse files
authored
Merge pull request RustPython#5071 from dvermd/fileinput_311
Update fileinput to CPython 3.11.5
2 parents 9ea4baa + 48d4c22 commit 80e7186

File tree

2 files changed

+8
-82
lines changed

2 files changed

+8
-82
lines changed

Lib/fileinput.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,10 @@ def __init__(self, files=None, inplace=False, backup="", *,
217217
EncodingWarning, 2)
218218

219219
# restrict mode argument to reading modes
220-
if mode not in ('r', 'rU', 'U', 'rb'):
221-
raise ValueError("FileInput opening mode must be one of "
222-
"'r', 'rU', 'U' and 'rb'")
223-
if 'U' in mode:
224-
import warnings
225-
warnings.warn("'U' mode is deprecated",
226-
DeprecationWarning, 2)
220+
if mode not in ('r', 'rb'):
221+
raise ValueError("FileInput opening mode must be 'r' or 'rb'")
227222
self._mode = mode
228-
self._write_mode = mode.replace('r', 'w') if 'U' not in mode else 'w'
223+
self._write_mode = mode.replace('r', 'w')
229224
if openhook:
230225
if inplace:
231226
raise ValueError("FileInput cannot use an opening hook in inplace mode")
@@ -262,21 +257,6 @@ def __next__(self):
262257
self.nextfile()
263258
# repeat with next file
264259

265-
def __getitem__(self, i):
266-
import warnings
267-
warnings.warn(
268-
"Support for indexing FileInput objects is deprecated. "
269-
"Use iterator protocol instead.",
270-
DeprecationWarning,
271-
stacklevel=2
272-
)
273-
if i != self.lineno():
274-
raise RuntimeError("accessing lines out of order")
275-
try:
276-
return self.__next__()
277-
except StopIteration:
278-
raise IndexError("end of input reached")
279-
280260
def nextfile(self):
281261
savestdout = self._savestdout
282262
self._savestdout = None

Lib/test/test_fileinput.py

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from test.support.os_helper import TESTFN
3030
from test.support.os_helper import unlink as safe_unlink
3131
from test.support import os_helper
32-
from test.support import warnings_helper
3332
from test import support
3433
from unittest import mock
3534

@@ -230,22 +229,11 @@ def test_fileno(self):
230229
line = list(fi)
231230
self.assertEqual(fi.fileno(), -1)
232231

233-
# TODO: RUSTPYTHON
234-
@unittest.expectedFailure
235-
def test_opening_mode(self):
236-
try:
237-
# invalid mode, should raise ValueError
238-
fi = FileInput(mode="w", encoding="utf-8")
239-
self.fail("FileInput should reject invalid mode argument")
240-
except ValueError:
241-
pass
242-
# try opening in universal newline mode
243-
t1 = self.writeTmp(b"A\nB\r\nC\rD", mode="wb")
244-
with warnings_helper.check_warnings(('', DeprecationWarning)):
245-
fi = FileInput(files=t1, mode="U", encoding="utf-8")
246-
with warnings_helper.check_warnings(('', DeprecationWarning)):
247-
lines = list(fi)
248-
self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
232+
def test_invalid_opening_mode(self):
233+
for mode in ('w', 'rU', 'U'):
234+
with self.subTest(mode=mode):
235+
with self.assertRaises(ValueError):
236+
FileInput(mode=mode)
249237

250238
def test_stdin_binary_mode(self):
251239
with mock.patch('sys.stdin') as m_stdin:
@@ -380,44 +368,6 @@ def test_empty_files_list_specified_to_constructor(self):
380368
with FileInput(files=[], encoding="utf-8") as fi:
381369
self.assertEqual(fi._files, ('-',))
382370

383-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
384-
def test__getitem__(self):
385-
"""Tests invoking FileInput.__getitem__() with the current
386-
line number"""
387-
t = self.writeTmp("line1\nline2\n")
388-
with FileInput(files=[t], encoding="utf-8") as fi:
389-
retval1 = fi[0]
390-
self.assertEqual(retval1, "line1\n")
391-
retval2 = fi[1]
392-
self.assertEqual(retval2, "line2\n")
393-
394-
def test__getitem___deprecation(self):
395-
t = self.writeTmp("line1\nline2\n")
396-
with self.assertWarnsRegex(DeprecationWarning,
397-
r'Use iterator protocol instead'):
398-
with FileInput(files=[t]) as fi:
399-
self.assertEqual(fi[0], "line1\n")
400-
401-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
402-
def test__getitem__invalid_key(self):
403-
"""Tests invoking FileInput.__getitem__() with an index unequal to
404-
the line number"""
405-
t = self.writeTmp("line1\nline2\n")
406-
with FileInput(files=[t], encoding="utf-8") as fi:
407-
with self.assertRaises(RuntimeError) as cm:
408-
fi[1]
409-
self.assertEqual(cm.exception.args, ("accessing lines out of order",))
410-
411-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
412-
def test__getitem__eof(self):
413-
"""Tests invoking FileInput.__getitem__() with the line number but at
414-
end-of-input"""
415-
t = self.writeTmp('')
416-
with FileInput(files=[t], encoding="utf-8") as fi:
417-
with self.assertRaises(IndexError) as cm:
418-
fi[0]
419-
self.assertEqual(cm.exception.args, ("end of input reached",))
420-
421371
def test_nextfile_oserror_deleting_backup(self):
422372
"""Tests invoking FileInput.nextfile() when the attempt to delete
423373
the backup file would raise OSError. This error is expected to be
@@ -1031,10 +981,6 @@ def check(mode, expected_lines):
1031981
self.assertEqual(lines, expected_lines)
1032982

1033983
check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
1034-
with self.assertWarns(DeprecationWarning):
1035-
check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
1036-
with self.assertWarns(DeprecationWarning):
1037-
check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
1038984
with self.assertRaises(ValueError):
1039985
check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
1040986

0 commit comments

Comments
 (0)