diff options
Diffstat (limited to 'Lib/test/test_fileinput.py')
-rw-r--r-- | Lib/test/test_fileinput.py | 97 |
1 files changed, 67 insertions, 30 deletions
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index 84aed1ab0ca..f312882b2ab 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -4,10 +4,10 @@ Nick Mathewson ''' import unittest -from test.test_support import verbose, TESTFN, run_unittest -from test.test_support import unlink as safe_unlink +from test.support import verbose, TESTFN, run_unittest +from test.support import unlink as safe_unlink import sys, re -from StringIO import StringIO +from io import StringIO from fileinput import FileInput, hook_encoded # The fileinput module has 2 interfaces: the FileInput class which does @@ -20,18 +20,21 @@ from fileinput import FileInput, hook_encoded def writeTmp(i, lines, mode='w'): # opening in text mode is the default name = TESTFN + str(i) f = open(name, mode) - f.writelines(lines) + for line in lines: + f.write(line) f.close() return name def remove_tempfiles(*names): for name in names: - safe_unlink(name) + if name: + safe_unlink(name) class BufferSizesTests(unittest.TestCase): def test_buffer_sizes(self): # First, run the tests with default and teeny buffer size. for round, bs in (0, 0), (1, 30): + t1 = t2 = t3 = t4 = None try: t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)]) t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)]) @@ -46,7 +49,7 @@ class BufferSizesTests(unittest.TestCase): start = 1 + round*6 if verbose: - print '%s. Simple iteration (bs=%s)' % (start+0, bs) + print('%s. Simple iteration (bs=%s)' % (start+0, bs)) fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) lines = list(fi) fi.close() @@ -57,7 +60,7 @@ class BufferSizesTests(unittest.TestCase): self.assertEqual(fi.filename(), t4) if verbose: - print '%s. Status variables (bs=%s)' % (start+1, bs) + print('%s. Status variables (bs=%s)' % (start+1, bs)) fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) s = "x" while s and s != 'Line 6 of file 2\n': @@ -69,14 +72,14 @@ class BufferSizesTests(unittest.TestCase): self.assertFalse(fi.isstdin()) if verbose: - print '%s. Nextfile (bs=%s)' % (start+2, bs) + print('%s. Nextfile (bs=%s)' % (start+2, bs)) fi.nextfile() self.assertEqual(fi.readline(), 'Line 1 of file 3\n') self.assertEqual(fi.lineno(), 22) fi.close() if verbose: - print '%s. Stdin (bs=%s)' % (start+3, bs) + print('%s. Stdin (bs=%s)' % (start+3, bs)) fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs) savestdin = sys.stdin try: @@ -90,7 +93,7 @@ class BufferSizesTests(unittest.TestCase): sys.stdin = savestdin if verbose: - print '%s. Boundary conditions (bs=%s)' % (start+4, bs) + print('%s. Boundary conditions (bs=%s)' % (start+4, bs)) fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) self.assertEqual(fi.lineno(), 0) self.assertEqual(fi.filename(), None) @@ -99,13 +102,13 @@ class BufferSizesTests(unittest.TestCase): self.assertEqual(fi.filename(), None) if verbose: - print '%s. Inplace (bs=%s)' % (start+5, bs) + print('%s. Inplace (bs=%s)' % (start+5, bs)) savestdout = sys.stdout try: fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs) for line in fi: line = line[:-1].upper() - print line + print(line) fi.close() finally: sys.stdout = savestdout @@ -120,6 +123,7 @@ class BufferSizesTests(unittest.TestCase): class FileInputTests(unittest.TestCase): def test_zero_byte_files(self): + t1 = t2 = t3 = t4 = None try: t1 = writeTmp(1, [""]) t2 = writeTmp(2, [""]) @@ -143,6 +147,7 @@ class FileInputTests(unittest.TestCase): remove_tempfiles(t1, t2, t3, t4) def test_files_that_dont_end_with_newline(self): + t1 = t2 = None try: t1 = writeTmp(1, ["A\nB\nC"]) t2 = writeTmp(2, ["D\nE\nF"]) @@ -154,25 +159,28 @@ class FileInputTests(unittest.TestCase): finally: remove_tempfiles(t1, t2) - def test_unicode_filenames(self): - try: - t1 = writeTmp(1, ["A\nB"]) - encoding = sys.getfilesystemencoding() - if encoding is None: - encoding = 'ascii' - fi = FileInput(files=unicode(t1, encoding)) - lines = list(fi) - self.assertEqual(lines, ["A\n", "B"]) - finally: - remove_tempfiles(t1) +## def test_unicode_filenames(self): +## # XXX A unicode string is always returned by writeTmp. +## # So is this needed? +## try: +## t1 = writeTmp(1, ["A\nB"]) +## encoding = sys.getfilesystemencoding() +## if encoding is None: +## encoding = 'ascii' +## fi = FileInput(files=str(t1, encoding)) +## lines = list(fi) +## self.assertEqual(lines, ["A\n", "B"]) +## finally: +## remove_tempfiles(t1) def test_fileno(self): + t1 = t2 = None try: t1 = writeTmp(1, ["A\nB"]) t2 = writeTmp(2, ["C\nD"]) fi = FileInput(files=(t1, t2)) self.assertEqual(fi.fileno(), -1) - line = fi.next() + line =next( fi) self.assertNotEqual(fi.fileno(), -1) fi.nextfile() self.assertEqual(fi.fileno(), -1) @@ -188,9 +196,10 @@ class FileInputTests(unittest.TestCase): self.fail("FileInput should reject invalid mode argument") except ValueError: pass + t1 = None try: # try opening in universal newline mode - t1 = writeTmp(1, ["A\nB\r\nC\rD"], mode="wb") + t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb") fi = FileInput(files=t1, mode="U") lines = list(fi) self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"]) @@ -200,7 +209,7 @@ class FileInputTests(unittest.TestCase): def test_file_opening_hook(self): try: # cannot use openhook and inplace mode - fi = FileInput(inplace=1, openhook=lambda f,m: None) + fi = FileInput(inplace=1, openhook=lambda f, m: None) self.fail("FileInput should raise if both inplace " "and openhook arguments are given") except ValueError: @@ -210,14 +219,42 @@ class FileInputTests(unittest.TestCase): self.fail("FileInput should check openhook for being callable") except ValueError: pass + # XXX The rot13 codec was removed. + # So this test needs to be changed to use something else. + # (Or perhaps the API needs to change so we can just pass + # an encoding rather than using a hook?) +## try: +## t1 = writeTmp(1, ["A\nB"], mode="wb") +## fi = FileInput(files=t1, openhook=hook_encoded("rot13")) +## lines = list(fi) +## self.assertEqual(lines, ["N\n", "O"]) +## finally: +## remove_tempfiles(t1) + + def test_context_manager(self): try: - t1 = writeTmp(1, ["A\nB"], mode="wb") - fi = FileInput(files=t1, openhook=hook_encoded("rot13")) - lines = list(fi) - self.assertEqual(lines, ["N\n", "O"]) + t1 = writeTmp(1, ["A\nB\nC"]) + t2 = writeTmp(2, ["D\nE\nF"]) + with FileInput(files=(t1, t2)) as fi: + lines = list(fi) + self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"]) + self.assertEqual(fi.filelineno(), 3) + self.assertEqual(fi.lineno(), 6) + self.assertEqual(fi._files, ()) + finally: + remove_tempfiles(t1, t2) + + def test_close_on_exception(self): + try: + t1 = writeTmp(1, [""]) + with FileInput(files=t1) as fi: + raise IOError + except IOError: + self.assertEqual(fi._files, ()) finally: remove_tempfiles(t1) + def test_main(): run_unittest(BufferSizesTests, FileInputTests) |