Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Tests for fileinput module. |
| 3 | Nick Mathewson |
| 4 | ''' |
| 5 | |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 6 | from test.test_support import verify, verbose, TESTFN, TestFailed |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 7 | import sys, os, re |
| 8 | from StringIO import StringIO |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 9 | from fileinput import FileInput, hook_encoded |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 10 | |
| 11 | # The fileinput module has 2 interfaces: the FileInput class which does |
| 12 | # all the work, and a few functions (input, etc.) that use a global _state |
| 13 | # variable. We only test the FileInput class, since the other functions |
| 14 | # only provide a thin facade over FileInput. |
| 15 | |
| 16 | # Write lines (a list of lines) to temp file number i, and return the |
| 17 | # temp file's name. |
Tim Peters | 4d7cad1 | 2006-02-19 21:22:10 +0000 | [diff] [blame] | 18 | def writeTmp(i, lines, mode='w'): # opening in text mode is the default |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 19 | name = TESTFN + str(i) |
Tim Peters | 4d7cad1 | 2006-02-19 21:22:10 +0000 | [diff] [blame] | 20 | f = open(name, mode) |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 21 | for line in lines: |
| 22 | f.write(line) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 23 | f.close() |
| 24 | return name |
| 25 | |
| 26 | pat = re.compile(r'LINE (\d+) OF FILE (\d+)') |
| 27 | |
| 28 | def remove_tempfiles(*names): |
| 29 | for name in names: |
| 30 | try: |
| 31 | os.unlink(name) |
| 32 | except: |
| 33 | pass |
| 34 | |
| 35 | def runTests(t1, t2, t3, t4, bs=0, round=0): |
| 36 | start = 1 + round*6 |
| 37 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 38 | print('%s. Simple iteration (bs=%s)' % (start+0, bs)) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 39 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
| 40 | lines = list(fi) |
| 41 | fi.close() |
| 42 | verify(len(lines) == 31) |
| 43 | verify(lines[4] == 'Line 5 of file 1\n') |
| 44 | verify(lines[30] == 'Line 1 of file 4\n') |
| 45 | verify(fi.lineno() == 31) |
| 46 | verify(fi.filename() == t4) |
| 47 | |
| 48 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 49 | print('%s. Status variables (bs=%s)' % (start+1, bs)) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 50 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
| 51 | s = "x" |
| 52 | while s and s != 'Line 6 of file 2\n': |
| 53 | s = fi.readline() |
| 54 | verify(fi.filename() == t2) |
| 55 | verify(fi.lineno() == 21) |
| 56 | verify(fi.filelineno() == 6) |
| 57 | verify(not fi.isfirstline()) |
| 58 | verify(not fi.isstdin()) |
| 59 | |
| 60 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 61 | print('%s. Nextfile (bs=%s)' % (start+2, bs)) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 62 | fi.nextfile() |
| 63 | verify(fi.readline() == 'Line 1 of file 3\n') |
| 64 | verify(fi.lineno() == 22) |
| 65 | fi.close() |
| 66 | |
| 67 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 68 | print('%s. Stdin (bs=%s)' % (start+3, bs)) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 69 | fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs) |
| 70 | savestdin = sys.stdin |
| 71 | try: |
| 72 | sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n") |
| 73 | lines = list(fi) |
| 74 | verify(len(lines) == 33) |
| 75 | verify(lines[32] == 'Line 2 of stdin\n') |
| 76 | verify(fi.filename() == '<stdin>') |
| 77 | fi.nextfile() |
| 78 | finally: |
| 79 | sys.stdin = savestdin |
| 80 | |
| 81 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 82 | print('%s. Boundary conditions (bs=%s)' % (start+4, bs)) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 83 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
| 84 | verify(fi.lineno() == 0) |
| 85 | verify(fi.filename() == None) |
| 86 | fi.nextfile() |
| 87 | verify(fi.lineno() == 0) |
| 88 | verify(fi.filename() == None) |
| 89 | |
| 90 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 91 | print('%s. Inplace (bs=%s)' % (start+5, bs)) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 92 | savestdout = sys.stdout |
| 93 | try: |
| 94 | fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs) |
| 95 | for line in fi: |
| 96 | line = line[:-1].upper() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 97 | print(line) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 98 | fi.close() |
| 99 | finally: |
| 100 | sys.stdout = savestdout |
| 101 | |
| 102 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
| 103 | for line in fi: |
| 104 | verify(line[-1] == '\n') |
| 105 | m = pat.match(line[:-1]) |
| 106 | verify(m != None) |
| 107 | verify(int(m.group(1)) == fi.filelineno()) |
| 108 | fi.close() |
| 109 | |
| 110 | |
| 111 | def writeFiles(): |
| 112 | global t1, t2, t3, t4 |
| 113 | t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)]) |
| 114 | t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)]) |
| 115 | t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)]) |
| 116 | t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)]) |
| 117 | |
| 118 | # First, run the tests with default and teeny buffer size. |
| 119 | for round, bs in (0, 0), (1, 30): |
| 120 | try: |
| 121 | writeFiles() |
| 122 | runTests(t1, t2, t3, t4, bs, round) |
| 123 | finally: |
| 124 | remove_tempfiles(t1, t2, t3, t4) |
| 125 | |
| 126 | # Next, check for proper behavior with 0-byte files. |
| 127 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 128 | print("13. 0-byte files") |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 129 | try: |
| 130 | t1 = writeTmp(1, [""]) |
| 131 | t2 = writeTmp(2, [""]) |
| 132 | t3 = writeTmp(3, ["The only line there is.\n"]) |
| 133 | t4 = writeTmp(4, [""]) |
| 134 | fi = FileInput(files=(t1, t2, t3, t4)) |
| 135 | line = fi.readline() |
| 136 | verify(line == 'The only line there is.\n') |
| 137 | verify(fi.lineno() == 1) |
| 138 | verify(fi.filelineno() == 1) |
| 139 | verify(fi.filename() == t3) |
| 140 | line = fi.readline() |
| 141 | verify(not line) |
| 142 | verify(fi.lineno() == 1) |
| 143 | verify(fi.filelineno() == 0) |
| 144 | verify(fi.filename() == t4) |
| 145 | fi.close() |
| 146 | finally: |
| 147 | remove_tempfiles(t1, t2, t3, t4) |
| 148 | |
| 149 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 150 | print("14. Files that don't end with newline") |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 151 | 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 | verify(lines == ["A\n", "B\n", "C", "D\n", "E\n", "F"]) |
| 157 | verify(fi.filelineno() == 3) |
| 158 | verify(fi.lineno() == 6) |
| 159 | finally: |
| 160 | remove_tempfiles(t1, t2) |
Georg Brandl | e466217 | 2006-02-19 09:51:27 +0000 | [diff] [blame] | 161 | |
| 162 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 163 | print("15. Unicode filenames") |
Georg Brandl | e466217 | 2006-02-19 09:51:27 +0000 | [diff] [blame] | 164 | try: |
| 165 | t1 = writeTmp(1, ["A\nB"]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 166 | encoding = sys.getfilesystemencoding() |
| 167 | if encoding is None: |
| 168 | encoding = 'ascii' |
| 169 | fi = FileInput(files=unicode(t1, encoding)) |
Georg Brandl | e466217 | 2006-02-19 09:51:27 +0000 | [diff] [blame] | 170 | lines = list(fi) |
| 171 | verify(lines == ["A\n", "B"]) |
| 172 | finally: |
| 173 | remove_tempfiles(t1) |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 174 | |
| 175 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 176 | print("16. fileno()") |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 177 | try: |
| 178 | t1 = writeTmp(1, ["A\nB"]) |
| 179 | t2 = writeTmp(2, ["C\nD"]) |
| 180 | fi = FileInput(files=(t1, t2)) |
| 181 | verify(fi.fileno() == -1) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 182 | line = next(fi) |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 183 | verify(fi.fileno() != -1) |
| 184 | fi.nextfile() |
| 185 | verify(fi.fileno() == -1) |
| 186 | line = list(fi) |
| 187 | verify(fi.fileno() == -1) |
| 188 | finally: |
| 189 | remove_tempfiles(t1, t2) |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 190 | |
| 191 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 192 | print("17. Specify opening mode") |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 193 | try: |
| 194 | # invalid mode, should raise ValueError |
| 195 | fi = FileInput(mode="w") |
| 196 | raise TestFailed("FileInput should reject invalid mode argument") |
| 197 | except ValueError: |
| 198 | pass |
| 199 | try: |
| 200 | # try opening in universal newline mode |
Tim Peters | 4d7cad1 | 2006-02-19 21:22:10 +0000 | [diff] [blame] | 201 | t1 = writeTmp(1, ["A\nB\r\nC\rD"], mode="wb") |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 202 | fi = FileInput(files=t1, mode="U") |
| 203 | lines = list(fi) |
| 204 | verify(lines == ["A\n", "B\n", "C\n", "D"]) |
| 205 | finally: |
| 206 | remove_tempfiles(t1) |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 207 | |
| 208 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 209 | print("18. Test file opening hook") |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 210 | try: |
| 211 | # cannot use openhook and inplace mode |
| 212 | fi = FileInput(inplace=1, openhook=lambda f,m: None) |
| 213 | raise TestFailed("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 | raise TestFailed("FileInput should check openhook for being callable") |
| 220 | except ValueError: |
| 221 | pass |
| 222 | try: |
Tim Peters | 4d7cad1 | 2006-02-19 21:22:10 +0000 | [diff] [blame] | 223 | t1 = writeTmp(1, ["A\nB"], mode="wb") |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 224 | fi = FileInput(files=t1, openhook=hook_encoded("rot13")) |
| 225 | lines = list(fi) |
| 226 | verify(lines == ["N\n", "O"]) |
| 227 | finally: |
| 228 | remove_tempfiles(t1) |