1 | n/a | """ Tests for the linecache module """ |
---|
2 | n/a | |
---|
3 | n/a | import linecache |
---|
4 | n/a | import unittest |
---|
5 | n/a | import os.path |
---|
6 | n/a | import tempfile |
---|
7 | n/a | import tokenize |
---|
8 | n/a | from test import support |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | FILENAME = linecache.__file__ |
---|
12 | n/a | NONEXISTENT_FILENAME = FILENAME + '.missing' |
---|
13 | n/a | INVALID_NAME = '!@$)(!@#_1' |
---|
14 | n/a | EMPTY = '' |
---|
15 | n/a | TEST_PATH = os.path.dirname(__file__) |
---|
16 | n/a | MODULES = "linecache abc".split() |
---|
17 | n/a | MODULE_PATH = os.path.dirname(FILENAME) |
---|
18 | n/a | |
---|
19 | n/a | SOURCE_1 = ''' |
---|
20 | n/a | " Docstring " |
---|
21 | n/a | |
---|
22 | n/a | def function(): |
---|
23 | n/a | return result |
---|
24 | n/a | |
---|
25 | n/a | ''' |
---|
26 | n/a | |
---|
27 | n/a | SOURCE_2 = ''' |
---|
28 | n/a | def f(): |
---|
29 | n/a | return 1 + 1 |
---|
30 | n/a | |
---|
31 | n/a | a = f() |
---|
32 | n/a | |
---|
33 | n/a | ''' |
---|
34 | n/a | |
---|
35 | n/a | SOURCE_3 = ''' |
---|
36 | n/a | def f(): |
---|
37 | n/a | return 3''' # No ending newline |
---|
38 | n/a | |
---|
39 | n/a | |
---|
40 | n/a | class TempFile: |
---|
41 | n/a | |
---|
42 | n/a | def setUp(self): |
---|
43 | n/a | super().setUp() |
---|
44 | n/a | with tempfile.NamedTemporaryFile(delete=False) as fp: |
---|
45 | n/a | self.file_name = fp.name |
---|
46 | n/a | fp.write(self.file_byte_string) |
---|
47 | n/a | self.addCleanup(support.unlink, self.file_name) |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | class GetLineTestsGoodData(TempFile): |
---|
51 | n/a | # file_list = ['list\n', 'of\n', 'good\n', 'strings\n'] |
---|
52 | n/a | |
---|
53 | n/a | def setUp(self): |
---|
54 | n/a | self.file_byte_string = ''.join(self.file_list).encode('utf-8') |
---|
55 | n/a | super().setUp() |
---|
56 | n/a | |
---|
57 | n/a | def test_getline(self): |
---|
58 | n/a | with tokenize.open(self.file_name) as fp: |
---|
59 | n/a | for index, line in enumerate(fp): |
---|
60 | n/a | if not line.endswith('\n'): |
---|
61 | n/a | line += '\n' |
---|
62 | n/a | |
---|
63 | n/a | cached_line = linecache.getline(self.file_name, index + 1) |
---|
64 | n/a | self.assertEqual(line, cached_line) |
---|
65 | n/a | |
---|
66 | n/a | def test_getlines(self): |
---|
67 | n/a | lines = linecache.getlines(self.file_name) |
---|
68 | n/a | self.assertEqual(lines, self.file_list) |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | class GetLineTestsBadData(TempFile): |
---|
72 | n/a | # file_byte_string = b'Bad data goes here' |
---|
73 | n/a | |
---|
74 | n/a | def test_getline(self): |
---|
75 | n/a | self.assertRaises((SyntaxError, UnicodeDecodeError), |
---|
76 | n/a | linecache.getline, self.file_name, 1) |
---|
77 | n/a | |
---|
78 | n/a | def test_getlines(self): |
---|
79 | n/a | self.assertRaises((SyntaxError, UnicodeDecodeError), |
---|
80 | n/a | linecache.getlines, self.file_name) |
---|
81 | n/a | |
---|
82 | n/a | |
---|
83 | n/a | class EmptyFile(GetLineTestsGoodData, unittest.TestCase): |
---|
84 | n/a | file_list = [] |
---|
85 | n/a | |
---|
86 | n/a | |
---|
87 | n/a | class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase): |
---|
88 | n/a | file_list = ['\n'] |
---|
89 | n/a | |
---|
90 | n/a | |
---|
91 | n/a | class GoodUnicode(GetLineTestsGoodData, unittest.TestCase): |
---|
92 | n/a | file_list = ['á\n', 'b\n', 'abcdef\n', 'ááááá\n'] |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | class BadUnicode(GetLineTestsBadData, unittest.TestCase): |
---|
96 | n/a | file_byte_string = b'\x80abc' |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | class LineCacheTests(unittest.TestCase): |
---|
100 | n/a | |
---|
101 | n/a | def test_getline(self): |
---|
102 | n/a | getline = linecache.getline |
---|
103 | n/a | |
---|
104 | n/a | # Bad values for line number should return an empty string |
---|
105 | n/a | self.assertEqual(getline(FILENAME, 2**15), EMPTY) |
---|
106 | n/a | self.assertEqual(getline(FILENAME, -1), EMPTY) |
---|
107 | n/a | |
---|
108 | n/a | # Float values currently raise TypeError, should it? |
---|
109 | n/a | self.assertRaises(TypeError, getline, FILENAME, 1.1) |
---|
110 | n/a | |
---|
111 | n/a | # Bad filenames should return an empty string |
---|
112 | n/a | self.assertEqual(getline(EMPTY, 1), EMPTY) |
---|
113 | n/a | self.assertEqual(getline(INVALID_NAME, 1), EMPTY) |
---|
114 | n/a | |
---|
115 | n/a | # Check module loading |
---|
116 | n/a | for entry in MODULES: |
---|
117 | n/a | filename = os.path.join(MODULE_PATH, entry) + '.py' |
---|
118 | n/a | with open(filename) as file: |
---|
119 | n/a | for index, line in enumerate(file): |
---|
120 | n/a | self.assertEqual(line, getline(filename, index + 1)) |
---|
121 | n/a | |
---|
122 | n/a | # Check that bogus data isn't returned (issue #1309567) |
---|
123 | n/a | empty = linecache.getlines('a/b/c/__init__.py') |
---|
124 | n/a | self.assertEqual(empty, []) |
---|
125 | n/a | |
---|
126 | n/a | def test_no_ending_newline(self): |
---|
127 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
128 | n/a | with open(support.TESTFN, "w") as fp: |
---|
129 | n/a | fp.write(SOURCE_3) |
---|
130 | n/a | lines = linecache.getlines(support.TESTFN) |
---|
131 | n/a | self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"]) |
---|
132 | n/a | |
---|
133 | n/a | def test_clearcache(self): |
---|
134 | n/a | cached = [] |
---|
135 | n/a | for entry in MODULES: |
---|
136 | n/a | filename = os.path.join(MODULE_PATH, entry) + '.py' |
---|
137 | n/a | cached.append(filename) |
---|
138 | n/a | linecache.getline(filename, 1) |
---|
139 | n/a | |
---|
140 | n/a | # Are all files cached? |
---|
141 | n/a | self.assertNotEqual(cached, []) |
---|
142 | n/a | cached_empty = [fn for fn in cached if fn not in linecache.cache] |
---|
143 | n/a | self.assertEqual(cached_empty, []) |
---|
144 | n/a | |
---|
145 | n/a | # Can we clear the cache? |
---|
146 | n/a | linecache.clearcache() |
---|
147 | n/a | cached_empty = [fn for fn in cached if fn in linecache.cache] |
---|
148 | n/a | self.assertEqual(cached_empty, []) |
---|
149 | n/a | |
---|
150 | n/a | def test_checkcache(self): |
---|
151 | n/a | getline = linecache.getline |
---|
152 | n/a | # Create a source file and cache its contents |
---|
153 | n/a | source_name = support.TESTFN + '.py' |
---|
154 | n/a | self.addCleanup(support.unlink, source_name) |
---|
155 | n/a | with open(source_name, 'w') as source: |
---|
156 | n/a | source.write(SOURCE_1) |
---|
157 | n/a | getline(source_name, 1) |
---|
158 | n/a | |
---|
159 | n/a | # Keep a copy of the old contents |
---|
160 | n/a | source_list = [] |
---|
161 | n/a | with open(source_name) as source: |
---|
162 | n/a | for index, line in enumerate(source): |
---|
163 | n/a | self.assertEqual(line, getline(source_name, index + 1)) |
---|
164 | n/a | source_list.append(line) |
---|
165 | n/a | |
---|
166 | n/a | with open(source_name, 'w') as source: |
---|
167 | n/a | source.write(SOURCE_2) |
---|
168 | n/a | |
---|
169 | n/a | # Try to update a bogus cache entry |
---|
170 | n/a | linecache.checkcache('dummy') |
---|
171 | n/a | |
---|
172 | n/a | # Check that the cache matches the old contents |
---|
173 | n/a | for index, line in enumerate(source_list): |
---|
174 | n/a | self.assertEqual(line, getline(source_name, index + 1)) |
---|
175 | n/a | |
---|
176 | n/a | # Update the cache and check whether it matches the new source file |
---|
177 | n/a | linecache.checkcache(source_name) |
---|
178 | n/a | with open(source_name) as source: |
---|
179 | n/a | for index, line in enumerate(source): |
---|
180 | n/a | self.assertEqual(line, getline(source_name, index + 1)) |
---|
181 | n/a | source_list.append(line) |
---|
182 | n/a | |
---|
183 | n/a | def test_lazycache_no_globals(self): |
---|
184 | n/a | lines = linecache.getlines(FILENAME) |
---|
185 | n/a | linecache.clearcache() |
---|
186 | n/a | self.assertEqual(False, linecache.lazycache(FILENAME, None)) |
---|
187 | n/a | self.assertEqual(lines, linecache.getlines(FILENAME)) |
---|
188 | n/a | |
---|
189 | n/a | def test_lazycache_smoke(self): |
---|
190 | n/a | lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
---|
191 | n/a | linecache.clearcache() |
---|
192 | n/a | self.assertEqual( |
---|
193 | n/a | True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) |
---|
194 | n/a | self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) |
---|
195 | n/a | # Note here that we're looking up a nonexistent filename with no |
---|
196 | n/a | # globals: this would error if the lazy value wasn't resolved. |
---|
197 | n/a | self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME)) |
---|
198 | n/a | |
---|
199 | n/a | def test_lazycache_provide_after_failed_lookup(self): |
---|
200 | n/a | linecache.clearcache() |
---|
201 | n/a | lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
---|
202 | n/a | linecache.clearcache() |
---|
203 | n/a | linecache.getlines(NONEXISTENT_FILENAME) |
---|
204 | n/a | linecache.lazycache(NONEXISTENT_FILENAME, globals()) |
---|
205 | n/a | self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME)) |
---|
206 | n/a | |
---|
207 | n/a | def test_lazycache_check(self): |
---|
208 | n/a | linecache.clearcache() |
---|
209 | n/a | linecache.lazycache(NONEXISTENT_FILENAME, globals()) |
---|
210 | n/a | linecache.checkcache() |
---|
211 | n/a | |
---|
212 | n/a | def test_lazycache_bad_filename(self): |
---|
213 | n/a | linecache.clearcache() |
---|
214 | n/a | self.assertEqual(False, linecache.lazycache('', globals())) |
---|
215 | n/a | self.assertEqual(False, linecache.lazycache('<foo>', globals())) |
---|
216 | n/a | |
---|
217 | n/a | def test_lazycache_already_cached(self): |
---|
218 | n/a | linecache.clearcache() |
---|
219 | n/a | lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
---|
220 | n/a | self.assertEqual( |
---|
221 | n/a | False, |
---|
222 | n/a | linecache.lazycache(NONEXISTENT_FILENAME, globals())) |
---|
223 | n/a | self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME])) |
---|
224 | n/a | |
---|
225 | n/a | def test_memoryerror(self): |
---|
226 | n/a | lines = linecache.getlines(FILENAME) |
---|
227 | n/a | self.assertTrue(lines) |
---|
228 | n/a | def raise_memoryerror(*args, **kwargs): |
---|
229 | n/a | raise MemoryError |
---|
230 | n/a | with support.swap_attr(linecache, 'updatecache', raise_memoryerror): |
---|
231 | n/a | lines2 = linecache.getlines(FILENAME) |
---|
232 | n/a | self.assertEqual(lines2, lines) |
---|
233 | n/a | |
---|
234 | n/a | linecache.clearcache() |
---|
235 | n/a | with support.swap_attr(linecache, 'updatecache', raise_memoryerror): |
---|
236 | n/a | lines3 = linecache.getlines(FILENAME) |
---|
237 | n/a | self.assertEqual(lines3, []) |
---|
238 | n/a | self.assertEqual(linecache.getlines(FILENAME), lines) |
---|
239 | n/a | |
---|
240 | n/a | |
---|
241 | n/a | if __name__ == "__main__": |
---|
242 | n/a | unittest.main() |
---|