blob: 5df810c8f999b831491ed0c14006e8027a347502 [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
Benjamin Petersoneb462882011-03-15 09:50:18 -050011import unittest
12
briancurtinf84f3c32011-03-18 13:03:17 -050013try:
14 import bz2
15except ImportError:
16 bz2 = None
Ezio Melottic3afbb92011-05-14 10:10:53 +030017try:
18 import gzip
19except ImportError:
20 gzip = None
briancurtinf84f3c32011-03-18 13:03:17 -050021
Serhiy Storchaka946cfc32014-05-14 21:08:33 +030022from io import BytesIO, StringIO
Benjamin Petersoneb462882011-03-15 09:50:18 -050023from fileinput import FileInput, hook_encoded
Roy Williams002665a2017-05-22 22:24:17 -070024from pathlib import Path
Benjamin Petersoneb462882011-03-15 09:50:18 -050025
Serhiy Storchaka597d15a2016-04-24 13:45:58 +030026from test.support import verbose, TESTFN, check_warnings
Benjamin Petersoneb462882011-03-15 09:50:18 -050027from test.support import unlink as safe_unlink
Martin Panter7978e102016-01-16 06:26:54 +000028from test import support
Serhiy Storchaka946cfc32014-05-14 21:08:33 +030029from unittest import mock
Benjamin Petersoneb462882011-03-15 09:50:18 -050030
Tim Peters3230d5c2001-07-11 22:21:17 +000031
32# The fileinput module has 2 interfaces: the FileInput class which does
33# all the work, and a few functions (input, etc.) that use a global _state
briancurtin906f0c42011-03-15 10:29:41 -040034# variable.
Tim Peters3230d5c2001-07-11 22:21:17 +000035
36# Write lines (a list of lines) to temp file number i, and return the
37# temp file's name.
Tim Peters4d7cad12006-02-19 21:22:10 +000038def writeTmp(i, lines, mode='w'): # opening in text mode is the default
Tim Peters3230d5c2001-07-11 22:21:17 +000039 name = TESTFN + str(i)
Tim Peters4d7cad12006-02-19 21:22:10 +000040 f = open(name, mode)
Guido van Rossumc43e79f2007-06-18 18:26:36 +000041 for line in lines:
42 f.write(line)
Tim Peters3230d5c2001-07-11 22:21:17 +000043 f.close()
44 return name
45
Tim Peters3230d5c2001-07-11 22:21:17 +000046def remove_tempfiles(*names):
47 for name in names:
Guido van Rossume22905a2007-08-27 23:09:25 +000048 if name:
49 safe_unlink(name)
Tim Peters3230d5c2001-07-11 22:21:17 +000050
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +020051class LineReader:
52
53 def __init__(self):
54 self._linesread = []
55
56 @property
57 def linesread(self):
58 try:
59 return self._linesread[:]
60 finally:
61 self._linesread = []
62
63 def openhook(self, filename, mode):
64 self.it = iter(filename.splitlines(True))
65 return self
66
67 def readline(self, size=None):
68 line = next(self.it, '')
69 self._linesread.append(line)
70 return line
71
72 def readlines(self, hint=-1):
73 lines = []
74 size = 0
75 while True:
76 line = self.readline()
77 if not line:
78 return lines
79 lines.append(line)
80 size += len(line)
81 if size >= hint:
82 return lines
83
84 def close(self):
85 pass
86
Guido van Rossumd8faa362007-04-27 19:54:29 +000087class BufferSizesTests(unittest.TestCase):
88 def test_buffer_sizes(self):
89 # First, run the tests with default and teeny buffer size.
90 for round, bs in (0, 0), (1, 30):
Neal Norwitz2595e762008-03-24 06:10:13 +000091 t1 = t2 = t3 = t4 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +000092 try:
93 t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
94 t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
95 t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
96 t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
Serhiy Storchaka674e2d02016-03-08 18:35:19 +020097 if bs:
98 with self.assertWarns(DeprecationWarning):
99 self.buffer_size_test(t1, t2, t3, t4, bs, round)
100 else:
101 self.buffer_size_test(t1, t2, t3, t4, bs, round)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000102 finally:
103 remove_tempfiles(t1, t2, t3, t4)
Tim Peters3230d5c2001-07-11 22:21:17 +0000104
Guido van Rossumd8faa362007-04-27 19:54:29 +0000105 def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
106 pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
Tim Peters3230d5c2001-07-11 22:21:17 +0000107
Guido van Rossumd8faa362007-04-27 19:54:29 +0000108 start = 1 + round*6
109 if verbose:
110 print('%s. Simple iteration (bs=%s)' % (start+0, bs))
111 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
Tim Peters3230d5c2001-07-11 22:21:17 +0000112 lines = list(fi)
Tim Peters3230d5c2001-07-11 22:21:17 +0000113 fi.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000114 self.assertEqual(len(lines), 31)
115 self.assertEqual(lines[4], 'Line 5 of file 1\n')
116 self.assertEqual(lines[30], 'Line 1 of file 4\n')
117 self.assertEqual(fi.lineno(), 31)
118 self.assertEqual(fi.filename(), t4)
Tim Peters3230d5c2001-07-11 22:21:17 +0000119
Guido van Rossumd8faa362007-04-27 19:54:29 +0000120 if verbose:
121 print('%s. Status variables (bs=%s)' % (start+1, bs))
122 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
123 s = "x"
124 while s and s != 'Line 6 of file 2\n':
125 s = fi.readline()
126 self.assertEqual(fi.filename(), t2)
127 self.assertEqual(fi.lineno(), 21)
128 self.assertEqual(fi.filelineno(), 6)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000129 self.assertFalse(fi.isfirstline())
130 self.assertFalse(fi.isstdin())
Tim Peters3230d5c2001-07-11 22:21:17 +0000131
Guido van Rossumd8faa362007-04-27 19:54:29 +0000132 if verbose:
133 print('%s. Nextfile (bs=%s)' % (start+2, bs))
134 fi.nextfile()
135 self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
136 self.assertEqual(fi.lineno(), 22)
137 fi.close()
Tim Peters3230d5c2001-07-11 22:21:17 +0000138
Guido van Rossumd8faa362007-04-27 19:54:29 +0000139 if verbose:
140 print('%s. Stdin (bs=%s)' % (start+3, bs))
141 fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
142 savestdin = sys.stdin
143 try:
144 sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
145 lines = list(fi)
146 self.assertEqual(len(lines), 33)
147 self.assertEqual(lines[32], 'Line 2 of stdin\n')
148 self.assertEqual(fi.filename(), '<stdin>')
149 fi.nextfile()
150 finally:
151 sys.stdin = savestdin
Tim Peters3230d5c2001-07-11 22:21:17 +0000152
Guido van Rossumd8faa362007-04-27 19:54:29 +0000153 if verbose:
154 print('%s. Boundary conditions (bs=%s)' % (start+4, bs))
155 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
156 self.assertEqual(fi.lineno(), 0)
157 self.assertEqual(fi.filename(), None)
158 fi.nextfile()
159 self.assertEqual(fi.lineno(), 0)
160 self.assertEqual(fi.filename(), None)
Tim Peters3230d5c2001-07-11 22:21:17 +0000161
Guido van Rossumd8faa362007-04-27 19:54:29 +0000162 if verbose:
163 print('%s. Inplace (bs=%s)' % (start+5, bs))
164 savestdout = sys.stdout
165 try:
166 fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
167 for line in fi:
168 line = line[:-1].upper()
169 print(line)
170 fi.close()
171 finally:
172 sys.stdout = savestdout
Tim Peters3230d5c2001-07-11 22:21:17 +0000173
Guido van Rossumd8faa362007-04-27 19:54:29 +0000174 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
175 for line in fi:
176 self.assertEqual(line[-1], '\n')
177 m = pat.match(line[:-1])
178 self.assertNotEqual(m, None)
179 self.assertEqual(int(m.group(1)), fi.filelineno())
180 fi.close()
Georg Brandle4662172006-02-19 09:51:27 +0000181
briancurtin906f0c42011-03-15 10:29:41 -0400182class UnconditionallyRaise:
183 def __init__(self, exception_type):
184 self.exception_type = exception_type
185 self.invoked = False
186 def __call__(self, *args, **kwargs):
187 self.invoked = True
188 raise self.exception_type()
189
Guido van Rossumd8faa362007-04-27 19:54:29 +0000190class FileInputTests(unittest.TestCase):
briancurtin906f0c42011-03-15 10:29:41 -0400191
Guido van Rossumd8faa362007-04-27 19:54:29 +0000192 def test_zero_byte_files(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000193 t1 = t2 = t3 = t4 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000194 try:
195 t1 = writeTmp(1, [""])
196 t2 = writeTmp(2, [""])
197 t3 = writeTmp(3, ["The only line there is.\n"])
198 t4 = writeTmp(4, [""])
199 fi = FileInput(files=(t1, t2, t3, t4))
Georg Brandl67e9fb92006-02-19 13:56:17 +0000200
Guido van Rossumd8faa362007-04-27 19:54:29 +0000201 line = fi.readline()
202 self.assertEqual(line, 'The only line there is.\n')
203 self.assertEqual(fi.lineno(), 1)
204 self.assertEqual(fi.filelineno(), 1)
205 self.assertEqual(fi.filename(), t3)
Georg Brandlc029f872006-02-19 14:12:34 +0000206
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207 line = fi.readline()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000208 self.assertFalse(line)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000209 self.assertEqual(fi.lineno(), 1)
210 self.assertEqual(fi.filelineno(), 0)
211 self.assertEqual(fi.filename(), t4)
212 fi.close()
213 finally:
214 remove_tempfiles(t1, t2, t3, t4)
Georg Brandlc98eeed2006-02-19 14:57:47 +0000215
Guido van Rossumd8faa362007-04-27 19:54:29 +0000216 def test_files_that_dont_end_with_newline(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000217 t1 = t2 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000218 try:
219 t1 = writeTmp(1, ["A\nB\nC"])
220 t2 = writeTmp(2, ["D\nE\nF"])
221 fi = FileInput(files=(t1, t2))
222 lines = list(fi)
223 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
224 self.assertEqual(fi.filelineno(), 3)
225 self.assertEqual(fi.lineno(), 6)
226 finally:
227 remove_tempfiles(t1, t2)
228
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000229## def test_unicode_filenames(self):
230## # XXX A unicode string is always returned by writeTmp.
231## # So is this needed?
232## try:
233## t1 = writeTmp(1, ["A\nB"])
234## encoding = sys.getfilesystemencoding()
235## if encoding is None:
236## encoding = 'ascii'
237## fi = FileInput(files=str(t1, encoding))
238## lines = list(fi)
239## self.assertEqual(lines, ["A\n", "B"])
240## finally:
241## remove_tempfiles(t1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000242
243 def test_fileno(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000244 t1 = t2 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000245 try:
246 t1 = writeTmp(1, ["A\nB"])
247 t2 = writeTmp(2, ["C\nD"])
248 fi = FileInput(files=(t1, t2))
249 self.assertEqual(fi.fileno(), -1)
250 line =next( fi)
251 self.assertNotEqual(fi.fileno(), -1)
252 fi.nextfile()
253 self.assertEqual(fi.fileno(), -1)
254 line = list(fi)
255 self.assertEqual(fi.fileno(), -1)
256 finally:
257 remove_tempfiles(t1, t2)
258
259 def test_opening_mode(self):
260 try:
261 # invalid mode, should raise ValueError
262 fi = FileInput(mode="w")
263 self.fail("FileInput should reject invalid mode argument")
264 except ValueError:
265 pass
Guido van Rossume22905a2007-08-27 23:09:25 +0000266 t1 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000267 try:
268 # try opening in universal newline mode
Guido van Rossume22905a2007-08-27 23:09:25 +0000269 t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb")
Serhiy Storchaka2480c2e2013-11-24 23:13:26 +0200270 with check_warnings(('', DeprecationWarning)):
271 fi = FileInput(files=t1, mode="U")
272 with check_warnings(('', DeprecationWarning)):
273 lines = list(fi)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000274 self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
275 finally:
276 remove_tempfiles(t1)
277
Serhiy Storchaka946cfc32014-05-14 21:08:33 +0300278 def test_stdin_binary_mode(self):
279 with mock.patch('sys.stdin') as m_stdin:
280 m_stdin.buffer = BytesIO(b'spam, bacon, sausage, and spam')
281 fi = FileInput(files=['-'], mode='rb')
282 lines = list(fi)
283 self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
284
R David Murray830207e2016-01-02 15:41:41 -0500285 def test_detached_stdin_binary_mode(self):
286 orig_stdin = sys.stdin
287 try:
288 sys.stdin = BytesIO(b'spam, bacon, sausage, and spam')
289 self.assertFalse(hasattr(sys.stdin, 'buffer'))
290 fi = FileInput(files=['-'], mode='rb')
291 lines = list(fi)
292 self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
293 finally:
294 sys.stdin = orig_stdin
295
Guido van Rossume22905a2007-08-27 23:09:25 +0000296 def test_file_opening_hook(self):
297 try:
298 # cannot use openhook and inplace mode
299 fi = FileInput(inplace=1, openhook=lambda f, m: None)
300 self.fail("FileInput should raise if both inplace "
301 "and openhook arguments are given")
302 except ValueError:
303 pass
304 try:
305 fi = FileInput(openhook=1)
306 self.fail("FileInput should check openhook for being callable")
307 except ValueError:
308 pass
briancurtin906f0c42011-03-15 10:29:41 -0400309
310 class CustomOpenHook:
311 def __init__(self):
312 self.invoked = False
313 def __call__(self, *args):
314 self.invoked = True
315 return open(*args)
316
317 t = writeTmp(1, ["\n"])
318 self.addCleanup(remove_tempfiles, t)
319 custom_open_hook = CustomOpenHook()
320 with FileInput([t], openhook=custom_open_hook) as fi:
321 fi.readline()
322 self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000323
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200324 def test_readline(self):
325 with open(TESTFN, 'wb') as f:
326 f.write(b'A\nB\r\nC\r')
327 # Fill TextIOWrapper buffer.
328 f.write(b'123456789\n' * 1000)
329 # Issue #20501: readline() shouldn't read whole file.
330 f.write(b'\x80')
331 self.addCleanup(safe_unlink, TESTFN)
332
333 with FileInput(files=TESTFN,
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200334 openhook=hook_encoded('ascii')) as fi:
Serhiy Storchaka682ea5f2014-03-03 21:17:17 +0200335 try:
336 self.assertEqual(fi.readline(), 'A\n')
337 self.assertEqual(fi.readline(), 'B\n')
338 self.assertEqual(fi.readline(), 'C\n')
339 except UnicodeDecodeError:
340 self.fail('Read to end of file')
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200341 with self.assertRaises(UnicodeDecodeError):
342 # Read to the end of file.
343 list(fi)
Serhiy Storchaka314464d2015-11-01 16:43:58 +0200344 self.assertEqual(fi.readline(), '')
345 self.assertEqual(fi.readline(), '')
346
347 def test_readline_binary_mode(self):
348 with open(TESTFN, 'wb') as f:
349 f.write(b'A\nB\r\nC\rD')
350 self.addCleanup(safe_unlink, TESTFN)
351
352 with FileInput(files=TESTFN, mode='rb') as fi:
353 self.assertEqual(fi.readline(), b'A\n')
354 self.assertEqual(fi.readline(), b'B\r\n')
355 self.assertEqual(fi.readline(), b'C\rD')
356 # Read to the end of file.
357 self.assertEqual(fi.readline(), b'')
358 self.assertEqual(fi.readline(), b'')
Serhiy Storchaka517b7472014-02-26 20:59:43 +0200359
Georg Brandl6cb7b652010-07-31 20:08:15 +0000360 def test_context_manager(self):
361 try:
362 t1 = writeTmp(1, ["A\nB\nC"])
363 t2 = writeTmp(2, ["D\nE\nF"])
364 with FileInput(files=(t1, t2)) as fi:
365 lines = list(fi)
366 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
367 self.assertEqual(fi.filelineno(), 3)
368 self.assertEqual(fi.lineno(), 6)
369 self.assertEqual(fi._files, ())
370 finally:
371 remove_tempfiles(t1, t2)
372
373 def test_close_on_exception(self):
374 try:
375 t1 = writeTmp(1, [""])
376 with FileInput(files=t1) as fi:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200377 raise OSError
378 except OSError:
Georg Brandl6cb7b652010-07-31 20:08:15 +0000379 self.assertEqual(fi._files, ())
380 finally:
381 remove_tempfiles(t1)
382
briancurtin906f0c42011-03-15 10:29:41 -0400383 def test_empty_files_list_specified_to_constructor(self):
384 with FileInput(files=[]) as fi:
Brett Cannond47af532011-03-15 15:55:12 -0400385 self.assertEqual(fi._files, ('-',))
briancurtin906f0c42011-03-15 10:29:41 -0400386
387 def test__getitem__(self):
388 """Tests invoking FileInput.__getitem__() with the current
389 line number"""
390 t = writeTmp(1, ["line1\n", "line2\n"])
391 self.addCleanup(remove_tempfiles, t)
392 with FileInput(files=[t]) as fi:
393 retval1 = fi[0]
394 self.assertEqual(retval1, "line1\n")
395 retval2 = fi[1]
396 self.assertEqual(retval2, "line2\n")
397
398 def test__getitem__invalid_key(self):
399 """Tests invoking FileInput.__getitem__() with an index unequal to
400 the line number"""
401 t = writeTmp(1, ["line1\n", "line2\n"])
402 self.addCleanup(remove_tempfiles, t)
403 with FileInput(files=[t]) as fi:
404 with self.assertRaises(RuntimeError) as cm:
405 fi[1]
Brett Cannond47af532011-03-15 15:55:12 -0400406 self.assertEqual(cm.exception.args, ("accessing lines out of order",))
briancurtin906f0c42011-03-15 10:29:41 -0400407
408 def test__getitem__eof(self):
409 """Tests invoking FileInput.__getitem__() with the line number but at
410 end-of-input"""
411 t = writeTmp(1, [])
412 self.addCleanup(remove_tempfiles, t)
413 with FileInput(files=[t]) as fi:
414 with self.assertRaises(IndexError) as cm:
415 fi[0]
Brett Cannond47af532011-03-15 15:55:12 -0400416 self.assertEqual(cm.exception.args, ("end of input reached",))
briancurtin906f0c42011-03-15 10:29:41 -0400417
418 def test_nextfile_oserror_deleting_backup(self):
419 """Tests invoking FileInput.nextfile() when the attempt to delete
420 the backup file would raise OSError. This error is expected to be
421 silently ignored"""
422
423 os_unlink_orig = os.unlink
424 os_unlink_replacement = UnconditionallyRaise(OSError)
425 try:
426 t = writeTmp(1, ["\n"])
427 self.addCleanup(remove_tempfiles, t)
428 with FileInput(files=[t], inplace=True) as fi:
429 next(fi) # make sure the file is opened
430 os.unlink = os_unlink_replacement
431 fi.nextfile()
432 finally:
433 os.unlink = os_unlink_orig
434
435 # sanity check to make sure that our test scenario was actually hit
436 self.assertTrue(os_unlink_replacement.invoked,
437 "os.unlink() was not invoked")
438
439 def test_readline_os_fstat_raises_OSError(self):
440 """Tests invoking FileInput.readline() when os.fstat() raises OSError.
441 This exception should be silently discarded."""
442
443 os_fstat_orig = os.fstat
444 os_fstat_replacement = UnconditionallyRaise(OSError)
445 try:
446 t = writeTmp(1, ["\n"])
447 self.addCleanup(remove_tempfiles, t)
448 with FileInput(files=[t], inplace=True) as fi:
449 os.fstat = os_fstat_replacement
450 fi.readline()
451 finally:
452 os.fstat = os_fstat_orig
453
454 # sanity check to make sure that our test scenario was actually hit
455 self.assertTrue(os_fstat_replacement.invoked,
456 "os.fstat() was not invoked")
457
458 @unittest.skipIf(not hasattr(os, "chmod"), "os.chmod does not exist")
459 def test_readline_os_chmod_raises_OSError(self):
460 """Tests invoking FileInput.readline() when os.chmod() raises OSError.
461 This exception should be silently discarded."""
462
463 os_chmod_orig = os.chmod
464 os_chmod_replacement = UnconditionallyRaise(OSError)
465 try:
466 t = writeTmp(1, ["\n"])
467 self.addCleanup(remove_tempfiles, t)
468 with FileInput(files=[t], inplace=True) as fi:
469 os.chmod = os_chmod_replacement
470 fi.readline()
471 finally:
472 os.chmod = os_chmod_orig
473
474 # sanity check to make sure that our test scenario was actually hit
475 self.assertTrue(os_chmod_replacement.invoked,
476 "os.fstat() was not invoked")
477
478 def test_fileno_when_ValueError_raised(self):
479 class FilenoRaisesValueError(UnconditionallyRaise):
480 def __init__(self):
481 UnconditionallyRaise.__init__(self, ValueError)
482 def fileno(self):
483 self.__call__()
484
485 unconditionally_raise_ValueError = FilenoRaisesValueError()
486 t = writeTmp(1, ["\n"])
487 self.addCleanup(remove_tempfiles, t)
488 with FileInput(files=[t]) as fi:
489 file_backup = fi._file
490 try:
491 fi._file = unconditionally_raise_ValueError
492 result = fi.fileno()
493 finally:
494 fi._file = file_backup # make sure the file gets cleaned up
495
496 # sanity check to make sure that our test scenario was actually hit
497 self.assertTrue(unconditionally_raise_ValueError.invoked,
498 "_file.fileno() was not invoked")
499
500 self.assertEqual(result, -1, "fileno() should return -1")
501
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200502 def test_readline_buffering(self):
503 src = LineReader()
504 with FileInput(files=['line1\nline2', 'line3\n'],
505 openhook=src.openhook) as fi:
506 self.assertEqual(src.linesread, [])
507 self.assertEqual(fi.readline(), 'line1\n')
508 self.assertEqual(src.linesread, ['line1\n'])
509 self.assertEqual(fi.readline(), 'line2')
510 self.assertEqual(src.linesread, ['line2'])
511 self.assertEqual(fi.readline(), 'line3\n')
512 self.assertEqual(src.linesread, ['', 'line3\n'])
513 self.assertEqual(fi.readline(), '')
514 self.assertEqual(src.linesread, [''])
515 self.assertEqual(fi.readline(), '')
516 self.assertEqual(src.linesread, [])
517
518 def test_iteration_buffering(self):
519 src = LineReader()
520 with FileInput(files=['line1\nline2', 'line3\n'],
521 openhook=src.openhook) as fi:
522 self.assertEqual(src.linesread, [])
523 self.assertEqual(next(fi), 'line1\n')
524 self.assertEqual(src.linesread, ['line1\n'])
525 self.assertEqual(next(fi), 'line2')
526 self.assertEqual(src.linesread, ['line2'])
527 self.assertEqual(next(fi), 'line3\n')
528 self.assertEqual(src.linesread, ['', 'line3\n'])
529 self.assertRaises(StopIteration, next, fi)
530 self.assertEqual(src.linesread, [''])
531 self.assertRaises(StopIteration, next, fi)
532 self.assertEqual(src.linesread, [])
533
Roy Williams002665a2017-05-22 22:24:17 -0700534 def test_pathlib_file(self):
535 t1 = None
536 try:
537 t1 = Path(writeTmp(1, ["Pathlib file."]))
538 with FileInput(t1) as fi:
539 line = fi.readline()
540 self.assertEqual(line, 'Pathlib file.')
541 self.assertEqual(fi.lineno(), 1)
542 self.assertEqual(fi.filelineno(), 1)
543 self.assertEqual(fi.filename(), os.fspath(t1))
544 finally:
545 remove_tempfiles(t1)
546
547
briancurtin906f0c42011-03-15 10:29:41 -0400548class MockFileInput:
549 """A class that mocks out fileinput.FileInput for use during unit tests"""
550
551 def __init__(self, files=None, inplace=False, backup="", bufsize=0,
552 mode="r", openhook=None):
553 self.files = files
554 self.inplace = inplace
555 self.backup = backup
556 self.bufsize = bufsize
557 self.mode = mode
558 self.openhook = openhook
559 self._file = None
560 self.invocation_counts = collections.defaultdict(lambda: 0)
561 self.return_values = {}
562
563 def close(self):
564 self.invocation_counts["close"] += 1
565
566 def nextfile(self):
567 self.invocation_counts["nextfile"] += 1
568 return self.return_values["nextfile"]
569
570 def filename(self):
571 self.invocation_counts["filename"] += 1
572 return self.return_values["filename"]
573
574 def lineno(self):
575 self.invocation_counts["lineno"] += 1
576 return self.return_values["lineno"]
577
578 def filelineno(self):
579 self.invocation_counts["filelineno"] += 1
580 return self.return_values["filelineno"]
581
582 def fileno(self):
583 self.invocation_counts["fileno"] += 1
584 return self.return_values["fileno"]
585
586 def isfirstline(self):
587 self.invocation_counts["isfirstline"] += 1
588 return self.return_values["isfirstline"]
589
590 def isstdin(self):
591 self.invocation_counts["isstdin"] += 1
592 return self.return_values["isstdin"]
593
594class BaseFileInputGlobalMethodsTest(unittest.TestCase):
595 """Base class for unit tests for the global function of
596 the fileinput module."""
597
598 def setUp(self):
599 self._orig_state = fileinput._state
600 self._orig_FileInput = fileinput.FileInput
601 fileinput.FileInput = MockFileInput
602
603 def tearDown(self):
604 fileinput.FileInput = self._orig_FileInput
605 fileinput._state = self._orig_state
606
607 def assertExactlyOneInvocation(self, mock_file_input, method_name):
608 # assert that the method with the given name was invoked once
609 actual_count = mock_file_input.invocation_counts[method_name]
610 self.assertEqual(actual_count, 1, method_name)
611 # assert that no other unexpected methods were invoked
612 actual_total_count = len(mock_file_input.invocation_counts)
613 self.assertEqual(actual_total_count, 1)
614
615class Test_fileinput_input(BaseFileInputGlobalMethodsTest):
616 """Unit tests for fileinput.input()"""
617
618 def test_state_is_not_None_and_state_file_is_not_None(self):
619 """Tests invoking fileinput.input() when fileinput._state is not None
620 and its _file attribute is also not None. Expect RuntimeError to
621 be raised with a meaningful error message and for fileinput._state
622 to *not* be modified."""
623 instance = MockFileInput()
624 instance._file = object()
625 fileinput._state = instance
626 with self.assertRaises(RuntimeError) as cm:
627 fileinput.input()
628 self.assertEqual(("input() already active",), cm.exception.args)
629 self.assertIs(instance, fileinput._state, "fileinput._state")
630
631 def test_state_is_not_None_and_state_file_is_None(self):
632 """Tests invoking fileinput.input() when fileinput._state is not None
633 but its _file attribute *is* None. Expect it to create and return
634 a new fileinput.FileInput object with all method parameters passed
635 explicitly to the __init__() method; also ensure that
636 fileinput._state is set to the returned instance."""
637 instance = MockFileInput()
638 instance._file = None
639 fileinput._state = instance
640 self.do_test_call_input()
641
642 def test_state_is_None(self):
643 """Tests invoking fileinput.input() when fileinput._state is None
644 Expect it to create and return a new fileinput.FileInput object
645 with all method parameters passed explicitly to the __init__()
646 method; also ensure that fileinput._state is set to the returned
647 instance."""
648 fileinput._state = None
649 self.do_test_call_input()
650
651 def do_test_call_input(self):
652 """Tests that fileinput.input() creates a new fileinput.FileInput
653 object, passing the given parameters unmodified to
654 fileinput.FileInput.__init__(). Note that this test depends on the
655 monkey patching of fileinput.FileInput done by setUp()."""
656 files = object()
657 inplace = object()
658 backup = object()
659 bufsize = object()
660 mode = object()
661 openhook = object()
662
663 # call fileinput.input() with different values for each argument
664 result = fileinput.input(files=files, inplace=inplace, backup=backup,
665 bufsize=bufsize,
666 mode=mode, openhook=openhook)
667
668 # ensure fileinput._state was set to the returned object
669 self.assertIs(result, fileinput._state, "fileinput._state")
670
671 # ensure the parameters to fileinput.input() were passed directly
672 # to FileInput.__init__()
673 self.assertIs(files, result.files, "files")
674 self.assertIs(inplace, result.inplace, "inplace")
675 self.assertIs(backup, result.backup, "backup")
676 self.assertIs(bufsize, result.bufsize, "bufsize")
677 self.assertIs(mode, result.mode, "mode")
678 self.assertIs(openhook, result.openhook, "openhook")
679
680class Test_fileinput_close(BaseFileInputGlobalMethodsTest):
681 """Unit tests for fileinput.close()"""
682
683 def test_state_is_None(self):
684 """Tests that fileinput.close() does nothing if fileinput._state
685 is None"""
686 fileinput._state = None
687 fileinput.close()
688 self.assertIsNone(fileinput._state)
689
690 def test_state_is_not_None(self):
691 """Tests that fileinput.close() invokes close() on fileinput._state
692 and sets _state=None"""
693 instance = MockFileInput()
694 fileinput._state = instance
695 fileinput.close()
696 self.assertExactlyOneInvocation(instance, "close")
697 self.assertIsNone(fileinput._state)
698
699class Test_fileinput_nextfile(BaseFileInputGlobalMethodsTest):
700 """Unit tests for fileinput.nextfile()"""
701
702 def test_state_is_None(self):
703 """Tests fileinput.nextfile() when fileinput._state is None.
704 Ensure that it raises RuntimeError with a meaningful error message
705 and does not modify fileinput._state"""
706 fileinput._state = None
707 with self.assertRaises(RuntimeError) as cm:
708 fileinput.nextfile()
709 self.assertEqual(("no active input()",), cm.exception.args)
710 self.assertIsNone(fileinput._state)
711
712 def test_state_is_not_None(self):
713 """Tests fileinput.nextfile() when fileinput._state is not None.
714 Ensure that it invokes fileinput._state.nextfile() exactly once,
715 returns whatever it returns, and does not modify fileinput._state
716 to point to a different object."""
717 nextfile_retval = object()
718 instance = MockFileInput()
719 instance.return_values["nextfile"] = nextfile_retval
720 fileinput._state = instance
721 retval = fileinput.nextfile()
722 self.assertExactlyOneInvocation(instance, "nextfile")
723 self.assertIs(retval, nextfile_retval)
724 self.assertIs(fileinput._state, instance)
725
726class Test_fileinput_filename(BaseFileInputGlobalMethodsTest):
727 """Unit tests for fileinput.filename()"""
728
729 def test_state_is_None(self):
730 """Tests fileinput.filename() when fileinput._state is None.
731 Ensure that it raises RuntimeError with a meaningful error message
732 and does not modify fileinput._state"""
733 fileinput._state = None
734 with self.assertRaises(RuntimeError) as cm:
735 fileinput.filename()
736 self.assertEqual(("no active input()",), cm.exception.args)
737 self.assertIsNone(fileinput._state)
738
739 def test_state_is_not_None(self):
740 """Tests fileinput.filename() when fileinput._state is not None.
741 Ensure that it invokes fileinput._state.filename() exactly once,
742 returns whatever it returns, and does not modify fileinput._state
743 to point to a different object."""
744 filename_retval = object()
745 instance = MockFileInput()
746 instance.return_values["filename"] = filename_retval
747 fileinput._state = instance
748 retval = fileinput.filename()
749 self.assertExactlyOneInvocation(instance, "filename")
750 self.assertIs(retval, filename_retval)
751 self.assertIs(fileinput._state, instance)
752
753class Test_fileinput_lineno(BaseFileInputGlobalMethodsTest):
754 """Unit tests for fileinput.lineno()"""
755
756 def test_state_is_None(self):
757 """Tests fileinput.lineno() when fileinput._state is None.
758 Ensure that it raises RuntimeError with a meaningful error message
759 and does not modify fileinput._state"""
760 fileinput._state = None
761 with self.assertRaises(RuntimeError) as cm:
762 fileinput.lineno()
763 self.assertEqual(("no active input()",), cm.exception.args)
764 self.assertIsNone(fileinput._state)
765
766 def test_state_is_not_None(self):
767 """Tests fileinput.lineno() when fileinput._state is not None.
768 Ensure that it invokes fileinput._state.lineno() exactly once,
769 returns whatever it returns, and does not modify fileinput._state
770 to point to a different object."""
771 lineno_retval = object()
772 instance = MockFileInput()
773 instance.return_values["lineno"] = lineno_retval
774 fileinput._state = instance
775 retval = fileinput.lineno()
776 self.assertExactlyOneInvocation(instance, "lineno")
777 self.assertIs(retval, lineno_retval)
778 self.assertIs(fileinput._state, instance)
779
780class Test_fileinput_filelineno(BaseFileInputGlobalMethodsTest):
781 """Unit tests for fileinput.filelineno()"""
782
783 def test_state_is_None(self):
784 """Tests fileinput.filelineno() when fileinput._state is None.
785 Ensure that it raises RuntimeError with a meaningful error message
786 and does not modify fileinput._state"""
787 fileinput._state = None
788 with self.assertRaises(RuntimeError) as cm:
789 fileinput.filelineno()
790 self.assertEqual(("no active input()",), cm.exception.args)
791 self.assertIsNone(fileinput._state)
792
793 def test_state_is_not_None(self):
794 """Tests fileinput.filelineno() when fileinput._state is not None.
795 Ensure that it invokes fileinput._state.filelineno() exactly once,
796 returns whatever it returns, and does not modify fileinput._state
797 to point to a different object."""
798 filelineno_retval = object()
799 instance = MockFileInput()
800 instance.return_values["filelineno"] = filelineno_retval
801 fileinput._state = instance
802 retval = fileinput.filelineno()
803 self.assertExactlyOneInvocation(instance, "filelineno")
804 self.assertIs(retval, filelineno_retval)
805 self.assertIs(fileinput._state, instance)
806
807class Test_fileinput_fileno(BaseFileInputGlobalMethodsTest):
808 """Unit tests for fileinput.fileno()"""
809
810 def test_state_is_None(self):
811 """Tests fileinput.fileno() when fileinput._state is None.
812 Ensure that it raises RuntimeError with a meaningful error message
813 and does not modify fileinput._state"""
814 fileinput._state = None
815 with self.assertRaises(RuntimeError) as cm:
816 fileinput.fileno()
817 self.assertEqual(("no active input()",), cm.exception.args)
818 self.assertIsNone(fileinput._state)
819
820 def test_state_is_not_None(self):
821 """Tests fileinput.fileno() when fileinput._state is not None.
822 Ensure that it invokes fileinput._state.fileno() exactly once,
823 returns whatever it returns, and does not modify fileinput._state
824 to point to a different object."""
825 fileno_retval = object()
826 instance = MockFileInput()
827 instance.return_values["fileno"] = fileno_retval
828 instance.fileno_retval = fileno_retval
829 fileinput._state = instance
830 retval = fileinput.fileno()
831 self.assertExactlyOneInvocation(instance, "fileno")
832 self.assertIs(retval, fileno_retval)
833 self.assertIs(fileinput._state, instance)
834
835class Test_fileinput_isfirstline(BaseFileInputGlobalMethodsTest):
836 """Unit tests for fileinput.isfirstline()"""
837
838 def test_state_is_None(self):
839 """Tests fileinput.isfirstline() when fileinput._state is None.
840 Ensure that it raises RuntimeError with a meaningful error message
841 and does not modify fileinput._state"""
842 fileinput._state = None
843 with self.assertRaises(RuntimeError) as cm:
844 fileinput.isfirstline()
845 self.assertEqual(("no active input()",), cm.exception.args)
846 self.assertIsNone(fileinput._state)
847
848 def test_state_is_not_None(self):
849 """Tests fileinput.isfirstline() when fileinput._state is not None.
850 Ensure that it invokes fileinput._state.isfirstline() exactly once,
851 returns whatever it returns, and does not modify fileinput._state
852 to point to a different object."""
853 isfirstline_retval = object()
854 instance = MockFileInput()
855 instance.return_values["isfirstline"] = isfirstline_retval
856 fileinput._state = instance
857 retval = fileinput.isfirstline()
858 self.assertExactlyOneInvocation(instance, "isfirstline")
859 self.assertIs(retval, isfirstline_retval)
860 self.assertIs(fileinput._state, instance)
861
862class Test_fileinput_isstdin(BaseFileInputGlobalMethodsTest):
863 """Unit tests for fileinput.isstdin()"""
864
865 def test_state_is_None(self):
866 """Tests fileinput.isstdin() when fileinput._state is None.
867 Ensure that it raises RuntimeError with a meaningful error message
868 and does not modify fileinput._state"""
869 fileinput._state = None
870 with self.assertRaises(RuntimeError) as cm:
871 fileinput.isstdin()
872 self.assertEqual(("no active input()",), cm.exception.args)
873 self.assertIsNone(fileinput._state)
874
875 def test_state_is_not_None(self):
876 """Tests fileinput.isstdin() when fileinput._state is not None.
877 Ensure that it invokes fileinput._state.isstdin() exactly once,
878 returns whatever it returns, and does not modify fileinput._state
879 to point to a different object."""
880 isstdin_retval = object()
881 instance = MockFileInput()
882 instance.return_values["isstdin"] = isstdin_retval
883 fileinput._state = instance
884 retval = fileinput.isstdin()
885 self.assertExactlyOneInvocation(instance, "isstdin")
886 self.assertIs(retval, isstdin_retval)
887 self.assertIs(fileinput._state, instance)
888
889class InvocationRecorder:
890 def __init__(self):
891 self.invocation_count = 0
892 def __call__(self, *args, **kwargs):
893 self.invocation_count += 1
894 self.last_invocation = (args, kwargs)
895
896class Test_hook_compressed(unittest.TestCase):
897 """Unit tests for fileinput.hook_compressed()"""
898
899 def setUp(self):
900 self.fake_open = InvocationRecorder()
901
902 def test_empty_string(self):
903 self.do_test_use_builtin_open("", 1)
904
905 def test_no_ext(self):
906 self.do_test_use_builtin_open("abcd", 2)
907
Ezio Melottic3afbb92011-05-14 10:10:53 +0300908 @unittest.skipUnless(gzip, "Requires gzip and zlib")
briancurtin5eb35912011-03-15 10:59:36 -0400909 def test_gz_ext_fake(self):
briancurtin906f0c42011-03-15 10:29:41 -0400910 original_open = gzip.open
911 gzip.open = self.fake_open
912 try:
913 result = fileinput.hook_compressed("test.gz", 3)
914 finally:
915 gzip.open = original_open
916
917 self.assertEqual(self.fake_open.invocation_count, 1)
918 self.assertEqual(self.fake_open.last_invocation, (("test.gz", 3), {}))
919
briancurtinf84f3c32011-03-18 13:03:17 -0500920 @unittest.skipUnless(bz2, "Requires bz2")
briancurtin5eb35912011-03-15 10:59:36 -0400921 def test_bz2_ext_fake(self):
briancurtin906f0c42011-03-15 10:29:41 -0400922 original_open = bz2.BZ2File
923 bz2.BZ2File = self.fake_open
924 try:
925 result = fileinput.hook_compressed("test.bz2", 4)
926 finally:
927 bz2.BZ2File = original_open
928
929 self.assertEqual(self.fake_open.invocation_count, 1)
930 self.assertEqual(self.fake_open.last_invocation, (("test.bz2", 4), {}))
931
932 def test_blah_ext(self):
933 self.do_test_use_builtin_open("abcd.blah", 5)
934
briancurtin5eb35912011-03-15 10:59:36 -0400935 def test_gz_ext_builtin(self):
briancurtin906f0c42011-03-15 10:29:41 -0400936 self.do_test_use_builtin_open("abcd.Gz", 6)
937
briancurtin5eb35912011-03-15 10:59:36 -0400938 def test_bz2_ext_builtin(self):
briancurtin906f0c42011-03-15 10:29:41 -0400939 self.do_test_use_builtin_open("abcd.Bz2", 7)
940
941 def do_test_use_builtin_open(self, filename, mode):
942 original_open = self.replace_builtin_open(self.fake_open)
943 try:
944 result = fileinput.hook_compressed(filename, mode)
945 finally:
946 self.replace_builtin_open(original_open)
947
948 self.assertEqual(self.fake_open.invocation_count, 1)
949 self.assertEqual(self.fake_open.last_invocation,
950 ((filename, mode), {}))
951
952 @staticmethod
953 def replace_builtin_open(new_open_func):
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100954 original_open = builtins.open
955 builtins.open = new_open_func
briancurtin906f0c42011-03-15 10:29:41 -0400956 return original_open
957
958class Test_hook_encoded(unittest.TestCase):
959 """Unit tests for fileinput.hook_encoded()"""
960
961 def test(self):
962 encoding = object()
Serhiy Storchakab2752102016-04-27 23:13:46 +0300963 errors = object()
964 result = fileinput.hook_encoded(encoding, errors=errors)
briancurtin906f0c42011-03-15 10:29:41 -0400965
966 fake_open = InvocationRecorder()
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100967 original_open = builtins.open
968 builtins.open = fake_open
briancurtin906f0c42011-03-15 10:29:41 -0400969 try:
970 filename = object()
971 mode = object()
972 open_result = result(filename, mode)
973 finally:
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100974 builtins.open = original_open
briancurtin906f0c42011-03-15 10:29:41 -0400975
976 self.assertEqual(fake_open.invocation_count, 1)
977
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100978 args, kwargs = fake_open.last_invocation
briancurtin906f0c42011-03-15 10:29:41 -0400979 self.assertIs(args[0], filename)
980 self.assertIs(args[1], mode)
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100981 self.assertIs(kwargs.pop('encoding'), encoding)
Serhiy Storchakab2752102016-04-27 23:13:46 +0300982 self.assertIs(kwargs.pop('errors'), errors)
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100983 self.assertFalse(kwargs)
Georg Brandl6cb7b652010-07-31 20:08:15 +0000984
Serhiy Storchakab2752102016-04-27 23:13:46 +0300985 def test_errors(self):
986 with open(TESTFN, 'wb') as f:
987 f.write(b'\x80abc')
988 self.addCleanup(safe_unlink, TESTFN)
989
990 def check(errors, expected_lines):
991 with FileInput(files=TESTFN, mode='r',
992 openhook=hook_encoded('utf-8', errors=errors)) as fi:
993 lines = list(fi)
994 self.assertEqual(lines, expected_lines)
995
996 check('ignore', ['abc'])
997 with self.assertRaises(UnicodeDecodeError):
998 check('strict', ['abc'])
999 check('replace', ['\ufffdabc'])
1000 check('backslashreplace', ['\\x80abc'])
1001
Serhiy Storchaka517b7472014-02-26 20:59:43 +02001002 def test_modes(self):
Serhiy Storchaka517b7472014-02-26 20:59:43 +02001003 with open(TESTFN, 'wb') as f:
Serhiy Storchaka682ea5f2014-03-03 21:17:17 +02001004 # UTF-7 is a convenient, seldom used encoding
Serhiy Storchaka517b7472014-02-26 20:59:43 +02001005 f.write(b'A\nB\r\nC\rD+IKw-')
1006 self.addCleanup(safe_unlink, TESTFN)
1007
1008 def check(mode, expected_lines):
1009 with FileInput(files=TESTFN, mode=mode,
1010 openhook=hook_encoded('utf-7')) as fi:
1011 lines = list(fi)
1012 self.assertEqual(lines, expected_lines)
1013
1014 check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
Serhiy Storchaka9fff8492014-02-26 21:03:19 +02001015 with self.assertWarns(DeprecationWarning):
1016 check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
1017 with self.assertWarns(DeprecationWarning):
1018 check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
Serhiy Storchaka517b7472014-02-26 20:59:43 +02001019 with self.assertRaises(ValueError):
1020 check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
1021
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022
Martin Panter7978e102016-01-16 06:26:54 +00001023class MiscTest(unittest.TestCase):
1024
1025 def test_all(self):
Serhiy Storchaka674e2d02016-03-08 18:35:19 +02001026 support.check__all__(self, fileinput)
Martin Panter7978e102016-01-16 06:26:54 +00001027
1028
Guido van Rossumd8faa362007-04-27 19:54:29 +00001029if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -04001030 unittest.main()