blob: f312882b2abbe3fddae76b1bfcc104addb553728 [file] [log] [blame]
Tim Peters3230d5c2001-07-11 22:21:17 +00001'''
2Tests for fileinput module.
3Nick Mathewson
4'''
5
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test.support import verbose, TESTFN, run_unittest
8from test.support import unlink as safe_unlink
Christian Heimes05e8be12008-02-23 18:30:17 +00009import sys, re
Guido van Rossum34d19282007-08-09 01:03:29 +000010from io import StringIO
Georg Brandlc98eeed2006-02-19 14:57:47 +000011from fileinput import FileInput, hook_encoded
Tim Peters3230d5c2001-07-11 22:21:17 +000012
13# The fileinput module has 2 interfaces: the FileInput class which does
14# all the work, and a few functions (input, etc.) that use a global _state
15# variable. We only test the FileInput class, since the other functions
16# only provide a thin facade over FileInput.
17
18# Write lines (a list of lines) to temp file number i, and return the
19# temp file's name.
Tim Peters4d7cad12006-02-19 21:22:10 +000020def writeTmp(i, lines, mode='w'): # opening in text mode is the default
Tim Peters3230d5c2001-07-11 22:21:17 +000021 name = TESTFN + str(i)
Tim Peters4d7cad12006-02-19 21:22:10 +000022 f = open(name, mode)
Guido van Rossumc43e79f2007-06-18 18:26:36 +000023 for line in lines:
24 f.write(line)
Tim Peters3230d5c2001-07-11 22:21:17 +000025 f.close()
26 return name
27
Tim Peters3230d5c2001-07-11 22:21:17 +000028def remove_tempfiles(*names):
29 for name in names:
Guido van Rossume22905a2007-08-27 23:09:25 +000030 if name:
31 safe_unlink(name)
Tim Peters3230d5c2001-07-11 22:21:17 +000032
Guido van Rossumd8faa362007-04-27 19:54:29 +000033class BufferSizesTests(unittest.TestCase):
34 def test_buffer_sizes(self):
35 # First, run the tests with default and teeny buffer size.
36 for round, bs in (0, 0), (1, 30):
Neal Norwitz2595e762008-03-24 06:10:13 +000037 t1 = t2 = t3 = t4 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +000038 try:
39 t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
40 t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
41 t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
42 t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
43 self.buffer_size_test(t1, t2, t3, t4, bs, round)
44 finally:
45 remove_tempfiles(t1, t2, t3, t4)
Tim Peters3230d5c2001-07-11 22:21:17 +000046
Guido van Rossumd8faa362007-04-27 19:54:29 +000047 def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
48 pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
Tim Peters3230d5c2001-07-11 22:21:17 +000049
Guido van Rossumd8faa362007-04-27 19:54:29 +000050 start = 1 + round*6
51 if verbose:
52 print('%s. Simple iteration (bs=%s)' % (start+0, bs))
53 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
Tim Peters3230d5c2001-07-11 22:21:17 +000054 lines = list(fi)
Tim Peters3230d5c2001-07-11 22:21:17 +000055 fi.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +000056 self.assertEqual(len(lines), 31)
57 self.assertEqual(lines[4], 'Line 5 of file 1\n')
58 self.assertEqual(lines[30], 'Line 1 of file 4\n')
59 self.assertEqual(fi.lineno(), 31)
60 self.assertEqual(fi.filename(), t4)
Tim Peters3230d5c2001-07-11 22:21:17 +000061
Guido van Rossumd8faa362007-04-27 19:54:29 +000062 if verbose:
63 print('%s. Status variables (bs=%s)' % (start+1, bs))
64 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
65 s = "x"
66 while s and s != 'Line 6 of file 2\n':
67 s = fi.readline()
68 self.assertEqual(fi.filename(), t2)
69 self.assertEqual(fi.lineno(), 21)
70 self.assertEqual(fi.filelineno(), 6)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000071 self.assertFalse(fi.isfirstline())
72 self.assertFalse(fi.isstdin())
Tim Peters3230d5c2001-07-11 22:21:17 +000073
Guido van Rossumd8faa362007-04-27 19:54:29 +000074 if verbose:
75 print('%s. Nextfile (bs=%s)' % (start+2, bs))
76 fi.nextfile()
77 self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
78 self.assertEqual(fi.lineno(), 22)
79 fi.close()
Tim Peters3230d5c2001-07-11 22:21:17 +000080
Guido van Rossumd8faa362007-04-27 19:54:29 +000081 if verbose:
82 print('%s. Stdin (bs=%s)' % (start+3, bs))
83 fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
84 savestdin = sys.stdin
85 try:
86 sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
87 lines = list(fi)
88 self.assertEqual(len(lines), 33)
89 self.assertEqual(lines[32], 'Line 2 of stdin\n')
90 self.assertEqual(fi.filename(), '<stdin>')
91 fi.nextfile()
92 finally:
93 sys.stdin = savestdin
Tim Peters3230d5c2001-07-11 22:21:17 +000094
Guido van Rossumd8faa362007-04-27 19:54:29 +000095 if verbose:
96 print('%s. Boundary conditions (bs=%s)' % (start+4, bs))
97 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
98 self.assertEqual(fi.lineno(), 0)
99 self.assertEqual(fi.filename(), None)
100 fi.nextfile()
101 self.assertEqual(fi.lineno(), 0)
102 self.assertEqual(fi.filename(), None)
Tim Peters3230d5c2001-07-11 22:21:17 +0000103
Guido van Rossumd8faa362007-04-27 19:54:29 +0000104 if verbose:
105 print('%s. Inplace (bs=%s)' % (start+5, bs))
106 savestdout = sys.stdout
107 try:
108 fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
109 for line in fi:
110 line = line[:-1].upper()
111 print(line)
112 fi.close()
113 finally:
114 sys.stdout = savestdout
Tim Peters3230d5c2001-07-11 22:21:17 +0000115
Guido van Rossumd8faa362007-04-27 19:54:29 +0000116 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
117 for line in fi:
118 self.assertEqual(line[-1], '\n')
119 m = pat.match(line[:-1])
120 self.assertNotEqual(m, None)
121 self.assertEqual(int(m.group(1)), fi.filelineno())
122 fi.close()
Georg Brandle4662172006-02-19 09:51:27 +0000123
Guido van Rossumd8faa362007-04-27 19:54:29 +0000124class FileInputTests(unittest.TestCase):
125 def test_zero_byte_files(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000126 t1 = t2 = t3 = t4 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000127 try:
128 t1 = writeTmp(1, [""])
129 t2 = writeTmp(2, [""])
130 t3 = writeTmp(3, ["The only line there is.\n"])
131 t4 = writeTmp(4, [""])
132 fi = FileInput(files=(t1, t2, t3, t4))
Georg Brandl67e9fb92006-02-19 13:56:17 +0000133
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134 line = fi.readline()
135 self.assertEqual(line, 'The only line there is.\n')
136 self.assertEqual(fi.lineno(), 1)
137 self.assertEqual(fi.filelineno(), 1)
138 self.assertEqual(fi.filename(), t3)
Georg Brandlc029f872006-02-19 14:12:34 +0000139
Guido van Rossumd8faa362007-04-27 19:54:29 +0000140 line = fi.readline()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000141 self.assertFalse(line)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000142 self.assertEqual(fi.lineno(), 1)
143 self.assertEqual(fi.filelineno(), 0)
144 self.assertEqual(fi.filename(), t4)
145 fi.close()
146 finally:
147 remove_tempfiles(t1, t2, t3, t4)
Georg Brandlc98eeed2006-02-19 14:57:47 +0000148
Guido van Rossumd8faa362007-04-27 19:54:29 +0000149 def test_files_that_dont_end_with_newline(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000150 t1 = t2 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000151 try:
152 t1 = writeTmp(1, ["A\nB\nC"])
153 t2 = writeTmp(2, ["D\nE\nF"])
154 fi = FileInput(files=(t1, t2))
155 lines = list(fi)
156 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
157 self.assertEqual(fi.filelineno(), 3)
158 self.assertEqual(fi.lineno(), 6)
159 finally:
160 remove_tempfiles(t1, t2)
161
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000162## def test_unicode_filenames(self):
163## # XXX A unicode string is always returned by writeTmp.
164## # So is this needed?
165## try:
166## t1 = writeTmp(1, ["A\nB"])
167## encoding = sys.getfilesystemencoding()
168## if encoding is None:
169## encoding = 'ascii'
170## fi = FileInput(files=str(t1, encoding))
171## lines = list(fi)
172## self.assertEqual(lines, ["A\n", "B"])
173## finally:
174## remove_tempfiles(t1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000175
176 def test_fileno(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000177 t1 = t2 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000178 try:
179 t1 = writeTmp(1, ["A\nB"])
180 t2 = writeTmp(2, ["C\nD"])
181 fi = FileInput(files=(t1, t2))
182 self.assertEqual(fi.fileno(), -1)
183 line =next( fi)
184 self.assertNotEqual(fi.fileno(), -1)
185 fi.nextfile()
186 self.assertEqual(fi.fileno(), -1)
187 line = list(fi)
188 self.assertEqual(fi.fileno(), -1)
189 finally:
190 remove_tempfiles(t1, t2)
191
192 def test_opening_mode(self):
193 try:
194 # invalid mode, should raise ValueError
195 fi = FileInput(mode="w")
196 self.fail("FileInput should reject invalid mode argument")
197 except ValueError:
198 pass
Guido van Rossume22905a2007-08-27 23:09:25 +0000199 t1 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000200 try:
201 # try opening in universal newline mode
Guido van Rossume22905a2007-08-27 23:09:25 +0000202 t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000203 fi = FileInput(files=t1, mode="U")
204 lines = list(fi)
205 self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
206 finally:
207 remove_tempfiles(t1)
208
Guido van Rossume22905a2007-08-27 23:09:25 +0000209 def test_file_opening_hook(self):
210 try:
211 # cannot use openhook and inplace mode
212 fi = FileInput(inplace=1, openhook=lambda f, m: None)
213 self.fail("FileInput should raise if both inplace "
214 "and openhook arguments are given")
215 except ValueError:
216 pass
217 try:
218 fi = FileInput(openhook=1)
219 self.fail("FileInput should check openhook for being callable")
220 except ValueError:
221 pass
222 # XXX The rot13 codec was removed.
223 # So this test needs to be changed to use something else.
224 # (Or perhaps the API needs to change so we can just pass
225 # an encoding rather than using a hook?)
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000226## try:
227## t1 = writeTmp(1, ["A\nB"], mode="wb")
228## fi = FileInput(files=t1, openhook=hook_encoded("rot13"))
229## lines = list(fi)
230## self.assertEqual(lines, ["N\n", "O"])
231## finally:
232## remove_tempfiles(t1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000233
Georg Brandl6cb7b652010-07-31 20:08:15 +0000234 def test_context_manager(self):
235 try:
236 t1 = writeTmp(1, ["A\nB\nC"])
237 t2 = writeTmp(2, ["D\nE\nF"])
238 with FileInput(files=(t1, t2)) as fi:
239 lines = list(fi)
240 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
241 self.assertEqual(fi.filelineno(), 3)
242 self.assertEqual(fi.lineno(), 6)
243 self.assertEqual(fi._files, ())
244 finally:
245 remove_tempfiles(t1, t2)
246
247 def test_close_on_exception(self):
248 try:
249 t1 = writeTmp(1, [""])
250 with FileInput(files=t1) as fi:
251 raise IOError
252 except IOError:
253 self.assertEqual(fi._files, ())
254 finally:
255 remove_tempfiles(t1)
256
257
Guido van Rossumd8faa362007-04-27 19:54:29 +0000258def test_main():
259 run_unittest(BufferSizesTests, FileInputTests)
260
261if __name__ == "__main__":
262 test_main()