Debian Codegraph @

Lib/test/test_fileinput.py

raw
1 '''
2 Tests for fileinput module.
3 Nick Mathewson
4 '''
5 import io
6 import os
7 import sys
8 import re
9 import fileinput
10 import collections
11 import builtins
12 import tempfile
13 import unittest
14
15 try:
16 import bz2
17 except ImportError:
18 bz2 = None
19 try:
20 import gzip
21 except ImportError:
22 gzip = None
23
24 from io import BytesIO, StringIO
25 from fileinput import FileInput, hook_encoded
26
27 from test.support import verbose
28 from test.support.os_helper import TESTFN, FakePath
29 from test.support.os_helper import unlink as safe_unlink
30 from test.support import os_helper
31 from test import support
32 from unittest import mock
33
34
35 # The fileinput module has 2 interfaces: the FileInput class which does
36 # all the work, and a few functions (input, etc.) that use a global _state
37 # variable.
38
39 class BaseTests:
40 # Write a content (str or bytes) to temp file, and return the
41 # temp file's name.
42 def writeTmp(self, content, *, mode='w'): # opening in text mode is the default
43 fd, name = tempfile.mkstemp()
44 self.addCleanup(os_helper.unlink, name)
45 encoding = None if "b" in mode else "utf-8"
46 with open(fd, mode, encoding=encoding) as f:
47 f.write(content)
48 return name
49
50 class 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
86 class BufferSizesTests(BaseTests, unittest.TestCase):
87 def test_buffer_sizes(self):
88
89 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
94 pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
95
96 if verbose:
97 print('1. Simple iteration')
98 fi = FileInput(files=(t1, t2, t3, t4), encoding="utf-8")
99 lines = list(fi)
100 fi.close()
101 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)
106
107 if verbose:
108 print('2. Status variables')
109 fi = FileInput(files=(t1, t2, t3, t4), encoding="utf-8")
110 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)
116 self.assertFalse(fi.isfirstline())
117 self.assertFalse(fi.isstdin())
118
119 if verbose:
120 print('3. Nextfile')
121 fi.nextfile()
122 self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
123 self.assertEqual(fi.lineno(), 22)
124 fi.close()
125
126 if verbose:
127 print('4. Stdin')
128 fi = FileInput(files=(t1, t2, t3, t4, '-'), encoding="utf-8")
129 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
139
140 if verbose:
141 print('5. Boundary conditions')
142 fi = FileInput(files=(t1, t2, t3, t4), encoding="utf-8")
143 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)
148
149 if verbose:
150 print('6. Inplace')
151 savestdout = sys.stdout
152 try:
153 fi = FileInput(files=(t1, t2, t3, t4), inplace=True, encoding="utf-8")
154 for line in fi:
155 line = line[:-1].upper()
156 print(line)
157 fi.close()
158 finally:
159 sys.stdout = savestdout
160
161 fi = FileInput(files=(t1, t2, t3, t4), encoding="utf-8")
162 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()
168
169 class 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
177 class FileInputTests(BaseTests, unittest.TestCase):
178
179 def test_zero_byte_files(self):
180 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), encoding="utf-8")
185
186 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)
191
192 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()
198
199 def test_files_that_dont_end_with_newline(self):
200 t1 = self.writeTmp("A\nB\nC")
201 t2 = self.writeTmp("D\nE\nF")
202 fi = FileInput(files=(t1, t2), encoding="utf-8")
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)
207
208 ## def test_unicode_filenames(self):
209 ## # XXX A unicode string is always returned by writeTmp.
210 ## # So is this needed?
211 ## t1 = self.writeTmp("A\nB")
212 ## encoding = sys.getfilesystemencoding()
213 ## if encoding is None:
214 ## encoding = 'ascii'
215 ## fi = FileInput(files=str(t1, encoding), encoding="utf-8")
216 ## lines = list(fi)
217 ## self.assertEqual(lines, ["A\n", "B"])
218
219 def test_fileno(self):
220 t1 = self.writeTmp("A\nB")
221 t2 = self.writeTmp("C\nD")
222 fi = FileInput(files=(t1, t2), encoding="utf-8")
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)
230
231 def test_invalid_opening_mode(self):
232 for mode in ('w', 'rU', 'U'):
233 with self.subTest(mode=mode):
234 with self.assertRaises(ValueError):
235 FileInput(mode=mode)
236
237 def test_stdin_binary_mode(self):
238 with mock.patch('sys.stdin') as m_stdin:
239 m_stdin.buffer = BytesIO(b'spam, bacon, sausage, and spam')
240 fi = FileInput(files=['-'], mode='rb')
241 lines = list(fi)
242 self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
243
244 def test_detached_stdin_binary_mode(self):
245 orig_stdin = sys.stdin
246 try:
247 sys.stdin = BytesIO(b'spam, bacon, sausage, and spam')
248 self.assertNotHasAttr(sys.stdin, 'buffer')
249 fi = FileInput(files=['-'], mode='rb')
250 lines = list(fi)
251 self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
252 finally:
253 sys.stdin = orig_stdin
254
255 def test_file_opening_hook(self):
256 try:
257 # cannot use openhook and inplace mode
258 fi = FileInput(inplace=True, openhook=lambda f, m: None)
259 self.fail("FileInput should raise if both inplace "
260 "and openhook arguments are given")
261 except ValueError:
262 pass
263 try:
264 fi = FileInput(openhook=1)
265 self.fail("FileInput should check openhook for being callable")
266 except ValueError:
267 pass
268
269 class CustomOpenHook:
270 def __init__(self):
271 self.invoked = False
272 def __call__(self, *args, **kargs):
273 self.invoked = True
274 return open(*args, encoding="utf-8")
275
276 t = self.writeTmp("\n")
277 custom_open_hook = CustomOpenHook()
278 with FileInput([t], openhook=custom_open_hook) as fi:
279 fi.readline()
280 self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
281
282 def test_readline(self):
283 with open(TESTFN, 'wb') as f:
284 f.write(b'A\nB\r\nC\r')
285 # Fill TextIOWrapper buffer.
286 f.write(b'123456789\n' * 1000)
287 # Issue #20501: readline() shouldn't read whole file.
288 f.write(b'\x80')
289 self.addCleanup(safe_unlink, TESTFN)
290
291 with FileInput(files=TESTFN,
292 openhook=hook_encoded('ascii')) as fi:
293 try:
294 self.assertEqual(fi.readline(), 'A\n')
295 self.assertEqual(fi.readline(), 'B\n')
296 self.assertEqual(fi.readline(), 'C\n')
297 except UnicodeDecodeError:
298 self.fail('Read to end of file')
299 with self.assertRaises(UnicodeDecodeError):
300 # Read to the end of file.
301 list(fi)
302 self.assertEqual(fi.readline(), '')
303 self.assertEqual(fi.readline(), '')
304
305 def test_readline_binary_mode(self):
306 with open(TESTFN, 'wb') as f:
307 f.write(b'A\nB\r\nC\rD')
308 self.addCleanup(safe_unlink, TESTFN)
309
310 with FileInput(files=TESTFN, mode='rb') as fi:
311 self.assertEqual(fi.readline(), b'A\n')
312 self.assertEqual(fi.readline(), b'B\r\n')
313 self.assertEqual(fi.readline(), b'C\rD')
314 # Read to the end of file.
315 self.assertEqual(fi.readline(), b'')
316 self.assertEqual(fi.readline(), b'')
317
318 def test_inplace_binary_write_mode(self):
319 temp_file = self.writeTmp(b'Initial text.', mode='wb')
320 with FileInput(temp_file, mode='rb', inplace=True) as fobj:
321 line = fobj.readline()
322 self.assertEqual(line, b'Initial text.')
323 # print() cannot be used with files opened in binary mode.
324 sys.stdout.write(b'New line.')
325 with open(temp_file, 'rb') as f:
326 self.assertEqual(f.read(), b'New line.')
327
328 def test_inplace_encoding_errors(self):
329 temp_file = self.writeTmp(b'Initial text \x88', mode='wb')
330 with FileInput(temp_file, inplace=True,
331 encoding="ascii", errors="replace") as fobj:
332 line = fobj.readline()
333 self.assertEqual(line, 'Initial text \ufffd')
334 print("New line \x88")
335 with open(temp_file, 'rb') as f:
336 self.assertEqual(f.read().rstrip(b'\r\n'), b'New line ?')
337
338 def test_file_hook_backward_compatibility(self):
339 def old_hook(filename, mode):
340 return io.StringIO("I used to receive only filename and mode")
341 t = self.writeTmp("\n")
342 with FileInput([t], openhook=old_hook) as fi:
343 result = fi.readline()
344 self.assertEqual(result, "I used to receive only filename and mode")
345
346 def test_context_manager(self):
347 t1 = self.writeTmp("A\nB\nC")
348 t2 = self.writeTmp("D\nE\nF")
349 with FileInput(files=(t1, t2), encoding="utf-8") as fi:
350 lines = list(fi)
351 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
352 self.assertEqual(fi.filelineno(), 3)
353 self.assertEqual(fi.lineno(), 6)
354 self.assertEqual(fi._files, ())
355
356 def test_close_on_exception(self):
357 t1 = self.writeTmp("")
358 try:
359 with FileInput(files=t1, encoding="utf-8") as fi:
360 raise OSError
361 except OSError:
362 self.assertEqual(fi._files, ())
363
364 def test_empty_files_list_specified_to_constructor(self):
365 with FileInput(files=[], encoding="utf-8") as fi:
366 self.assertEqual(fi._files, ('-',))
367
368 def test_nextfile_oserror_deleting_backup(self):
369 """Tests invoking FileInput.nextfile() when the attempt to delete
370 the backup file would raise OSError. This error is expected to be
371 silently ignored"""
372
373 os_unlink_orig = os.unlink
374 os_unlink_replacement = UnconditionallyRaise(OSError)
375 try:
376 t = self.writeTmp("\n")
377 self.addCleanup(safe_unlink, t + '.bak')
378 with FileInput(files=[t], inplace=True, encoding="utf-8") as fi:
379 next(fi) # make sure the file is opened
380 os.unlink = os_unlink_replacement
381 fi.nextfile()
382 finally:
383 os.unlink = os_unlink_orig
384
385 # sanity check to make sure that our test scenario was actually hit
386 self.assertTrue(os_unlink_replacement.invoked,
387 "os.unlink() was not invoked")
388
389 def test_readline_os_fstat_raises_OSError(self):
390 """Tests invoking FileInput.readline() when os.fstat() raises OSError.
391 This exception should be silently discarded."""
392
393 os_fstat_orig = os.fstat
394 os_fstat_replacement = UnconditionallyRaise(OSError)
395 try:
396 t = self.writeTmp("\n")
397 with FileInput(files=[t], inplace=True, encoding="utf-8") as fi:
398 os.fstat = os_fstat_replacement
399 fi.readline()
400 finally:
401 os.fstat = os_fstat_orig
402
403 # sanity check to make sure that our test scenario was actually hit
404 self.assertTrue(os_fstat_replacement.invoked,
405 "os.fstat() was not invoked")
406
407 def test_readline_os_chmod_raises_OSError(self):
408 """Tests invoking FileInput.readline() when os.chmod() raises OSError.
409 This exception should be silently discarded."""
410
411 os_chmod_orig = os.chmod
412 os_chmod_replacement = UnconditionallyRaise(OSError)
413 try:
414 t = self.writeTmp("\n")
415 with FileInput(files=[t], inplace=True, encoding="utf-8") as fi:
416 os.chmod = os_chmod_replacement
417 fi.readline()
418 finally:
419 os.chmod = os_chmod_orig
420
421 # sanity check to make sure that our test scenario was actually hit
422 self.assertTrue(os_chmod_replacement.invoked,
423 "os.fstat() was not invoked")
424
425 def test_fileno_when_ValueError_raised(self):
426 class FilenoRaisesValueError(UnconditionallyRaise):
427 def __init__(self):
428 UnconditionallyRaise.__init__(self, ValueError)
429 def fileno(self):
430 self.__call__()
431
432 unconditionally_raise_ValueError = FilenoRaisesValueError()
433 t = self.writeTmp("\n")
434 with FileInput(files=[t], encoding="utf-8") as fi:
435 file_backup = fi._file
436 try:
437 fi._file = unconditionally_raise_ValueError
438 result = fi.fileno()
439 finally:
440 fi._file = file_backup # make sure the file gets cleaned up
441
442 # sanity check to make sure that our test scenario was actually hit
443 self.assertTrue(unconditionally_raise_ValueError.invoked,
444 "_file.fileno() was not invoked")
445
446 self.assertEqual(result, -1, "fileno() should return -1")
447
448 def test_readline_buffering(self):
449 src = LineReader()
450 with FileInput(files=['line1\nline2', 'line3\n'],
451 openhook=src.openhook) as fi:
452 self.assertEqual(src.linesread, [])
453 self.assertEqual(fi.readline(), 'line1\n')
454 self.assertEqual(src.linesread, ['line1\n'])
455 self.assertEqual(fi.readline(), 'line2')
456 self.assertEqual(src.linesread, ['line2'])
457 self.assertEqual(fi.readline(), 'line3\n')
458 self.assertEqual(src.linesread, ['', 'line3\n'])
459 self.assertEqual(fi.readline(), '')
460 self.assertEqual(src.linesread, [''])
461 self.assertEqual(fi.readline(), '')
462 self.assertEqual(src.linesread, [])
463
464 def test_iteration_buffering(self):
465 src = LineReader()
466 with FileInput(files=['line1\nline2', 'line3\n'],
467 openhook=src.openhook) as fi:
468 self.assertEqual(src.linesread, [])
469 self.assertEqual(next(fi), 'line1\n')
470 self.assertEqual(src.linesread, ['line1\n'])
471 self.assertEqual(next(fi), 'line2')
472 self.assertEqual(src.linesread, ['line2'])
473 self.assertEqual(next(fi), 'line3\n')
474 self.assertEqual(src.linesread, ['', 'line3\n'])
475 self.assertRaises(StopIteration, next, fi)
476 self.assertEqual(src.linesread, [''])
477 self.assertRaises(StopIteration, next, fi)
478 self.assertEqual(src.linesread, [])
479
480 def test_pathlike_file(self):
481 t1 = FakePath(self.writeTmp("Path-like file."))
482 with FileInput(t1, encoding="utf-8") as fi:
483 line = fi.readline()
484 self.assertEqual(line, 'Path-like file.')
485 self.assertEqual(fi.lineno(), 1)
486 self.assertEqual(fi.filelineno(), 1)
487 self.assertEqual(fi.filename(), os.fspath(t1))
488
489 def test_pathlike_file_inplace(self):
490 t1 = FakePath(self.writeTmp('Path-like file.'))
491 with FileInput(t1, inplace=True, encoding="utf-8") as fi:
492 line = fi.readline()
493 self.assertEqual(line, 'Path-like file.')
494 print('Modified %s' % line)
495 with open(t1, encoding="utf-8") as f:
496 self.assertEqual(f.read(), 'Modified Path-like file.\n')
497
498
499 class MockFileInput:
500 """A class that mocks out fileinput.FileInput for use during unit tests"""
501
502 def __init__(self, files=None, inplace=False, backup="", *,
503 mode="r", openhook=None, encoding=None, errors=None):
504 self.files = files
505 self.inplace = inplace
506 self.backup = backup
507 self.mode = mode
508 self.openhook = openhook
509 self.encoding = encoding
510 self.errors = errors
511 self._file = None
512 self.invocation_counts = collections.defaultdict(lambda: 0)
513 self.return_values = {}
514
515 def close(self):
516 self.invocation_counts["close"] += 1
517
518 def nextfile(self):
519 self.invocation_counts["nextfile"] += 1
520 return self.return_values["nextfile"]
521
522 def filename(self):
523 self.invocation_counts["filename"] += 1
524 return self.return_values["filename"]
525
526 def lineno(self):
527 self.invocation_counts["lineno"] += 1
528 return self.return_values["lineno"]
529
530 def filelineno(self):
531 self.invocation_counts["filelineno"] += 1
532 return self.return_values["filelineno"]
533
534 def fileno(self):
535 self.invocation_counts["fileno"] += 1
536 return self.return_values["fileno"]
537
538 def isfirstline(self):
539 self.invocation_counts["isfirstline"] += 1
540 return self.return_values["isfirstline"]
541
542 def isstdin(self):
543 self.invocation_counts["isstdin"] += 1
544 return self.return_values["isstdin"]
545
546 class BaseFileInputGlobalMethodsTest(unittest.TestCase):
547 """Base class for unit tests for the global function of
548 the fileinput module."""
549
550 def setUp(self):
551 self._orig_state = fileinput._state
552 self._orig_FileInput = fileinput.FileInput
553 fileinput.FileInput = MockFileInput
554
555 def tearDown(self):
556 fileinput.FileInput = self._orig_FileInput
557 fileinput._state = self._orig_state
558
559 def assertExactlyOneInvocation(self, mock_file_input, method_name):
560 # assert that the method with the given name was invoked once
561 actual_count = mock_file_input.invocation_counts[method_name]
562 self.assertEqual(actual_count, 1, method_name)
563 # assert that no other unexpected methods were invoked
564 actual_total_count = len(mock_file_input.invocation_counts)
565 self.assertEqual(actual_total_count, 1)
566
567 class Test_fileinput_input(BaseFileInputGlobalMethodsTest):
568 """Unit tests for fileinput.input()"""
569
570 def test_state_is_not_None_and_state_file_is_not_None(self):
571 """Tests invoking fileinput.input() when fileinput._state is not None
572 and its _file attribute is also not None. Expect RuntimeError to
573 be raised with a meaningful error message and for fileinput._state
574 to *not* be modified."""
575 instance = MockFileInput()
576 instance._file = object()
577 fileinput._state = instance
578 with self.assertRaises(RuntimeError) as cm:
579 fileinput.input()
580 self.assertEqual(("input() already active",), cm.exception.args)
581 self.assertIs(instance, fileinput._state, "fileinput._state")
582
583 def test_state_is_not_None_and_state_file_is_None(self):
584 """Tests invoking fileinput.input() when fileinput._state is not None
585 but its _file attribute *is* None. Expect it to create and return
586 a new fileinput.FileInput object with all method parameters passed
587 explicitly to the __init__() method; also ensure that
588 fileinput._state is set to the returned instance."""
589 instance = MockFileInput()
590 instance._file = None
591 fileinput._state = instance
592 self.do_test_call_input()
593
594 def test_state_is_None(self):
595 """Tests invoking fileinput.input() when fileinput._state is None
596 Expect it to create and return a new fileinput.FileInput object
597 with all method parameters passed explicitly to the __init__()
598 method; also ensure that fileinput._state is set to the returned
599 instance."""
600 fileinput._state = None
601 self.do_test_call_input()
602
603 def do_test_call_input(self):
604 """Tests that fileinput.input() creates a new fileinput.FileInput
605 object, passing the given parameters unmodified to
606 fileinput.FileInput.__init__(). Note that this test depends on the
607 monkey patching of fileinput.FileInput done by setUp()."""
608 files = object()
609 inplace = object()
610 backup = object()
611 mode = object()
612 openhook = object()
613 encoding = object()
614
615 # call fileinput.input() with different values for each argument
616 result = fileinput.input(files=files, inplace=inplace, backup=backup,
617 mode=mode, openhook=openhook, encoding=encoding)
618
619 # ensure fileinput._state was set to the returned object
620 self.assertIs(result, fileinput._state, "fileinput._state")
621
622 # ensure the parameters to fileinput.input() were passed directly
623 # to FileInput.__init__()
624 self.assertIs(files, result.files, "files")
625 self.assertIs(inplace, result.inplace, "inplace")
626 self.assertIs(backup, result.backup, "backup")
627 self.assertIs(mode, result.mode, "mode")
628 self.assertIs(openhook, result.openhook, "openhook")
629
630 class Test_fileinput_close(BaseFileInputGlobalMethodsTest):
631 """Unit tests for fileinput.close()"""
632
633 def test_state_is_None(self):
634 """Tests that fileinput.close() does nothing if fileinput._state
635 is None"""
636 fileinput._state = None
637 fileinput.close()
638 self.assertIsNone(fileinput._state)
639
640 def test_state_is_not_None(self):
641 """Tests that fileinput.close() invokes close() on fileinput._state
642 and sets _state=None"""
643 instance = MockFileInput()
644 fileinput._state = instance
645 fileinput.close()
646 self.assertExactlyOneInvocation(instance, "close")
647 self.assertIsNone(fileinput._state)
648
649 class Test_fileinput_nextfile(BaseFileInputGlobalMethodsTest):
650 """Unit tests for fileinput.nextfile()"""
651
652 def test_state_is_None(self):
653 """Tests fileinput.nextfile() when fileinput._state is None.
654 Ensure that it raises RuntimeError with a meaningful error message
655 and does not modify fileinput._state"""
656 fileinput._state = None
657 with self.assertRaises(RuntimeError) as cm:
658 fileinput.nextfile()
659 self.assertEqual(("no active input()",), cm.exception.args)
660 self.assertIsNone(fileinput._state)
661
662 def test_state_is_not_None(self):
663 """Tests fileinput.nextfile() when fileinput._state is not None.
664 Ensure that it invokes fileinput._state.nextfile() exactly once,
665 returns whatever it returns, and does not modify fileinput._state
666 to point to a different object."""
667 nextfile_retval = object()
668 instance = MockFileInput()
669 instance.return_values["nextfile"] = nextfile_retval
670 fileinput._state = instance
671 retval = fileinput.nextfile()
672 self.assertExactlyOneInvocation(instance, "nextfile")
673 self.assertIs(retval, nextfile_retval)
674 self.assertIs(fileinput._state, instance)
675
676 class Test_fileinput_filename(BaseFileInputGlobalMethodsTest):
677 """Unit tests for fileinput.filename()"""
678
679 def test_state_is_None(self):
680 """Tests fileinput.filename() when fileinput._state is None.
681 Ensure that it raises RuntimeError with a meaningful error message
682 and does not modify fileinput._state"""
683 fileinput._state = None
684 with self.assertRaises(RuntimeError) as cm:
685 fileinput.filename()
686 self.assertEqual(("no active input()",), cm.exception.args)
687 self.assertIsNone(fileinput._state)
688
689 def test_state_is_not_None(self):
690 """Tests fileinput.filename() when fileinput._state is not None.
691 Ensure that it invokes fileinput._state.filename() exactly once,
692 returns whatever it returns, and does not modify fileinput._state
693 to point to a different object."""
694 filename_retval = object()
695 instance = MockFileInput()
696 instance.return_values["filename"] = filename_retval
697 fileinput._state = instance
698 retval = fileinput.filename()
699 self.assertExactlyOneInvocation(instance, "filename")
700 self.assertIs(retval, filename_retval)
701 self.assertIs(fileinput._state, instance)
702
703 class Test_fileinput_lineno(BaseFileInputGlobalMethodsTest):
704 """Unit tests for fileinput.lineno()"""
705
706 def test_state_is_None(self):
707 """Tests fileinput.lineno() when fileinput._state is None.
708 Ensure that it raises RuntimeError with a meaningful error message
709 and does not modify fileinput._state"""
710 fileinput._state = None
711 with self.assertRaises(RuntimeError) as cm:
712 fileinput.lineno()
713 self.assertEqual(("no active input()",), cm.exception.args)
714 self.assertIsNone(fileinput._state)
715
716 def test_state_is_not_None(self):
717 """Tests fileinput.lineno() when fileinput._state is not None.
718 Ensure that it invokes fileinput._state.lineno() exactly once,
719 returns whatever it returns, and does not modify fileinput._state
720 to point to a different object."""
721 lineno_retval = object()
722 instance = MockFileInput()
723 instance.return_values["lineno"] = lineno_retval
724 fileinput._state = instance
725 retval = fileinput.lineno()
726 self.assertExactlyOneInvocation(instance, "lineno")
727 self.assertIs(retval, lineno_retval)
728 self.assertIs(fileinput._state, instance)
729
730 class Test_fileinput_filelineno(BaseFileInputGlobalMethodsTest):
731 """Unit tests for fileinput.filelineno()"""
732
733 def test_state_is_None(self):
734 """Tests fileinput.filelineno() when fileinput._state is None.
735 Ensure that it raises RuntimeError with a meaningful error message
736 and does not modify fileinput._state"""
737 fileinput._state = None
738 with self.assertRaises(RuntimeError) as cm:
739 fileinput.filelineno()
740 self.assertEqual(("no active input()",), cm.exception.args)
741 self.assertIsNone(fileinput._state)
742
743 def test_state_is_not_None(self):
744 """Tests fileinput.filelineno() when fileinput._state is not None.
745 Ensure that it invokes fileinput._state.filelineno() exactly once,
746 returns whatever it returns, and does not modify fileinput._state
747 to point to a different object."""
748 filelineno_retval = object()
749 instance = MockFileInput()
750 instance.return_values["filelineno"] = filelineno_retval
751 fileinput._state = instance
752 retval = fileinput.filelineno()
753 self.assertExactlyOneInvocation(instance, "filelineno")
754 self.assertIs(retval, filelineno_retval)
755 self.assertIs(fileinput._state, instance)
756
757 class Test_fileinput_fileno(BaseFileInputGlobalMethodsTest):
758 """Unit tests for fileinput.fileno()"""
759
760 def test_state_is_None(self):
761 """Tests fileinput.fileno() when fileinput._state is None.
762 Ensure that it raises RuntimeError with a meaningful error message
763 and does not modify fileinput._state"""
764 fileinput._state = None
765 with self.assertRaises(RuntimeError) as cm:
766 fileinput.fileno()
767 self.assertEqual(("no active input()",), cm.exception.args)
768 self.assertIsNone(fileinput._state)
769
770 def test_state_is_not_None(self):
771 """Tests fileinput.fileno() when fileinput._state is not None.
772 Ensure that it invokes fileinput._state.fileno() exactly once,
773 returns whatever it returns, and does not modify fileinput._state
774 to point to a different object."""
775 fileno_retval = object()
776 instance = MockFileInput()
777 instance.return_values["fileno"] = fileno_retval
778 instance.fileno_retval = fileno_retval
779 fileinput._state = instance
780 retval = fileinput.fileno()
781 self.assertExactlyOneInvocation(instance, "fileno")
782 self.assertIs(retval, fileno_retval)
783 self.assertIs(fileinput._state, instance)
784
785 class Test_fileinput_isfirstline(BaseFileInputGlobalMethodsTest):
786 """Unit tests for fileinput.isfirstline()"""
787
788 def test_state_is_None(self):
789 """Tests fileinput.isfirstline() when fileinput._state is None.
790 Ensure that it raises RuntimeError with a meaningful error message
791 and does not modify fileinput._state"""
792 fileinput._state = None
793 with self.assertRaises(RuntimeError) as cm:
794 fileinput.isfirstline()
795 self.assertEqual(("no active input()",), cm.exception.args)
796 self.assertIsNone(fileinput._state)
797
798 def test_state_is_not_None(self):
799 """Tests fileinput.isfirstline() when fileinput._state is not None.
800 Ensure that it invokes fileinput._state.isfirstline() exactly once,
801 returns whatever it returns, and does not modify fileinput._state
802 to point to a different object."""
803 isfirstline_retval = object()
804 instance = MockFileInput()
805 instance.return_values["isfirstline"] = isfirstline_retval
806 fileinput._state = instance
807 retval = fileinput.isfirstline()
808 self.assertExactlyOneInvocation(instance, "isfirstline")
809 self.assertIs(retval, isfirstline_retval)
810 self.assertIs(fileinput._state, instance)
811
812 class Test_fileinput_isstdin(BaseFileInputGlobalMethodsTest):
813 """Unit tests for fileinput.isstdin()"""
814
815 def test_state_is_None(self):
816 """Tests fileinput.isstdin() when fileinput._state is None.
817 Ensure that it raises RuntimeError with a meaningful error message
818 and does not modify fileinput._state"""
819 fileinput._state = None
820 with self.assertRaises(RuntimeError) as cm:
821 fileinput.isstdin()
822 self.assertEqual(("no active input()",), cm.exception.args)
823 self.assertIsNone(fileinput._state)
824
825 def test_state_is_not_None(self):
826 """Tests fileinput.isstdin() when fileinput._state is not None.
827 Ensure that it invokes fileinput._state.isstdin() exactly once,
828 returns whatever it returns, and does not modify fileinput._state
829 to point to a different object."""
830 isstdin_retval = object()
831 instance = MockFileInput()
832 instance.return_values["isstdin"] = isstdin_retval
833 fileinput._state = instance
834 retval = fileinput.isstdin()
835 self.assertExactlyOneInvocation(instance, "isstdin")
836 self.assertIs(retval, isstdin_retval)
837 self.assertIs(fileinput._state, instance)
838
839 class InvocationRecorder:
840
841 def __init__(self):
842 self.invocation_count = 0
843
844 def __call__(self, *args, **kwargs):
845 self.invocation_count += 1
846 self.last_invocation = (args, kwargs)
847 return io.BytesIO(b'some bytes')
848
849
850 class Test_hook_compressed(unittest.TestCase):
851 """Unit tests for fileinput.hook_compressed()"""
852
853 def setUp(self):
854 self.fake_open = InvocationRecorder()
855
856 def test_empty_string(self):
857 self.do_test_use_builtin_open_text("", "r")
858
859 def test_no_ext(self):
860 self.do_test_use_builtin_open_text("abcd", "r")
861
862 @unittest.skipUnless(gzip, "Requires gzip and zlib")
863 def test_gz_ext_fake(self):
864 original_open = gzip.open
865 gzip.open = self.fake_open
866 try:
867 result = fileinput.hook_compressed("test.gz", "r")
868 finally:
869 gzip.open = original_open
870
871 self.assertEqual(self.fake_open.invocation_count, 1)
872 self.assertEqual(self.fake_open.last_invocation, (("test.gz", "r"), {}))
873
874 @unittest.skipUnless(gzip, "Requires gzip and zlib")
875 def test_gz_with_encoding_fake(self):
876 original_open = gzip.open
877 gzip.open = lambda filename, mode: io.BytesIO(b'Ex-binary string')
878 try:
879 result = fileinput.hook_compressed("test.gz", "r", encoding="utf-8")
880 finally:
881 gzip.open = original_open
882 self.assertEqual(list(result), ['Ex-binary string'])
883
884 @unittest.skipUnless(bz2, "Requires bz2")
885 def test_bz2_ext_fake(self):
886 original_open = bz2.BZ2File
887 bz2.BZ2File = self.fake_open
888 try:
889 result = fileinput.hook_compressed("test.bz2", "r")
890 finally:
891 bz2.BZ2File = original_open
892
893 self.assertEqual(self.fake_open.invocation_count, 1)
894 self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "r"), {}))
895
896 def test_blah_ext(self):
897 self.do_test_use_builtin_open_binary("abcd.blah", "rb")
898
899 def test_gz_ext_builtin(self):
900 self.do_test_use_builtin_open_binary("abcd.Gz", "rb")
901
902 def test_bz2_ext_builtin(self):
903 self.do_test_use_builtin_open_binary("abcd.Bz2", "rb")
904
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):
923 original_open = self.replace_builtin_open(self.fake_open)
924 try:
925 result = fileinput.hook_compressed(filename, mode)
926 finally:
927 self.replace_builtin_open(original_open)
928
929 self.assertEqual(self.fake_open.invocation_count, 1)
930 self.assertEqual(self.fake_open.last_invocation,
931 ((filename, mode), {'encoding': 'locale', 'errors': None}))
932
933 @staticmethod
934 def replace_builtin_open(new_open_func):
935 original_open = builtins.open
936 builtins.open = new_open_func
937 return original_open
938
939 class Test_hook_encoded(unittest.TestCase):
940 """Unit tests for fileinput.hook_encoded()"""
941
942 def test(self):
943 encoding = object()
944 errors = object()
945 result = fileinput.hook_encoded(encoding, errors=errors)
946
947 fake_open = InvocationRecorder()
948 original_open = builtins.open
949 builtins.open = fake_open
950 try:
951 filename = object()
952 mode = object()
953 open_result = result(filename, mode)
954 finally:
955 builtins.open = original_open
956
957 self.assertEqual(fake_open.invocation_count, 1)
958
959 args, kwargs = fake_open.last_invocation
960 self.assertIs(args[0], filename)
961 self.assertIs(args[1], mode)
962 self.assertIs(kwargs.pop('encoding'), encoding)
963 self.assertIs(kwargs.pop('errors'), errors)
964 self.assertFalse(kwargs)
965
966 def test_errors(self):
967 with open(TESTFN, 'wb') as f:
968 f.write(b'\x80abc')
969 self.addCleanup(safe_unlink, TESTFN)
970
971 def check(errors, expected_lines):
972 with FileInput(files=TESTFN, mode='r',
973 openhook=hook_encoded('utf-8', errors=errors)) as fi:
974 lines = list(fi)
975 self.assertEqual(lines, expected_lines)
976
977 check('ignore', ['abc'])
978 with self.assertRaises(UnicodeDecodeError):
979 check('strict', ['abc'])
980 check('replace', ['\ufffdabc'])
981 check('backslashreplace', ['\\x80abc'])
982
983 def test_modes(self):
984 with open(TESTFN, 'wb') as f:
985 # UTF-7 is a convenient, seldom used encoding
986 f.write(b'A\nB\r\nC\rD+IKw-')
987 self.addCleanup(safe_unlink, TESTFN)
988
989 def check(mode, expected_lines):
990 with FileInput(files=TESTFN, mode=mode,
991 openhook=hook_encoded('utf-7')) as fi:
992 lines = list(fi)
993 self.assertEqual(lines, expected_lines)
994
995 check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
996 with self.assertRaises(ValueError):
997 check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
998
999
1000 class MiscTest(unittest.TestCase):
1001
1002 def test_all(self):
1003 support.check__all__(self, fileinput)
1004
1005
1006 if __name__ == "__main__":
1007 unittest.main()