Skip to content

Commit b84d6a3

Browse files
arihant2mathyouknowone
authored andcommitted
update fileinput to 3.13.3
1 parent d73f03b commit b84d6a3

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

Lib/fileinput.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
sequence must be accessed in strictly sequential order; sequence
5454
access and readline() cannot be mixed.
5555
56-
Optional in-place filtering: if the keyword argument inplace=1 is
56+
Optional in-place filtering: if the keyword argument inplace=True is
5757
passed to input() or to the FileInput constructor, the file is moved
5858
to a backup file and standard output is directed to the input file.
5959
This makes it possible to write a filter that rewrites its input file
@@ -399,7 +399,7 @@ def isstdin(self):
399399

400400

401401
def hook_compressed(filename, mode, *, encoding=None, errors=None):
402-
if encoding is None: # EncodingWarning is emitted in FileInput() already.
402+
if encoding is None and "b" not in mode: # EncodingWarning is emitted in FileInput() already.
403403
encoding = "locale"
404404
ext = os.path.splitext(filename)[1]
405405
if ext == '.gz':

Lib/test/test_fileinput.py

+38-22
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
from io import BytesIO, StringIO
2525
from fileinput import FileInput, hook_encoded
26-
from pathlib import Path
2726

2827
from test.support import verbose
29-
from test.support.os_helper import TESTFN
28+
from test.support.os_helper import TESTFN, FakePath
3029
from test.support.os_helper import unlink as safe_unlink
3130
from test.support import os_helper
3231
from test import support
@@ -151,7 +150,7 @@ def test_buffer_sizes(self):
151150
print('6. Inplace')
152151
savestdout = sys.stdout
153152
try:
154-
fi = FileInput(files=(t1, t2, t3, t4), inplace=1, encoding="utf-8")
153+
fi = FileInput(files=(t1, t2, t3, t4), inplace=True, encoding="utf-8")
155154
for line in fi:
156155
line = line[:-1].upper()
157156
print(line)
@@ -256,7 +255,7 @@ def test_detached_stdin_binary_mode(self):
256255
def test_file_opening_hook(self):
257256
try:
258257
# cannot use openhook and inplace mode
259-
fi = FileInput(inplace=1, openhook=lambda f, m: None)
258+
fi = FileInput(inplace=True, openhook=lambda f, m: None)
260259
self.fail("FileInput should raise if both inplace "
261260
"and openhook arguments are given")
262261
except ValueError:
@@ -478,23 +477,23 @@ def test_iteration_buffering(self):
478477
self.assertRaises(StopIteration, next, fi)
479478
self.assertEqual(src.linesread, [])
480479

481-
def test_pathlib_file(self):
482-
t1 = Path(self.writeTmp("Pathlib file."))
480+
def test_pathlike_file(self):
481+
t1 = FakePath(self.writeTmp("Path-like file."))
483482
with FileInput(t1, encoding="utf-8") as fi:
484483
line = fi.readline()
485-
self.assertEqual(line, 'Pathlib file.')
484+
self.assertEqual(line, 'Path-like file.')
486485
self.assertEqual(fi.lineno(), 1)
487486
self.assertEqual(fi.filelineno(), 1)
488487
self.assertEqual(fi.filename(), os.fspath(t1))
489488

490-
def test_pathlib_file_inplace(self):
491-
t1 = Path(self.writeTmp('Pathlib file.'))
489+
def test_pathlike_file_inplace(self):
490+
t1 = FakePath(self.writeTmp('Path-like file.'))
492491
with FileInput(t1, inplace=True, encoding="utf-8") as fi:
493492
line = fi.readline()
494-
self.assertEqual(line, 'Pathlib file.')
493+
self.assertEqual(line, 'Path-like file.')
495494
print('Modified %s' % line)
496495
with open(t1, encoding="utf-8") as f:
497-
self.assertEqual(f.read(), 'Modified Pathlib file.\n')
496+
self.assertEqual(f.read(), 'Modified Path-like file.\n')
498497

499498

500499
class MockFileInput:
@@ -855,29 +854,29 @@ def setUp(self):
855854
self.fake_open = InvocationRecorder()
856855

857856
def test_empty_string(self):
858-
self.do_test_use_builtin_open("", 1)
857+
self.do_test_use_builtin_open_text("", "r")
859858

860859
def test_no_ext(self):
861-
self.do_test_use_builtin_open("abcd", 2)
860+
self.do_test_use_builtin_open_text("abcd", "r")
862861

863862
@unittest.skipUnless(gzip, "Requires gzip and zlib")
864863
def test_gz_ext_fake(self):
865864
original_open = gzip.open
866865
gzip.open = self.fake_open
867866
try:
868-
result = fileinput.hook_compressed("test.gz", "3")
867+
result = fileinput.hook_compressed("test.gz", "r")
869868
finally:
870869
gzip.open = original_open
871870

872871
self.assertEqual(self.fake_open.invocation_count, 1)
873-
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "3"), {}))
872+
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "r"), {}))
874873

875874
@unittest.skipUnless(gzip, "Requires gzip and zlib")
876875
def test_gz_with_encoding_fake(self):
877876
original_open = gzip.open
878877
gzip.open = lambda filename, mode: io.BytesIO(b'Ex-binary string')
879878
try:
880-
result = fileinput.hook_compressed("test.gz", "3", encoding="utf-8")
879+
result = fileinput.hook_compressed("test.gz", "r", encoding="utf-8")
881880
finally:
882881
gzip.open = original_open
883882
self.assertEqual(list(result), ['Ex-binary string'])
@@ -887,23 +886,40 @@ def test_bz2_ext_fake(self):
887886
original_open = bz2.BZ2File
888887
bz2.BZ2File = self.fake_open
889888
try:
890-
result = fileinput.hook_compressed("test.bz2", "4")
889+
result = fileinput.hook_compressed("test.bz2", "r")
891890
finally:
892891
bz2.BZ2File = original_open
893892

894893
self.assertEqual(self.fake_open.invocation_count, 1)
895-
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "4"), {}))
894+
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "r"), {}))
896895

897896
def test_blah_ext(self):
898-
self.do_test_use_builtin_open("abcd.blah", "5")
897+
self.do_test_use_builtin_open_binary("abcd.blah", "rb")
899898

900899
def test_gz_ext_builtin(self):
901-
self.do_test_use_builtin_open("abcd.Gz", "6")
900+
self.do_test_use_builtin_open_binary("abcd.Gz", "rb")
902901

903902
def test_bz2_ext_builtin(self):
904-
self.do_test_use_builtin_open("abcd.Bz2", "7")
903+
self.do_test_use_builtin_open_binary("abcd.Bz2", "rb")
905904

906-
def do_test_use_builtin_open(self, filename, mode):
905+
def test_binary_mode_encoding(self):
906+
self.do_test_use_builtin_open_binary("abcd", "rb")
907+
908+
def test_text_mode_encoding(self):
909+
self.do_test_use_builtin_open_text("abcd", "r")
910+
911+
def do_test_use_builtin_open_binary(self, filename, mode):
912+
original_open = self.replace_builtin_open(self.fake_open)
913+
try:
914+
result = fileinput.hook_compressed(filename, mode)
915+
finally:
916+
self.replace_builtin_open(original_open)
917+
918+
self.assertEqual(self.fake_open.invocation_count, 1)
919+
self.assertEqual(self.fake_open.last_invocation,
920+
((filename, mode), {'encoding': None, 'errors': None}))
921+
922+
def do_test_use_builtin_open_text(self, filename, mode):
907923
original_open = self.replace_builtin_open(self.fake_open)
908924
try:
909925
result = fileinput.hook_compressed(filename, mode)

0 commit comments

Comments
 (0)