10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi'''
20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiTests for fileinput module.
30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiNick Mathewson
40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi'''
50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport unittest
70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test.test_support import verbose, TESTFN, run_unittest
80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test.test_support import unlink as safe_unlink
90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport sys, re
100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom StringIO import StringIO
110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom fileinput import FileInput, hook_encoded
120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# The fileinput module has 2 interfaces: the FileInput class which does
140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# all the work, and a few functions (input, etc.) that use a global _state
150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# variable.  We only test the FileInput class, since the other functions
160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# only provide a thin facade over FileInput.
170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Write lines (a list of lines) to temp file number i, and return the
190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# temp file's name.
200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef writeTmp(i, lines, mode='w'):  # opening in text mode is the default
210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    name = TESTFN + str(i)
220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    f = open(name, mode)
230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    f.writelines(lines)
240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    f.close()
250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return name
260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef remove_tempfiles(*names):
280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    for name in names:
290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        safe_unlink(name)
300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass BufferSizesTests(unittest.TestCase):
320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_buffer_sizes(self):
330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # First, run the tests with default and teeny buffer size.
340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for round, bs in (0, 0), (1, 30):
350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            try:
360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                self.buffer_size_test(t1, t2, t3, t4, bs, round)
410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            finally:
420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                remove_tempfiles(t1, t2, t3, t4)
430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        start = 1 + round*6
480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if verbose:
490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            print '%s. Simple iteration (bs=%s)' % (start+0, bs)
500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        lines = list(fi)
520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi.close()
530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(len(lines), 31)
540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(lines[4], 'Line 5 of file 1\n')
550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(lines[30], 'Line 1 of file 4\n')
560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.lineno(), 31)
570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.filename(), t4)
580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if verbose:
600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            print '%s. Status variables (bs=%s)' % (start+1, bs)
610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        s = "x"
630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        while s and s != 'Line 6 of file 2\n':
640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            s = fi.readline()
650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.filename(), t2)
660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.lineno(), 21)
670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.filelineno(), 6)
680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertFalse(fi.isfirstline())
690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertFalse(fi.isstdin())
700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if verbose:
720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            print '%s. Nextfile (bs=%s)' % (start+2, bs)
730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi.nextfile()
740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.lineno(), 22)
760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi.close()
770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if verbose:
790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            print '%s. Stdin (bs=%s)' % (start+3, bs)
800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        savestdin = sys.stdin
820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            lines = list(fi)
850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(len(lines), 33)
860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(lines[32], 'Line 2 of stdin\n')
870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.filename(), '<stdin>')
880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi.nextfile()
890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            sys.stdin = savestdin
910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if verbose:
930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            print '%s. Boundary conditions (bs=%s)' % (start+4, bs)
940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.lineno(), 0)
960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.filename(), None)
970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi.nextfile()
980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.lineno(), 0)
990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(fi.filename(), None)
1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if verbose:
1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            print '%s. Inplace (bs=%s)' % (start+5, bs)
1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        savestdout = sys.stdout
1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            for line in fi:
1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                line = line[:-1].upper()
1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                print line
1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi.close()
1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
1110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            sys.stdout = savestdout
1120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
1140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for line in fi:
1150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(line[-1], '\n')
1160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            m = pat.match(line[:-1])
1170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertNotEqual(m, None)
1180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(int(m.group(1)), fi.filelineno())
1190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        fi.close()
1200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass FileInputTests(unittest.TestCase):
1220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_zero_byte_files(self):
1230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
1240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t1 = writeTmp(1, [""])
1250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t2 = writeTmp(2, [""])
1260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t3 = writeTmp(3, ["The only line there is.\n"])
1270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t4 = writeTmp(4, [""])
1280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(files=(t1, t2, t3, t4))
1290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            line = fi.readline()
1310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(line, 'The only line there is.\n')
1320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.lineno(), 1)
1330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.filelineno(), 1)
1340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.filename(), t3)
1350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            line = fi.readline()
1370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertFalse(line)
1380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.lineno(), 1)
1390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.filelineno(), 0)
1400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.filename(), t4)
1410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi.close()
1420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
1430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            remove_tempfiles(t1, t2, t3, t4)
1440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_files_that_dont_end_with_newline(self):
1460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
1470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t1 = writeTmp(1, ["A\nB\nC"])
1480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t2 = writeTmp(2, ["D\nE\nF"])
1490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(files=(t1, t2))
1500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            lines = list(fi)
1510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
1520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.filelineno(), 3)
1530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.lineno(), 6)
1540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
1550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            remove_tempfiles(t1, t2)
1560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_unicode_filenames(self):
1580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
1590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t1 = writeTmp(1, ["A\nB"])
1600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            encoding = sys.getfilesystemencoding()
1610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            if encoding is None:
1620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                encoding = 'ascii'
1630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(files=unicode(t1, encoding))
1640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            lines = list(fi)
1650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(lines, ["A\n", "B"])
1660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
1670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            remove_tempfiles(t1)
1680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_fileno(self):
1700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
1710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t1 = writeTmp(1, ["A\nB"])
1720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t2 = writeTmp(2, ["C\nD"])
1730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(files=(t1, t2))
1740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.fileno(), -1)
1750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            line = fi.next()
1760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertNotEqual(fi.fileno(), -1)
1770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi.nextfile()
1780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.fileno(), -1)
1790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            line = list(fi)
1800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(fi.fileno(), -1)
1810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
1820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            remove_tempfiles(t1, t2)
1830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_opening_mode(self):
1850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
1860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            # invalid mode, should raise ValueError
1870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(mode="w")
1880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.fail("FileInput should reject invalid mode argument")
1890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        except ValueError:
1900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
1910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
1920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            # try opening in universal newline mode
1930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t1 = writeTmp(1, ["A\nB\r\nC\rD"], mode="wb")
1940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(files=t1, mode="U")
1950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            lines = list(fi)
1960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
1970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
1980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            remove_tempfiles(t1)
1990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_file_opening_hook(self):
2010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
2020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            # cannot use openhook and inplace mode
2030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(inplace=1, openhook=lambda f,m: None)
2040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.fail("FileInput should raise if both inplace "
2050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                             "and openhook arguments are given")
2060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        except ValueError:
2070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
2080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
2090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(openhook=1)
2100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.fail("FileInput should check openhook for being callable")
2110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        except ValueError:
2120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
2130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        try:
2140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            t1 = writeTmp(1, ["A\nB"], mode="wb")
2150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            fi = FileInput(files=t1, openhook=hook_encoded("rot13"))
2160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            lines = list(fi)
2170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(lines, ["N\n", "O"])
2180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        finally:
2190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            remove_tempfiles(t1)
2200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef test_main():
2220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    run_unittest(BufferSizesTests, FileInputTests)
2230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == "__main__":
2250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    test_main()
226