| 1 |
""" Tests for the linecache module """ |
| 2 |
|
| 3 |
import linecache |
| 4 |
import unittest |
| 5 |
import os.path |
| 6 |
import tempfile |
| 7 |
import threading |
| 8 |
import tokenize |
| 9 |
from importlib.machinery import ModuleSpec |
| 10 |
from test import support |
| 11 |
from test.support import os_helper |
| 12 |
from test.support import threading_helper |
| 13 |
from test.support.script_helper import assert_python_ok |
| 14 |
|
| 15 |
|
| 16 |
FILENAME = linecache.__file__ |
| 17 |
NONEXISTENT_FILENAME = FILENAME + '.missing' |
| 18 |
INVALID_NAME = '!@$)(!@#_1' |
| 19 |
EMPTY = '' |
| 20 |
TEST_PATH = os.path.dirname(__file__) |
| 21 |
MODULES = "linecache abc".split() |
| 22 |
MODULE_PATH = os.path.dirname(FILENAME) |
| 23 |
|
| 24 |
SOURCE_1 = ''' |
| 25 |
" Docstring " |
| 26 |
|
| 27 |
def function(): |
| 28 |
return result |
| 29 |
|
| 30 |
''' |
| 31 |
|
| 32 |
SOURCE_2 = ''' |
| 33 |
def f(): |
| 34 |
return 1 + 1 |
| 35 |
|
| 36 |
a = f() |
| 37 |
|
| 38 |
''' |
| 39 |
|
| 40 |
SOURCE_3 = ''' |
| 41 |
def f(): |
| 42 |
return 3''' # No ending newline |
| 43 |
|
| 44 |
|
| 45 |
class TempFile: |
| 46 |
|
| 47 |
def setUp(self): |
| 48 |
super().setUp() |
| 49 |
with tempfile.NamedTemporaryFile(delete=False) as fp: |
| 50 |
self.file_name = fp.name |
| 51 |
fp.write(self.file_byte_string) |
| 52 |
self.addCleanup(os_helper.unlink, self.file_name) |
| 53 |
|
| 54 |
|
| 55 |
class GetLineTestsGoodData(TempFile): |
| 56 |
# file_list = ['list\n', 'of\n', 'good\n', 'strings\n'] |
| 57 |
|
| 58 |
def setUp(self): |
| 59 |
self.file_byte_string = ''.join(self.file_list).encode('utf-8') |
| 60 |
super().setUp() |
| 61 |
|
| 62 |
def test_getline(self): |
| 63 |
with tokenize.open(self.file_name) as fp: |
| 64 |
for index, line in enumerate(fp): |
| 65 |
if not line.endswith('\n'): |
| 66 |
line += '\n' |
| 67 |
|
| 68 |
cached_line = linecache.getline(self.file_name, index + 1) |
| 69 |
self.assertEqual(line, cached_line) |
| 70 |
|
| 71 |
def test_getlines(self): |
| 72 |
lines = linecache.getlines(self.file_name) |
| 73 |
self.assertEqual(lines, self.file_list) |
| 74 |
|
| 75 |
|
| 76 |
class GetLineTestsBadData(TempFile): |
| 77 |
# file_byte_string = b'Bad data goes here' |
| 78 |
|
| 79 |
def test_getline(self): |
| 80 |
self.assertEqual(linecache.getline(self.file_name, 1), '') |
| 81 |
|
| 82 |
def test_getlines(self): |
| 83 |
self.assertEqual(linecache.getlines(self.file_name), []) |
| 84 |
|
| 85 |
|
| 86 |
class EmptyFile(GetLineTestsGoodData, unittest.TestCase): |
| 87 |
file_list = [] |
| 88 |
|
| 89 |
def test_getlines(self): |
| 90 |
lines = linecache.getlines(self.file_name) |
| 91 |
self.assertEqual(lines, ['\n']) |
| 92 |
|
| 93 |
|
| 94 |
class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase): |
| 95 |
file_list = ['\n'] |
| 96 |
|
| 97 |
|
| 98 |
class GoodUnicode(GetLineTestsGoodData, unittest.TestCase): |
| 99 |
file_list = ['á\n', 'b\n', 'abcdef\n', 'ááááá\n'] |
| 100 |
|
| 101 |
class BadUnicode_NoDeclaration(GetLineTestsBadData, unittest.TestCase): |
| 102 |
file_byte_string = b'\n\x80abc' |
| 103 |
|
| 104 |
class BadUnicode_WithDeclaration(GetLineTestsBadData, unittest.TestCase): |
| 105 |
file_byte_string = b'# coding=utf-8\n\x80abc' |
| 106 |
|
| 107 |
|
| 108 |
class FakeLoader: |
| 109 |
def get_source(self, fullname): |
| 110 |
return f'source for {fullname}' |
| 111 |
|
| 112 |
|
| 113 |
class NoSourceLoader: |
| 114 |
def get_source(self, fullname): |
| 115 |
return None |
| 116 |
|
| 117 |
|
| 118 |
class LineCacheTests(unittest.TestCase): |
| 119 |
|
| 120 |
def test_getline(self): |
| 121 |
getline = linecache.getline |
| 122 |
|
| 123 |
# Bad values for line number should return an empty string |
| 124 |
self.assertEqual(getline(FILENAME, 2**15), EMPTY) |
| 125 |
self.assertEqual(getline(FILENAME, -1), EMPTY) |
| 126 |
|
| 127 |
# Float values currently raise TypeError, should it? |
| 128 |
self.assertRaises(TypeError, getline, FILENAME, 1.1) |
| 129 |
|
| 130 |
# Bad filenames should return an empty string |
| 131 |
self.assertEqual(getline(EMPTY, 1), EMPTY) |
| 132 |
self.assertEqual(getline(INVALID_NAME, 1), EMPTY) |
| 133 |
|
| 134 |
# Check module loading |
| 135 |
for entry in MODULES: |
| 136 |
filename = os.path.join(MODULE_PATH, entry) + '.py' |
| 137 |
with open(filename, encoding='utf-8') as file: |
| 138 |
for index, line in enumerate(file): |
| 139 |
self.assertEqual(line, getline(filename, index + 1)) |
| 140 |
|
| 141 |
# Check that bogus data isn't returned (issue #1309567) |
| 142 |
empty = linecache.getlines('a/b/c/__init__.py') |
| 143 |
self.assertEqual(empty, []) |
| 144 |
|
| 145 |
def test_no_ending_newline(self): |
| 146 |
self.addCleanup(os_helper.unlink, os_helper.TESTFN) |
| 147 |
with open(os_helper.TESTFN, "w", encoding='utf-8') as fp: |
| 148 |
fp.write(SOURCE_3) |
| 149 |
lines = linecache.getlines(os_helper.TESTFN) |
| 150 |
self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"]) |
| 151 |
|
| 152 |
def test_clearcache(self): |
| 153 |
cached = [] |
| 154 |
for entry in MODULES: |
| 155 |
filename = os.path.join(MODULE_PATH, entry) + '.py' |
| 156 |
cached.append(filename) |
| 157 |
linecache.getline(filename, 1) |
| 158 |
|
| 159 |
# Are all files cached? |
| 160 |
self.assertNotEqual(cached, []) |
| 161 |
cached_empty = [fn for fn in cached if fn not in linecache.cache] |
| 162 |
self.assertEqual(cached_empty, []) |
| 163 |
|
| 164 |
# Can we clear the cache? |
| 165 |
linecache.clearcache() |
| 166 |
cached_empty = [fn for fn in cached if fn in linecache.cache] |
| 167 |
self.assertEqual(cached_empty, []) |
| 168 |
|
| 169 |
def test_checkcache(self): |
| 170 |
getline = linecache.getline |
| 171 |
# Create a source file and cache its contents |
| 172 |
source_name = os_helper.TESTFN + '.py' |
| 173 |
self.addCleanup(os_helper.unlink, source_name) |
| 174 |
with open(source_name, 'w', encoding='utf-8') as source: |
| 175 |
source.write(SOURCE_1) |
| 176 |
getline(source_name, 1) |
| 177 |
|
| 178 |
# Keep a copy of the old contents |
| 179 |
source_list = [] |
| 180 |
with open(source_name, encoding='utf-8') as source: |
| 181 |
for index, line in enumerate(source): |
| 182 |
self.assertEqual(line, getline(source_name, index + 1)) |
| 183 |
source_list.append(line) |
| 184 |
|
| 185 |
with open(source_name, 'w', encoding='utf-8') as source: |
| 186 |
source.write(SOURCE_2) |
| 187 |
|
| 188 |
# Try to update a bogus cache entry |
| 189 |
linecache.checkcache('dummy') |
| 190 |
|
| 191 |
# Check that the cache matches the old contents |
| 192 |
for index, line in enumerate(source_list): |
| 193 |
self.assertEqual(line, getline(source_name, index + 1)) |
| 194 |
|
| 195 |
# Update the cache and check whether it matches the new source file |
| 196 |
linecache.checkcache(source_name) |
| 197 |
with open(source_name, encoding='utf-8') as source: |
| 198 |
for index, line in enumerate(source): |
| 199 |
self.assertEqual(line, getline(source_name, index + 1)) |
| 200 |
source_list.append(line) |
| 201 |
|
| 202 |
def test_lazycache_no_globals(self): |
| 203 |
lines = linecache.getlines(FILENAME) |
| 204 |
linecache.clearcache() |
| 205 |
self.assertEqual(False, linecache.lazycache(FILENAME, None)) |
| 206 |
self.assertEqual(lines, linecache.getlines(FILENAME)) |
| 207 |
|
| 208 |
def test_lazycache_smoke(self): |
| 209 |
lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
| 210 |
linecache.clearcache() |
| 211 |
self.assertEqual( |
| 212 |
True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) |
| 213 |
self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) |
| 214 |
# Note here that we're looking up a nonexistent filename with no |
| 215 |
# globals: this would error if the lazy value wasn't resolved. |
| 216 |
self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME)) |
| 217 |
|
| 218 |
def test_lazycache_provide_after_failed_lookup(self): |
| 219 |
linecache.clearcache() |
| 220 |
lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
| 221 |
linecache.clearcache() |
| 222 |
linecache.getlines(NONEXISTENT_FILENAME) |
| 223 |
linecache.lazycache(NONEXISTENT_FILENAME, globals()) |
| 224 |
self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME)) |
| 225 |
|
| 226 |
def test_lazycache_check(self): |
| 227 |
linecache.clearcache() |
| 228 |
linecache.lazycache(NONEXISTENT_FILENAME, globals()) |
| 229 |
linecache.checkcache() |
| 230 |
|
| 231 |
def test_lazycache_bad_filename(self): |
| 232 |
linecache.clearcache() |
| 233 |
self.assertEqual(False, linecache.lazycache('', globals())) |
| 234 |
self.assertEqual(False, linecache.lazycache('<foo>', globals())) |
| 235 |
|
| 236 |
def test_lazycache_already_cached(self): |
| 237 |
linecache.clearcache() |
| 238 |
lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
| 239 |
self.assertEqual( |
| 240 |
False, |
| 241 |
linecache.lazycache(NONEXISTENT_FILENAME, globals())) |
| 242 |
self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME])) |
| 243 |
|
| 244 |
def test_memoryerror(self): |
| 245 |
lines = linecache.getlines(FILENAME) |
| 246 |
self.assertTrue(lines) |
| 247 |
def raise_memoryerror(*args, **kwargs): |
| 248 |
raise MemoryError |
| 249 |
with support.swap_attr(linecache, 'updatecache', raise_memoryerror): |
| 250 |
lines2 = linecache.getlines(FILENAME) |
| 251 |
self.assertEqual(lines2, lines) |
| 252 |
|
| 253 |
linecache.clearcache() |
| 254 |
with support.swap_attr(linecache, 'updatecache', raise_memoryerror): |
| 255 |
lines3 = linecache.getlines(FILENAME) |
| 256 |
self.assertEqual(lines3, []) |
| 257 |
self.assertEqual(linecache.getlines(FILENAME), lines) |
| 258 |
|
| 259 |
def test_loader(self): |
| 260 |
filename = 'scheme://path' |
| 261 |
|
| 262 |
for loader in (None, object(), NoSourceLoader()): |
| 263 |
linecache.clearcache() |
| 264 |
module_globals = {'__name__': 'a.b.c', '__loader__': loader} |
| 265 |
self.assertEqual(linecache.getlines(filename, module_globals), []) |
| 266 |
|
| 267 |
linecache.clearcache() |
| 268 |
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader()} |
| 269 |
self.assertEqual(linecache.getlines(filename, module_globals), |
| 270 |
['source for a.b.c\n']) |
| 271 |
|
| 272 |
for spec in (None, object(), ModuleSpec('', FakeLoader())): |
| 273 |
linecache.clearcache() |
| 274 |
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader(), |
| 275 |
'__spec__': spec} |
| 276 |
self.assertEqual(linecache.getlines(filename, module_globals), |
| 277 |
['source for a.b.c\n']) |
| 278 |
|
| 279 |
linecache.clearcache() |
| 280 |
spec = ModuleSpec('x.y.z', FakeLoader()) |
| 281 |
module_globals = {'__name__': 'a.b.c', '__loader__': spec.loader, |
| 282 |
'__spec__': spec} |
| 283 |
self.assertEqual(linecache.getlines(filename, module_globals), |
| 284 |
['source for x.y.z\n']) |
| 285 |
|
| 286 |
def test_frozen(self): |
| 287 |
filename = '<frozen fakemodule>' |
| 288 |
module_globals = {'__file__': FILENAME} |
| 289 |
empty = linecache.getlines(filename) |
| 290 |
self.assertEqual(empty, []) |
| 291 |
lines = linecache.getlines(filename, module_globals) |
| 292 |
self.assertGreater(len(lines), 0) |
| 293 |
lines_cached = linecache.getlines(filename) |
| 294 |
self.assertEqual(lines, lines_cached) |
| 295 |
linecache.clearcache() |
| 296 |
empty = linecache.getlines(filename) |
| 297 |
self.assertEqual(empty, []) |
| 298 |
|
| 299 |
def test_invalid_names(self): |
| 300 |
for name, desc in [ |
| 301 |
('\x00', 'NUL bytes filename'), |
| 302 |
(__file__ + '\x00', 'filename with embedded NUL bytes'), |
| 303 |
# A filename with surrogate codes. A UnicodeEncodeError is raised |
| 304 |
# by os.stat() upon querying, which is a subclass of ValueError. |
| 305 |
("\uD834\uDD1E.py", 'surrogate codes (MUSICAL SYMBOL G CLEF)'), |
| 306 |
# For POSIX platforms, an OSError will be raised but for Windows |
| 307 |
# platforms, a ValueError is raised due to the path_t converter. |
| 308 |
# See: https://github.com/python/cpython/issues/122170 |
| 309 |
('a' * 1_000_000, 'very long filename'), |
| 310 |
]: |
| 311 |
with self.subTest(f'updatecache: {desc}'): |
| 312 |
linecache.clearcache() |
| 313 |
lines = linecache.updatecache(name) |
| 314 |
self.assertListEqual(lines, []) |
| 315 |
self.assertNotIn(name, linecache.cache) |
| 316 |
|
| 317 |
# hack into the cache (it shouldn't be allowed |
| 318 |
# but we never know what people do...) |
| 319 |
for key, fullname in [(name, 'ok'), ('key', name), (name, name)]: |
| 320 |
with self.subTest(f'checkcache: {desc}', |
| 321 |
key=key, fullname=fullname): |
| 322 |
linecache.clearcache() |
| 323 |
linecache.cache[key] = (0, 1234, [], fullname) |
| 324 |
linecache.checkcache(key) |
| 325 |
self.assertNotIn(key, linecache.cache) |
| 326 |
|
| 327 |
# just to be sure that we did not mess with cache |
| 328 |
linecache.clearcache() |
| 329 |
|
| 330 |
def test_linecache_python_string(self): |
| 331 |
cmdline = "import linecache;assert len(linecache.cache) == 0" |
| 332 |
retcode, stdout, stderr = assert_python_ok('-c', cmdline) |
| 333 |
self.assertEqual(retcode, 0) |
| 334 |
self.assertEqual(stdout, b'') |
| 335 |
self.assertEqual(stderr, b'') |
| 336 |
|
| 337 |
class LineCacheInvalidationTests(unittest.TestCase): |
| 338 |
def setUp(self): |
| 339 |
super().setUp() |
| 340 |
linecache.clearcache() |
| 341 |
self.deleted_file = os_helper.TESTFN + '.1' |
| 342 |
self.modified_file = os_helper.TESTFN + '.2' |
| 343 |
self.unchanged_file = os_helper.TESTFN + '.3' |
| 344 |
|
| 345 |
for fname in (self.deleted_file, |
| 346 |
self.modified_file, |
| 347 |
self.unchanged_file): |
| 348 |
self.addCleanup(os_helper.unlink, fname) |
| 349 |
with open(fname, 'w', encoding='utf-8') as source: |
| 350 |
source.write(f'print("I am {fname}")') |
| 351 |
|
| 352 |
self.assertNotIn(fname, linecache.cache) |
| 353 |
linecache.getlines(fname) |
| 354 |
self.assertIn(fname, linecache.cache) |
| 355 |
|
| 356 |
os.remove(self.deleted_file) |
| 357 |
with open(self.modified_file, 'w', encoding='utf-8') as source: |
| 358 |
source.write('print("was modified")') |
| 359 |
|
| 360 |
def test_checkcache_for_deleted_file(self): |
| 361 |
linecache.checkcache(self.deleted_file) |
| 362 |
self.assertNotIn(self.deleted_file, linecache.cache) |
| 363 |
self.assertIn(self.modified_file, linecache.cache) |
| 364 |
self.assertIn(self.unchanged_file, linecache.cache) |
| 365 |
|
| 366 |
def test_checkcache_for_modified_file(self): |
| 367 |
linecache.checkcache(self.modified_file) |
| 368 |
self.assertIn(self.deleted_file, linecache.cache) |
| 369 |
self.assertNotIn(self.modified_file, linecache.cache) |
| 370 |
self.assertIn(self.unchanged_file, linecache.cache) |
| 371 |
|
| 372 |
def test_checkcache_with_no_parameter(self): |
| 373 |
linecache.checkcache() |
| 374 |
self.assertNotIn(self.deleted_file, linecache.cache) |
| 375 |
self.assertNotIn(self.modified_file, linecache.cache) |
| 376 |
self.assertIn(self.unchanged_file, linecache.cache) |
| 377 |
|
| 378 |
|
| 379 |
class MultiThreadingTest(unittest.TestCase): |
| 380 |
@threading_helper.reap_threads |
| 381 |
@threading_helper.requires_working_threading() |
| 382 |
def test_read_write_safety(self): |
| 383 |
|
| 384 |
with tempfile.TemporaryDirectory() as tmpdirname: |
| 385 |
filenames = [] |
| 386 |
for i in range(10): |
| 387 |
name = os.path.join(tmpdirname, f"test_{i}.py") |
| 388 |
with open(name, "w") as h: |
| 389 |
h.write("import time\n") |
| 390 |
h.write("import system\n") |
| 391 |
filenames.append(name) |
| 392 |
|
| 393 |
def linecache_get_line(b): |
| 394 |
b.wait() |
| 395 |
for _ in range(100): |
| 396 |
for name in filenames: |
| 397 |
linecache.getline(name, 1) |
| 398 |
|
| 399 |
def check(funcs): |
| 400 |
barrier = threading.Barrier(len(funcs)) |
| 401 |
threads = [] |
| 402 |
|
| 403 |
for func in funcs: |
| 404 |
thread = threading.Thread(target=func, args=(barrier,)) |
| 405 |
|
| 406 |
threads.append(thread) |
| 407 |
|
| 408 |
with threading_helper.start_threads(threads): |
| 409 |
pass |
| 410 |
|
| 411 |
check([linecache_get_line] * 20) |
| 412 |
|
| 413 |
|
| 414 |
if __name__ == "__main__": |
| 415 |
unittest.main() |