Kaydet (Commit) 002665a9 authored tarafından Roy Williams's avatar Roy Williams Kaydeden (comit) Łukasz Langa

bpo-30432: FileInput doesn't accept PathLike objects for file names (#1732)

* Allow FileInput to accept a single PathLike object as a parameter for `files`

Fixes

bpo-30432: FileInput doesn't accept PathLike objects for file names

* Address comments from @ambv
üst d618c8c6
......@@ -189,6 +189,8 @@ class FileInput:
mode="r", openhook=None):
if isinstance(files, str):
files = (files,)
elif isinstance(files, os.PathLike):
files = (os.fspath(files), )
else:
if files is None:
files = sys.argv[1:]
......
......@@ -21,6 +21,7 @@ except ImportError:
from io import BytesIO, StringIO
from fileinput import FileInput, hook_encoded
from pathlib import Path
from test.support import verbose, TESTFN, check_warnings
from test.support import unlink as safe_unlink
......@@ -530,6 +531,20 @@ class FileInputTests(unittest.TestCase):
self.assertRaises(StopIteration, next, fi)
self.assertEqual(src.linesread, [])
def test_pathlib_file(self):
t1 = None
try:
t1 = Path(writeTmp(1, ["Pathlib file."]))
with FileInput(t1) as fi:
line = fi.readline()
self.assertEqual(line, 'Pathlib file.')
self.assertEqual(fi.lineno(), 1)
self.assertEqual(fi.filelineno(), 1)
self.assertEqual(fi.filename(), os.fspath(t1))
finally:
remove_tempfiles(t1)
class MockFileInput:
"""A class that mocks out fileinput.FileInput for use during unit tests"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment