blob: d5edf74938548ef72d63fc1e38d68592b86b3f5d [file] [log] [blame]
Tim Peters3230d5c2001-07-11 22:21:17 +00001'''
2Tests for fileinput module.
3Nick Mathewson
4'''
Benjamin Petersoneb462882011-03-15 09:50:18 -05005import os
6import sys
7import re
briancurtin906f0c42011-03-15 10:29:41 -04008import fileinput
9import collections
Florent Xiclunaa011e2b2011-11-07 19:43:07 +010010import builtins
Serhiy Storchaka5f48e262018-06-05 12:08:36 +030011import tempfile
Benjamin Petersoneb462882011-03-15 09:50:18 -050012import unittest
13
briancurtinf84f3c32011-03-18 13:03:17 -050014try:
15 import bz2
16except ImportError:
17 bz2 = None
Ezio Melottic3afbb92011-05-14 10:10:53 +030018try:
19 import gzip
20except ImportError:
21 gzip = None
briancurtinf84f3c32011-03-18 13:03:17 -050022
Serhiy Storchaka946cfc32014-05-14 21:08:33 +030023from io import BytesIO, StringIO
Benjamin Petersoneb462882011-03-15 09:50:18 -050024from fileinput import FileInput, hook_encoded
Roy Williams002665a2017-05-22 22:24:17 -070025from pathlib import Path
Benjamin Petersoneb462882011-03-15 09:50:18 -050026
Hai Shi847f94f2020-06-26 01:17:57 +080027from test.support import verbose
28from test.support.os_helper import TESTFN
29from test.support.os_helper import unlink as safe_unlink
30from test.support import os_helper
31from test.support import warnings_helper
Martin Panter7978e102016-01-16 06:26:54 +000032from test import support
Serhiy Storchaka946cfc32014-05-14 21:08:33 +030033from unittest import mock
Benjamin Petersoneb462882011-03-15 09:50:18 -050034
Tim Peters3230d5c2001-07-11 22:21:17 +000035
36# The fileinput module has 2 interfaces: the FileInput class which does
37# all the work, and a few functions (input, etc.) that use a global _state
briancurtin906f0c42011-03-15 10:29:41 -040038# variable.
Tim Peters3230d5c2001-07-11 22:21:17 +000039
Serhiy Storchaka5f48e262018-06-05 12:08:36 +030040class BaseTests:
41 # Write a content (str or bytes) to temp file, and return the
42 # temp file's name.
43 def writeTmp(self, content, *, mode='w'): # opening in text mode is the default
44 fd, name = tempfile.mkstemp()
Hai Shi847f94f2020-06-26 01:17:57 +080045 self.addCleanup(os_helper.unlink, name)
Serhiy Storchaka5f48e262018-06-05 12:08:36 +030046 with open(fd, mode) as f:
47 f.write(content)
48 return name
Tim Peters3230d5c2001-07-11 22:21:17 +000049
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +020050class LineReader:
51
52 def __init__(self):
53 self._linesread = []
54
55 @property
56 def linesread(self):
57 try:
58 return self._linesread[:]
59 finally:
60 self._linesread = []
61
62 def openhook(self, filename, mode):
63 self.it = iter(filename.splitlines(True))
64 return self
65
66 def readline(self, size=None):
67 line = next(self.it, '')
68 self._linesread.append(line)
69 return line
70
71 def readlines(self, hint=-1):
72 lines = []
73 size = 0
74 while True:
75 line = self.readline()
76 if not line:
77 return lines
78 lines.append(line)
79 size += len(line)
80 if size >= hint:
81 return lines
82
83 def close(self):
84 pass
85
Serhiy Storchaka5f48e262018-06-05 12:08:36 +030086class BufferSizesTests(BaseTests, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +000087 def test_buffer_sizes(self):
Tim Peters3230d5c2001-07-11 22:21:17 +000088
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -070089 t1 = self.writeTmp(''.join("Line %s of file 1\n" % (i+1) for i in range(15)))
90 t2 = self.writeTmp(''.join("Line %s of file 2\n" % (i+1) for i in range(10)))
91 t3 = self.writeTmp(''.join("Line %s of file 3\n" % (i+1) for i in range(5)))
92 t4 = self.writeTmp(''.join("Line %s of file 4\n" % (i+1) for i in range(1)))
93
Guido van Rossumd8faa362007-04-27 19:54:29 +000094 pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
Tim Peters3230d5c2001-07-11 22:21:17 +000095
Guido van Rossumd8faa362007-04-27 19:54:29 +000096 if verbose:
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -070097 print('1. Simple iteration')
98 fi = FileInput(files=(t1, t2, t3, t4))
Tim Peters3230d5c2001-07-11 22:21:17 +000099 lines = list(fi)
Tim Peters3230d5c2001-07-11 22:21:17 +0000100 fi.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000101 self.assertEqual(len(lines), 31)
102 self.assertEqual(lines[4], 'Line 5 of file 1\n')
103 self.assertEqual(lines[30], 'Line 1 of file 4\n')
104 self.assertEqual(fi.lineno(), 31)
105 self.assertEqual(fi.filename(), t4)
Tim Peters3230d5c2001-07-11 22:21:17 +0000106
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107 if verbose:
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700108 print('2. Status variables')
109 fi = FileInput(files=(t1, t2, t3, t4))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000110 s = "x"
111 while s and s != 'Line 6 of file 2\n':
112 s = fi.readline()
113 self.assertEqual(fi.filename(), t2)
114 self.assertEqual(fi.lineno(), 21)
115 self.assertEqual(fi.filelineno(), 6)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000116 self.assertFalse(fi.isfirstline())
117 self.assertFalse(fi.isstdin())
Tim Peters3230d5c2001-07-11 22:21:17 +0000118
Guido van Rossumd8faa362007-04-27 19:54:29 +0000119 if verbose:
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700120 print('3. Nextfile')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000121 fi.nextfile()
122 self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
123 self.assertEqual(fi.lineno(), 22)
124 fi.close()
Tim Peters3230d5c2001-07-11 22:21:17 +0000125
Guido van Rossumd8faa362007-04-27 19:54:29 +0000126 if verbose:
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700127 print('4. Stdin')
128 fi = FileInput(files=(t1, t2, t3, t4, '-'))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000129 savestdin = sys.stdin
130 try:
131 sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
132 lines = list(fi)
133 self.assertEqual(len(lines), 33)
134 self.assertEqual(lines[32], 'Line 2 of stdin\n')
135 self.assertEqual(fi.filename(), '<stdin>')
136 fi.nextfile()
137 finally:
138 sys.stdin = savestdin
Tim Peters3230d5c2001-07-11 22:21:17 +0000139
Guido van Rossumd8faa362007-04-27 19:54:29 +0000140 if verbose:
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700141 print('5. Boundary conditions')
142 fi = FileInput(files=(t1, t2, t3, t4))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000143 self.assertEqual(fi.lineno(), 0)
144 self.assertEqual(fi.filename(), None)
145 fi.nextfile()
146 self.assertEqual(fi.lineno(), 0)
147 self.assertEqual(fi.filename(), None)
Tim Peters3230d5c2001-07-11 22:21:17 +0000148
Guido van Rossumd8faa362007-04-27 19:54:29 +0000149 if verbose:
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700150 print('6. Inplace')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000151 savestdout = sys.stdout
152 try:
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700153 fi = FileInput(files=(t1, t2, t3, t4), inplace=1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000154 for line in fi:
155 line = line[:-1].upper()
156 print(line)
157 fi.close()
158 finally:
159 sys.stdout = savestdout
Tim Peters3230d5c2001-07-11 22:21:17 +0000160
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700161 fi = FileInput(files=(t1, t2, t3, t4))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000162 for line in fi:
163 self.assertEqual(line[-1], '\n')
164 m = pat.match(line[:-1])
165 self.assertNotEqual(m, None)
166 self.assertEqual(int(m.group(1)), fi.filelineno())
167 fi.close()
Georg Brandle4662172006-02-19 09:51:27 +0000168
briancurtin906f0c42011-03-15 10:29:41 -0400169class UnconditionallyRaise:
170 def __init__(self, exception_type):
171 self.exception_type = exception_type
172 self.invoked = False
173 def __call__(self, *args, **kwargs):
174 self.invoked = True
175 raise self.exception_type()
176
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300177class FileInputTests(BaseTests, unittest.TestCase):
briancurtin906f0c42011-03-15 10:29:41 -0400178
Guido van Rossumd8faa362007-04-27 19:54:29 +0000179 def test_zero_byte_files(self):
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300180 t1 = self.writeTmp("")
181 t2 = self.writeTmp("")
182 t3 = self.writeTmp("The only line there is.\n")
183 t4 = self.writeTmp("")
184 fi = FileInput(files=(t1, t2, t3, t4))
Georg Brandl67e9fb92006-02-19 13:56:17 +0000185
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300186 line = fi.readline()
187 self.assertEqual(line, 'The only line there is.\n')
188 self.assertEqual(fi.lineno(), 1)
189 self.assertEqual(fi.filelineno(), 1)
190 self.assertEqual(fi.filename(), t3)
Georg Brandlc029f872006-02-19 14:12:34 +0000191
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300192 line = fi.readline()
193 self.assertFalse(line)
194 self.assertEqual(fi.lineno(), 1)
195 self.assertEqual(fi.filelineno(), 0)
196 self.assertEqual(fi.filename(), t4)
197 fi.close()
Georg Brandlc98eeed2006-02-19 14:57:47 +0000198
Guido van Rossumd8faa362007-04-27 19:54:29 +0000199 def test_files_that_dont_end_with_newline(self):
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300200 t1 = self.writeTmp("A\nB\nC")
201 t2 = self.writeTmp("D\nE\nF")
202 fi = FileInput(files=(t1, t2))
203 lines = list(fi)
204 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
205 self.assertEqual(fi.filelineno(), 3)
206 self.assertEqual(fi.lineno(), 6)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000208## def test_unicode_filenames(self):
209## # XXX A unicode string is always returned by writeTmp.
210## # So is this needed?
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300211## t1 = self.writeTmp("A\nB")
212## encoding = sys.getfilesystemencoding()
213## if encoding is None:
214## encoding = 'ascii'
215## fi = FileInput(files=str(t1, encoding))
216## lines = list(fi)
217## self.assertEqual(lines, ["A\n", "B"])
Guido van Rossumd8faa362007-04-27 19:54:29 +0000218
219 def test_fileno(self):
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300220 t1 = self.writeTmp("A\nB")
221 t2 = self.writeTmp("C\nD")
222 fi = FileInput(files=(t1, t2))
223 self.assertEqual(fi.fileno(), -1)
224 line = next(fi)
225 self.assertNotEqual(fi.fileno(), -1)
226 fi.nextfile()
227 self.assertEqual(fi.fileno(), -1)
228 line = list(fi)
229 self.assertEqual(fi.fileno(), -1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000230
231 def test_opening_mode(self):
Victor Stinner942f7a22020-03-04 18:50:22 +0100232 try:
233 # invalid mode, should raise ValueError
234 fi = FileInput(mode="w")
235 self.fail("FileInput should reject invalid mode argument")
236 except ValueError:
237 pass
238 # try opening in universal newline mode
239 t1 = self.writeTmp(b"A\nB\r\nC\rD", mode="wb")
Hai Shi847f94f2020-06-26 01:17:57 +0800240 with warnings_helper.check_warnings(('', DeprecationWarning)):
Victor Stinner942f7a22020-03-04 18:50:22 +0100241 fi = FileInput(files=t1, mode="U")
Hai Shi847f94f2020-06-26 01:17:57 +0800242 with warnings_helper.check_warnings(('', DeprecationWarning)):
Victor Stinner942f7a22020-03-04 18:50:22 +0100243 lines = list(fi)
244 self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
Guido van Rossumd8faa362007-04-27 19:54:29 +0000245
Serhiy Storchaka946cfc32014-05-14 21:08:33 +0300246 def test_stdin_binary_mode(self):
247 with mock.patch('sys.stdin') as m_stdin:
248 m_stdin.buffer = BytesIO(b'spam, bacon, sausage, and spam')
249 fi = FileInput(files=['-'], mode='rb')
250 lines = list(fi)
251 self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
252
R David Murray830207e2016-01-02 15:41:41 -0500253 def test_detached_stdin_binary_mode(self):
254 orig_stdin = sys.stdin
255 try:
256 sys.stdin = BytesIO(b'spam, bacon, sausage, and spam')
257 self.assertFalse(hasattr(sys.stdin, 'buffer'))
258 fi = FileInput(files=['-'], mode='rb')
259 lines = list(fi)
260 self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
261 finally:
262 sys.stdin = orig_stdin
263
Guido van Rossume22905a2007-08-27 23:09:25 +0000264 def test_file_opening_hook(self):
265 try:
266 # cannot use openhook and inplace mode
267 fi = FileInput(inplace=1, openhook=lambda f, m: None)
268 self.fail("FileInput should raise if both inplace "
269 "and openhook arguments are given")
270 except ValueError:
271 pass
272 try:
273 fi = FileInput(openhook=1)
274 self.fail("FileInput should check openhook for being callable")
275 except ValueError:
276 pass
briancurtin906f0c42011-03-15 10:29:41 -0400277
278 class CustomOpenHook:
279 def __init__(self):
280 self.invoked = False
281 def __call__(self, *args):
282 self.invoked = True
283 return open(*args)
284
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300285 t = self.writeTmp("\n")
briancurtin906f0c42011-03-15 10:29:41 -0400286 custom_open_hook = CustomOpenHook()
287 with FileInput([t], openhook=custom_open_hook) as fi:
288 fi.readline()
289 self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000290
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200291 def test_readline(self):
292 with open(TESTFN, 'wb') as f:
293 f.write(b'A\nB\r\nC\r')
294 # Fill TextIOWrapper buffer.
295 f.write(b'123456789\n' * 1000)
296 # Issue #20501: readline() shouldn't read whole file.
297 f.write(b'\x80')
298 self.addCleanup(safe_unlink, TESTFN)
299
300 with FileInput(files=TESTFN,
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200301 openhook=hook_encoded('ascii')) as fi:
Serhiy Storchaka682ea5f2014-03-03 21:17:17 +0200302 try:
303 self.assertEqual(fi.readline(), 'A\n')
304 self.assertEqual(fi.readline(), 'B\n')
305 self.assertEqual(fi.readline(), 'C\n')
306 except UnicodeDecodeError:
307 self.fail('Read to end of file')
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200308 with self.assertRaises(UnicodeDecodeError):
309 # Read to the end of file.
310 list(fi)
Serhiy Storchaka314464d2015-11-01 16:43:58 +0200311 self.assertEqual(fi.readline(), '')
312 self.assertEqual(fi.readline(), '')
313
314 def test_readline_binary_mode(self):
315 with open(TESTFN, 'wb') as f:
316 f.write(b'A\nB\r\nC\rD')
317 self.addCleanup(safe_unlink, TESTFN)
318
319 with FileInput(files=TESTFN, mode='rb') as fi:
320 self.assertEqual(fi.readline(), b'A\n')
321 self.assertEqual(fi.readline(), b'B\r\n')
322 self.assertEqual(fi.readline(), b'C\rD')
323 # Read to the end of file.
324 self.assertEqual(fi.readline(), b'')
325 self.assertEqual(fi.readline(), b'')
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200326
Berker Peksagbe6dbfb2019-04-29 17:55:39 +0300327 def test_inplace_binary_write_mode(self):
328 temp_file = self.writeTmp(b'Initial text.', mode='wb')
329 with FileInput(temp_file, mode='rb', inplace=True) as fobj:
330 line = fobj.readline()
331 self.assertEqual(line, b'Initial text.')
332 # print() cannot be used with files opened in binary mode.
333 sys.stdout.write(b'New line.')
334 with open(temp_file, 'rb') as f:
335 self.assertEqual(f.read(), b'New line.')
336
Georg Brandl6cb7b652010-07-31 20:08:15 +0000337 def test_context_manager(self):
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300338 t1 = self.writeTmp("A\nB\nC")
339 t2 = self.writeTmp("D\nE\nF")
340 with FileInput(files=(t1, t2)) as fi:
341 lines = list(fi)
342 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
343 self.assertEqual(fi.filelineno(), 3)
344 self.assertEqual(fi.lineno(), 6)
345 self.assertEqual(fi._files, ())
Georg Brandl6cb7b652010-07-31 20:08:15 +0000346
347 def test_close_on_exception(self):
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300348 t1 = self.writeTmp("")
Georg Brandl6cb7b652010-07-31 20:08:15 +0000349 try:
Georg Brandl6cb7b652010-07-31 20:08:15 +0000350 with FileInput(files=t1) as fi:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200351 raise OSError
352 except OSError:
Georg Brandl6cb7b652010-07-31 20:08:15 +0000353 self.assertEqual(fi._files, ())
Georg Brandl6cb7b652010-07-31 20:08:15 +0000354
briancurtin906f0c42011-03-15 10:29:41 -0400355 def test_empty_files_list_specified_to_constructor(self):
356 with FileInput(files=[]) as fi:
Brett Cannond47af532011-03-15 15:55:12 -0400357 self.assertEqual(fi._files, ('-',))
briancurtin906f0c42011-03-15 10:29:41 -0400358
Hai Shi847f94f2020-06-26 01:17:57 +0800359 @warnings_helper.ignore_warnings(category=DeprecationWarning)
briancurtin906f0c42011-03-15 10:29:41 -0400360 def test__getitem__(self):
361 """Tests invoking FileInput.__getitem__() with the current
362 line number"""
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300363 t = self.writeTmp("line1\nline2\n")
briancurtin906f0c42011-03-15 10:29:41 -0400364 with FileInput(files=[t]) as fi:
365 retval1 = fi[0]
366 self.assertEqual(retval1, "line1\n")
367 retval2 = fi[1]
368 self.assertEqual(retval2, "line2\n")
369
Berker Peksag84a13fb2018-08-11 09:05:04 +0300370 def test__getitem___deprecation(self):
371 t = self.writeTmp("line1\nline2\n")
372 with self.assertWarnsRegex(DeprecationWarning,
373 r'Use iterator protocol instead'):
374 with FileInput(files=[t]) as fi:
375 self.assertEqual(fi[0], "line1\n")
376
Hai Shi847f94f2020-06-26 01:17:57 +0800377 @warnings_helper.ignore_warnings(category=DeprecationWarning)
briancurtin906f0c42011-03-15 10:29:41 -0400378 def test__getitem__invalid_key(self):
379 """Tests invoking FileInput.__getitem__() with an index unequal to
380 the line number"""
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300381 t = self.writeTmp("line1\nline2\n")
briancurtin906f0c42011-03-15 10:29:41 -0400382 with FileInput(files=[t]) as fi:
383 with self.assertRaises(RuntimeError) as cm:
384 fi[1]
Brett Cannond47af532011-03-15 15:55:12 -0400385 self.assertEqual(cm.exception.args, ("accessing lines out of order",))
briancurtin906f0c42011-03-15 10:29:41 -0400386
Hai Shi847f94f2020-06-26 01:17:57 +0800387 @warnings_helper.ignore_warnings(category=DeprecationWarning)
briancurtin906f0c42011-03-15 10:29:41 -0400388 def test__getitem__eof(self):
389 """Tests invoking FileInput.__getitem__() with the line number but at
390 end-of-input"""
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300391 t = self.writeTmp('')
briancurtin906f0c42011-03-15 10:29:41 -0400392 with FileInput(files=[t]) as fi:
393 with self.assertRaises(IndexError) as cm:
394 fi[0]
Brett Cannond47af532011-03-15 15:55:12 -0400395 self.assertEqual(cm.exception.args, ("end of input reached",))
briancurtin906f0c42011-03-15 10:29:41 -0400396
397 def test_nextfile_oserror_deleting_backup(self):
398 """Tests invoking FileInput.nextfile() when the attempt to delete
399 the backup file would raise OSError. This error is expected to be
400 silently ignored"""
401
402 os_unlink_orig = os.unlink
403 os_unlink_replacement = UnconditionallyRaise(OSError)
404 try:
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300405 t = self.writeTmp("\n")
Hai Shi847f94f2020-06-26 01:17:57 +0800406 self.addCleanup(safe_unlink, t + '.bak')
briancurtin906f0c42011-03-15 10:29:41 -0400407 with FileInput(files=[t], inplace=True) as fi:
408 next(fi) # make sure the file is opened
409 os.unlink = os_unlink_replacement
410 fi.nextfile()
411 finally:
412 os.unlink = os_unlink_orig
413
414 # sanity check to make sure that our test scenario was actually hit
415 self.assertTrue(os_unlink_replacement.invoked,
416 "os.unlink() was not invoked")
417
418 def test_readline_os_fstat_raises_OSError(self):
419 """Tests invoking FileInput.readline() when os.fstat() raises OSError.
420 This exception should be silently discarded."""
421
422 os_fstat_orig = os.fstat
423 os_fstat_replacement = UnconditionallyRaise(OSError)
424 try:
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300425 t = self.writeTmp("\n")
briancurtin906f0c42011-03-15 10:29:41 -0400426 with FileInput(files=[t], inplace=True) as fi:
427 os.fstat = os_fstat_replacement
428 fi.readline()
429 finally:
430 os.fstat = os_fstat_orig
431
432 # sanity check to make sure that our test scenario was actually hit
433 self.assertTrue(os_fstat_replacement.invoked,
434 "os.fstat() was not invoked")
435
briancurtin906f0c42011-03-15 10:29:41 -0400436 def test_readline_os_chmod_raises_OSError(self):
437 """Tests invoking FileInput.readline() when os.chmod() raises OSError.
438 This exception should be silently discarded."""
439
440 os_chmod_orig = os.chmod
441 os_chmod_replacement = UnconditionallyRaise(OSError)
442 try:
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300443 t = self.writeTmp("\n")
briancurtin906f0c42011-03-15 10:29:41 -0400444 with FileInput(files=[t], inplace=True) as fi:
445 os.chmod = os_chmod_replacement
446 fi.readline()
447 finally:
448 os.chmod = os_chmod_orig
449
450 # sanity check to make sure that our test scenario was actually hit
451 self.assertTrue(os_chmod_replacement.invoked,
452 "os.fstat() was not invoked")
453
454 def test_fileno_when_ValueError_raised(self):
455 class FilenoRaisesValueError(UnconditionallyRaise):
456 def __init__(self):
457 UnconditionallyRaise.__init__(self, ValueError)
458 def fileno(self):
459 self.__call__()
460
461 unconditionally_raise_ValueError = FilenoRaisesValueError()
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300462 t = self.writeTmp("\n")
briancurtin906f0c42011-03-15 10:29:41 -0400463 with FileInput(files=[t]) as fi:
464 file_backup = fi._file
465 try:
466 fi._file = unconditionally_raise_ValueError
467 result = fi.fileno()
468 finally:
469 fi._file = file_backup # make sure the file gets cleaned up
470
471 # sanity check to make sure that our test scenario was actually hit
472 self.assertTrue(unconditionally_raise_ValueError.invoked,
473 "_file.fileno() was not invoked")
474
475 self.assertEqual(result, -1, "fileno() should return -1")
476
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200477 def test_readline_buffering(self):
478 src = LineReader()
479 with FileInput(files=['line1\nline2', 'line3\n'],
480 openhook=src.openhook) as fi:
481 self.assertEqual(src.linesread, [])
482 self.assertEqual(fi.readline(), 'line1\n')
483 self.assertEqual(src.linesread, ['line1\n'])
484 self.assertEqual(fi.readline(), 'line2')
485 self.assertEqual(src.linesread, ['line2'])
486 self.assertEqual(fi.readline(), 'line3\n')
487 self.assertEqual(src.linesread, ['', 'line3\n'])
488 self.assertEqual(fi.readline(), '')
489 self.assertEqual(src.linesread, [''])
490 self.assertEqual(fi.readline(), '')
491 self.assertEqual(src.linesread, [])
492
493 def test_iteration_buffering(self):
494 src = LineReader()
495 with FileInput(files=['line1\nline2', 'line3\n'],
496 openhook=src.openhook) as fi:
497 self.assertEqual(src.linesread, [])
498 self.assertEqual(next(fi), 'line1\n')
499 self.assertEqual(src.linesread, ['line1\n'])
500 self.assertEqual(next(fi), 'line2')
501 self.assertEqual(src.linesread, ['line2'])
502 self.assertEqual(next(fi), 'line3\n')
503 self.assertEqual(src.linesread, ['', 'line3\n'])
504 self.assertRaises(StopIteration, next, fi)
505 self.assertEqual(src.linesread, [''])
506 self.assertRaises(StopIteration, next, fi)
507 self.assertEqual(src.linesread, [])
508
Roy Williams002665a2017-05-22 22:24:17 -0700509 def test_pathlib_file(self):
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300510 t1 = Path(self.writeTmp("Pathlib file."))
511 with FileInput(t1) as fi:
512 line = fi.readline()
513 self.assertEqual(line, 'Pathlib file.')
514 self.assertEqual(fi.lineno(), 1)
515 self.assertEqual(fi.filelineno(), 1)
516 self.assertEqual(fi.filename(), os.fspath(t1))
Roy Williams002665a2017-05-22 22:24:17 -0700517
Zhiming Wang06de1ae2017-09-05 01:37:24 +0800518 def test_pathlib_file_inplace(self):
Serhiy Storchaka5f48e262018-06-05 12:08:36 +0300519 t1 = Path(self.writeTmp('Pathlib file.'))
520 with FileInput(t1, inplace=True) as fi:
521 line = fi.readline()
522 self.assertEqual(line, 'Pathlib file.')
523 print('Modified %s' % line)
524 with open(t1) as f:
525 self.assertEqual(f.read(), 'Modified Pathlib file.\n')
Zhiming Wang06de1ae2017-09-05 01:37:24 +0800526
Roy Williams002665a2017-05-22 22:24:17 -0700527
briancurtin906f0c42011-03-15 10:29:41 -0400528class MockFileInput:
529 """A class that mocks out fileinput.FileInput for use during unit tests"""
530
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700531 def __init__(self, files=None, inplace=False, backup="", *,
briancurtin906f0c42011-03-15 10:29:41 -0400532 mode="r", openhook=None):
533 self.files = files
534 self.inplace = inplace
535 self.backup = backup
briancurtin906f0c42011-03-15 10:29:41 -0400536 self.mode = mode
537 self.openhook = openhook
538 self._file = None
539 self.invocation_counts = collections.defaultdict(lambda: 0)
540 self.return_values = {}
541
542 def close(self):
543 self.invocation_counts["close"] += 1
544
545 def nextfile(self):
546 self.invocation_counts["nextfile"] += 1
547 return self.return_values["nextfile"]
548
549 def filename(self):
550 self.invocation_counts["filename"] += 1
551 return self.return_values["filename"]
552
553 def lineno(self):
554 self.invocation_counts["lineno"] += 1
555 return self.return_values["lineno"]
556
557 def filelineno(self):
558 self.invocation_counts["filelineno"] += 1
559 return self.return_values["filelineno"]
560
561 def fileno(self):
562 self.invocation_counts["fileno"] += 1
563 return self.return_values["fileno"]
564
565 def isfirstline(self):
566 self.invocation_counts["isfirstline"] += 1
567 return self.return_values["isfirstline"]
568
569 def isstdin(self):
570 self.invocation_counts["isstdin"] += 1
571 return self.return_values["isstdin"]
572
573class BaseFileInputGlobalMethodsTest(unittest.TestCase):
574 """Base class for unit tests for the global function of
575 the fileinput module."""
576
577 def setUp(self):
578 self._orig_state = fileinput._state
579 self._orig_FileInput = fileinput.FileInput
580 fileinput.FileInput = MockFileInput
581
582 def tearDown(self):
583 fileinput.FileInput = self._orig_FileInput
584 fileinput._state = self._orig_state
585
586 def assertExactlyOneInvocation(self, mock_file_input, method_name):
587 # assert that the method with the given name was invoked once
588 actual_count = mock_file_input.invocation_counts[method_name]
589 self.assertEqual(actual_count, 1, method_name)
590 # assert that no other unexpected methods were invoked
591 actual_total_count = len(mock_file_input.invocation_counts)
592 self.assertEqual(actual_total_count, 1)
593
594class Test_fileinput_input(BaseFileInputGlobalMethodsTest):
595 """Unit tests for fileinput.input()"""
596
597 def test_state_is_not_None_and_state_file_is_not_None(self):
598 """Tests invoking fileinput.input() when fileinput._state is not None
599 and its _file attribute is also not None. Expect RuntimeError to
600 be raised with a meaningful error message and for fileinput._state
601 to *not* be modified."""
602 instance = MockFileInput()
603 instance._file = object()
604 fileinput._state = instance
605 with self.assertRaises(RuntimeError) as cm:
606 fileinput.input()
607 self.assertEqual(("input() already active",), cm.exception.args)
608 self.assertIs(instance, fileinput._state, "fileinput._state")
609
610 def test_state_is_not_None_and_state_file_is_None(self):
611 """Tests invoking fileinput.input() when fileinput._state is not None
612 but its _file attribute *is* None. Expect it to create and return
613 a new fileinput.FileInput object with all method parameters passed
614 explicitly to the __init__() method; also ensure that
615 fileinput._state is set to the returned instance."""
616 instance = MockFileInput()
617 instance._file = None
618 fileinput._state = instance
619 self.do_test_call_input()
620
621 def test_state_is_None(self):
622 """Tests invoking fileinput.input() when fileinput._state is None
623 Expect it to create and return a new fileinput.FileInput object
624 with all method parameters passed explicitly to the __init__()
625 method; also ensure that fileinput._state is set to the returned
626 instance."""
627 fileinput._state = None
628 self.do_test_call_input()
629
630 def do_test_call_input(self):
631 """Tests that fileinput.input() creates a new fileinput.FileInput
632 object, passing the given parameters unmodified to
633 fileinput.FileInput.__init__(). Note that this test depends on the
634 monkey patching of fileinput.FileInput done by setUp()."""
635 files = object()
636 inplace = object()
637 backup = object()
briancurtin906f0c42011-03-15 10:29:41 -0400638 mode = object()
639 openhook = object()
640
641 # call fileinput.input() with different values for each argument
642 result = fileinput.input(files=files, inplace=inplace, backup=backup,
briancurtin906f0c42011-03-15 10:29:41 -0400643 mode=mode, openhook=openhook)
644
645 # ensure fileinput._state was set to the returned object
646 self.assertIs(result, fileinput._state, "fileinput._state")
647
648 # ensure the parameters to fileinput.input() were passed directly
649 # to FileInput.__init__()
650 self.assertIs(files, result.files, "files")
651 self.assertIs(inplace, result.inplace, "inplace")
652 self.assertIs(backup, result.backup, "backup")
briancurtin906f0c42011-03-15 10:29:41 -0400653 self.assertIs(mode, result.mode, "mode")
654 self.assertIs(openhook, result.openhook, "openhook")
655
656class Test_fileinput_close(BaseFileInputGlobalMethodsTest):
657 """Unit tests for fileinput.close()"""
658
659 def test_state_is_None(self):
660 """Tests that fileinput.close() does nothing if fileinput._state
661 is None"""
662 fileinput._state = None
663 fileinput.close()
664 self.assertIsNone(fileinput._state)
665
666 def test_state_is_not_None(self):
667 """Tests that fileinput.close() invokes close() on fileinput._state
668 and sets _state=None"""
669 instance = MockFileInput()
670 fileinput._state = instance
671 fileinput.close()
672 self.assertExactlyOneInvocation(instance, "close")
673 self.assertIsNone(fileinput._state)
674
675class Test_fileinput_nextfile(BaseFileInputGlobalMethodsTest):
676 """Unit tests for fileinput.nextfile()"""
677
678 def test_state_is_None(self):
679 """Tests fileinput.nextfile() when fileinput._state is None.
680 Ensure that it raises RuntimeError with a meaningful error message
681 and does not modify fileinput._state"""
682 fileinput._state = None
683 with self.assertRaises(RuntimeError) as cm:
684 fileinput.nextfile()
685 self.assertEqual(("no active input()",), cm.exception.args)
686 self.assertIsNone(fileinput._state)
687
688 def test_state_is_not_None(self):
689 """Tests fileinput.nextfile() when fileinput._state is not None.
690 Ensure that it invokes fileinput._state.nextfile() exactly once,
691 returns whatever it returns, and does not modify fileinput._state
692 to point to a different object."""
693 nextfile_retval = object()
694 instance = MockFileInput()
695 instance.return_values["nextfile"] = nextfile_retval
696 fileinput._state = instance
697 retval = fileinput.nextfile()
698 self.assertExactlyOneInvocation(instance, "nextfile")
699 self.assertIs(retval, nextfile_retval)
700 self.assertIs(fileinput._state, instance)
701
702class Test_fileinput_filename(BaseFileInputGlobalMethodsTest):
703 """Unit tests for fileinput.filename()"""
704
705 def test_state_is_None(self):
706 """Tests fileinput.filename() when fileinput._state is None.
707 Ensure that it raises RuntimeError with a meaningful error message
708 and does not modify fileinput._state"""
709 fileinput._state = None
710 with self.assertRaises(RuntimeError) as cm:
711 fileinput.filename()
712 self.assertEqual(("no active input()",), cm.exception.args)
713 self.assertIsNone(fileinput._state)
714
715 def test_state_is_not_None(self):
716 """Tests fileinput.filename() when fileinput._state is not None.
717 Ensure that it invokes fileinput._state.filename() exactly once,
718 returns whatever it returns, and does not modify fileinput._state
719 to point to a different object."""
720 filename_retval = object()
721 instance = MockFileInput()
722 instance.return_values["filename"] = filename_retval
723 fileinput._state = instance
724 retval = fileinput.filename()
725 self.assertExactlyOneInvocation(instance, "filename")
726 self.assertIs(retval, filename_retval)
727 self.assertIs(fileinput._state, instance)
728
729class Test_fileinput_lineno(BaseFileInputGlobalMethodsTest):
730 """Unit tests for fileinput.lineno()"""
731
732 def test_state_is_None(self):
733 """Tests fileinput.lineno() when fileinput._state is None.
734 Ensure that it raises RuntimeError with a meaningful error message
735 and does not modify fileinput._state"""
736 fileinput._state = None
737 with self.assertRaises(RuntimeError) as cm:
738 fileinput.lineno()
739 self.assertEqual(("no active input()",), cm.exception.args)
740 self.assertIsNone(fileinput._state)
741
742 def test_state_is_not_None(self):
743 """Tests fileinput.lineno() when fileinput._state is not None.
744 Ensure that it invokes fileinput._state.lineno() exactly once,
745 returns whatever it returns, and does not modify fileinput._state
746 to point to a different object."""
747 lineno_retval = object()
748 instance = MockFileInput()
749 instance.return_values["lineno"] = lineno_retval
750 fileinput._state = instance
751 retval = fileinput.lineno()
752 self.assertExactlyOneInvocation(instance, "lineno")
753 self.assertIs(retval, lineno_retval)
754 self.assertIs(fileinput._state, instance)
755
756class Test_fileinput_filelineno(BaseFileInputGlobalMethodsTest):
757 """Unit tests for fileinput.filelineno()"""
758
759 def test_state_is_None(self):
760 """Tests fileinput.filelineno() when fileinput._state is None.
761 Ensure that it raises RuntimeError with a meaningful error message
762 and does not modify fileinput._state"""
763 fileinput._state = None
764 with self.assertRaises(RuntimeError) as cm:
765 fileinput.filelineno()
766 self.assertEqual(("no active input()",), cm.exception.args)
767 self.assertIsNone(fileinput._state)
768
769 def test_state_is_not_None(self):
770 """Tests fileinput.filelineno() when fileinput._state is not None.
771 Ensure that it invokes fileinput._state.filelineno() exactly once,
772 returns whatever it returns, and does not modify fileinput._state
773 to point to a different object."""
774 filelineno_retval = object()
775 instance = MockFileInput()
776 instance.return_values["filelineno"] = filelineno_retval
777 fileinput._state = instance
778 retval = fileinput.filelineno()
779 self.assertExactlyOneInvocation(instance, "filelineno")
780 self.assertIs(retval, filelineno_retval)
781 self.assertIs(fileinput._state, instance)
782
783class Test_fileinput_fileno(BaseFileInputGlobalMethodsTest):
784 """Unit tests for fileinput.fileno()"""
785
786 def test_state_is_None(self):
787 """Tests fileinput.fileno() when fileinput._state is None.
788 Ensure that it raises RuntimeError with a meaningful error message
789 and does not modify fileinput._state"""
790 fileinput._state = None
791 with self.assertRaises(RuntimeError) as cm:
792 fileinput.fileno()
793 self.assertEqual(("no active input()",), cm.exception.args)
794 self.assertIsNone(fileinput._state)
795
796 def test_state_is_not_None(self):
797 """Tests fileinput.fileno() when fileinput._state is not None.
798 Ensure that it invokes fileinput._state.fileno() exactly once,
799 returns whatever it returns, and does not modify fileinput._state
800 to point to a different object."""
801 fileno_retval = object()
802 instance = MockFileInput()
803 instance.return_values["fileno"] = fileno_retval
804 instance.fileno_retval = fileno_retval
805 fileinput._state = instance
806 retval = fileinput.fileno()
807 self.assertExactlyOneInvocation(instance, "fileno")
808 self.assertIs(retval, fileno_retval)
809 self.assertIs(fileinput._state, instance)
810
811class Test_fileinput_isfirstline(BaseFileInputGlobalMethodsTest):
812 """Unit tests for fileinput.isfirstline()"""
813
814 def test_state_is_None(self):
815 """Tests fileinput.isfirstline() when fileinput._state is None.
816 Ensure that it raises RuntimeError with a meaningful error message
817 and does not modify fileinput._state"""
818 fileinput._state = None
819 with self.assertRaises(RuntimeError) as cm:
820 fileinput.isfirstline()
821 self.assertEqual(("no active input()",), cm.exception.args)
822 self.assertIsNone(fileinput._state)
823
824 def test_state_is_not_None(self):
825 """Tests fileinput.isfirstline() when fileinput._state is not None.
826 Ensure that it invokes fileinput._state.isfirstline() exactly once,
827 returns whatever it returns, and does not modify fileinput._state
828 to point to a different object."""
829 isfirstline_retval = object()
830 instance = MockFileInput()
831 instance.return_values["isfirstline"] = isfirstline_retval
832 fileinput._state = instance
833 retval = fileinput.isfirstline()
834 self.assertExactlyOneInvocation(instance, "isfirstline")
835 self.assertIs(retval, isfirstline_retval)
836 self.assertIs(fileinput._state, instance)
837
838class Test_fileinput_isstdin(BaseFileInputGlobalMethodsTest):
839 """Unit tests for fileinput.isstdin()"""
840
841 def test_state_is_None(self):
842 """Tests fileinput.isstdin() when fileinput._state is None.
843 Ensure that it raises RuntimeError with a meaningful error message
844 and does not modify fileinput._state"""
845 fileinput._state = None
846 with self.assertRaises(RuntimeError) as cm:
847 fileinput.isstdin()
848 self.assertEqual(("no active input()",), cm.exception.args)
849 self.assertIsNone(fileinput._state)
850
851 def test_state_is_not_None(self):
852 """Tests fileinput.isstdin() when fileinput._state is not None.
853 Ensure that it invokes fileinput._state.isstdin() exactly once,
854 returns whatever it returns, and does not modify fileinput._state
855 to point to a different object."""
856 isstdin_retval = object()
857 instance = MockFileInput()
858 instance.return_values["isstdin"] = isstdin_retval
859 fileinput._state = instance
860 retval = fileinput.isstdin()
861 self.assertExactlyOneInvocation(instance, "isstdin")
862 self.assertIs(retval, isstdin_retval)
863 self.assertIs(fileinput._state, instance)
864
865class InvocationRecorder:
866 def __init__(self):
867 self.invocation_count = 0
868 def __call__(self, *args, **kwargs):
869 self.invocation_count += 1
870 self.last_invocation = (args, kwargs)
871
872class Test_hook_compressed(unittest.TestCase):
873 """Unit tests for fileinput.hook_compressed()"""
874
875 def setUp(self):
876 self.fake_open = InvocationRecorder()
877
878 def test_empty_string(self):
879 self.do_test_use_builtin_open("", 1)
880
881 def test_no_ext(self):
882 self.do_test_use_builtin_open("abcd", 2)
883
Ezio Melottic3afbb92011-05-14 10:10:53 +0300884 @unittest.skipUnless(gzip, "Requires gzip and zlib")
briancurtin5eb35912011-03-15 10:59:36 -0400885 def test_gz_ext_fake(self):
briancurtin906f0c42011-03-15 10:29:41 -0400886 original_open = gzip.open
887 gzip.open = self.fake_open
888 try:
889 result = fileinput.hook_compressed("test.gz", 3)
890 finally:
891 gzip.open = original_open
892
893 self.assertEqual(self.fake_open.invocation_count, 1)
894 self.assertEqual(self.fake_open.last_invocation, (("test.gz", 3), {}))
895
briancurtinf84f3c32011-03-18 13:03:17 -0500896 @unittest.skipUnless(bz2, "Requires bz2")
briancurtin5eb35912011-03-15 10:59:36 -0400897 def test_bz2_ext_fake(self):
briancurtin906f0c42011-03-15 10:29:41 -0400898 original_open = bz2.BZ2File
899 bz2.BZ2File = self.fake_open
900 try:
901 result = fileinput.hook_compressed("test.bz2", 4)
902 finally:
903 bz2.BZ2File = original_open
904
905 self.assertEqual(self.fake_open.invocation_count, 1)
906 self.assertEqual(self.fake_open.last_invocation, (("test.bz2", 4), {}))
907
908 def test_blah_ext(self):
909 self.do_test_use_builtin_open("abcd.blah", 5)
910
briancurtin5eb35912011-03-15 10:59:36 -0400911 def test_gz_ext_builtin(self):
briancurtin906f0c42011-03-15 10:29:41 -0400912 self.do_test_use_builtin_open("abcd.Gz", 6)
913
briancurtin5eb35912011-03-15 10:59:36 -0400914 def test_bz2_ext_builtin(self):
briancurtin906f0c42011-03-15 10:29:41 -0400915 self.do_test_use_builtin_open("abcd.Bz2", 7)
916
917 def do_test_use_builtin_open(self, filename, mode):
918 original_open = self.replace_builtin_open(self.fake_open)
919 try:
920 result = fileinput.hook_compressed(filename, mode)
921 finally:
922 self.replace_builtin_open(original_open)
923
924 self.assertEqual(self.fake_open.invocation_count, 1)
925 self.assertEqual(self.fake_open.last_invocation,
926 ((filename, mode), {}))
927
928 @staticmethod
929 def replace_builtin_open(new_open_func):
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100930 original_open = builtins.open
931 builtins.open = new_open_func
briancurtin906f0c42011-03-15 10:29:41 -0400932 return original_open
933
934class Test_hook_encoded(unittest.TestCase):
935 """Unit tests for fileinput.hook_encoded()"""
936
937 def test(self):
938 encoding = object()
Serhiy Storchakab2752102016-04-27 23:13:46 +0300939 errors = object()
940 result = fileinput.hook_encoded(encoding, errors=errors)
briancurtin906f0c42011-03-15 10:29:41 -0400941
942 fake_open = InvocationRecorder()
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100943 original_open = builtins.open
944 builtins.open = fake_open
briancurtin906f0c42011-03-15 10:29:41 -0400945 try:
946 filename = object()
947 mode = object()
948 open_result = result(filename, mode)
949 finally:
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100950 builtins.open = original_open
briancurtin906f0c42011-03-15 10:29:41 -0400951
952 self.assertEqual(fake_open.invocation_count, 1)
953
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100954 args, kwargs = fake_open.last_invocation
briancurtin906f0c42011-03-15 10:29:41 -0400955 self.assertIs(args[0], filename)
956 self.assertIs(args[1], mode)
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100957 self.assertIs(kwargs.pop('encoding'), encoding)
Serhiy Storchakab2752102016-04-27 23:13:46 +0300958 self.assertIs(kwargs.pop('errors'), errors)
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100959 self.assertFalse(kwargs)
Georg Brandl6cb7b652010-07-31 20:08:15 +0000960
Serhiy Storchakab2752102016-04-27 23:13:46 +0300961 def test_errors(self):
962 with open(TESTFN, 'wb') as f:
963 f.write(b'\x80abc')
964 self.addCleanup(safe_unlink, TESTFN)
965
966 def check(errors, expected_lines):
967 with FileInput(files=TESTFN, mode='r',
968 openhook=hook_encoded('utf-8', errors=errors)) as fi:
969 lines = list(fi)
970 self.assertEqual(lines, expected_lines)
971
972 check('ignore', ['abc'])
973 with self.assertRaises(UnicodeDecodeError):
974 check('strict', ['abc'])
975 check('replace', ['\ufffdabc'])
976 check('backslashreplace', ['\\x80abc'])
977
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200978 def test_modes(self):
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200979 with open(TESTFN, 'wb') as f:
Serhiy Storchaka682ea5f2014-03-03 21:17:17 +0200980 # UTF-7 is a convenient, seldom used encoding
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200981 f.write(b'A\nB\r\nC\rD+IKw-')
982 self.addCleanup(safe_unlink, TESTFN)
983
984 def check(mode, expected_lines):
985 with FileInput(files=TESTFN, mode=mode,
986 openhook=hook_encoded('utf-7')) as fi:
987 lines = list(fi)
988 self.assertEqual(lines, expected_lines)
989
990 check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
Victor Stinner942f7a22020-03-04 18:50:22 +0100991 with self.assertWarns(DeprecationWarning):
992 check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
993 with self.assertWarns(DeprecationWarning):
994 check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200995 with self.assertRaises(ValueError):
996 check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
997
Guido van Rossumd8faa362007-04-27 19:54:29 +0000998
Martin Panter7978e102016-01-16 06:26:54 +0000999class MiscTest(unittest.TestCase):
1000
1001 def test_all(self):
Serhiy Storchaka674e2d02016-03-08 18:35:19 +02001002 support.check__all__(self, fileinput)
Martin Panter7978e102016-01-16 06:26:54 +00001003
1004
Guido van Rossumd8faa362007-04-27 19:54:29 +00001005if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -04001006 unittest.main()