test_linecache.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """ Tests for the linecache module """
  2. import linecache
  3. import unittest
  4. import os.path
  5. from test import test_support as support
  6. FILENAME = linecache.__file__
  7. INVALID_NAME = '!@$)(!@#_1'
  8. EMPTY = ''
  9. TESTS = 'inspect_fodder inspect_fodder2 mapping_tests'
  10. TESTS = TESTS.split()
  11. TEST_PATH = os.path.dirname(support.__file__)
  12. MODULES = "linecache abc".split()
  13. MODULE_PATH = os.path.dirname(FILENAME)
  14. SOURCE_1 = '''
  15. " Docstring "
  16. def function():
  17. return result
  18. '''
  19. SOURCE_2 = '''
  20. def f():
  21. return 1 + 1
  22. a = f()
  23. '''
  24. SOURCE_3 = '''
  25. def f():
  26. return 3''' # No ending newline
  27. class LineCacheTests(unittest.TestCase):
  28. def test_getline(self):
  29. getline = linecache.getline
  30. # Bad values for line number should return an empty string
  31. self.assertEqual(getline(FILENAME, 2**15), EMPTY)
  32. self.assertEqual(getline(FILENAME, -1), EMPTY)
  33. # Float values currently raise TypeError, should it?
  34. self.assertRaises(TypeError, getline, FILENAME, 1.1)
  35. # Bad filenames should return an empty string
  36. self.assertEqual(getline(EMPTY, 1), EMPTY)
  37. self.assertEqual(getline(INVALID_NAME, 1), EMPTY)
  38. # Check whether lines correspond to those from file iteration
  39. for entry in TESTS:
  40. filename = os.path.join(TEST_PATH, entry) + '.py'
  41. for index, line in enumerate(open(filename)):
  42. self.assertEqual(line, getline(filename, index + 1))
  43. # Check module loading
  44. for entry in MODULES:
  45. filename = os.path.join(MODULE_PATH, entry) + '.py'
  46. for index, line in enumerate(open(filename)):
  47. self.assertEqual(line, getline(filename, index + 1))
  48. # Check that bogus data isn't returned (issue #1309567)
  49. empty = linecache.getlines('a/b/c/__init__.py')
  50. self.assertEqual(empty, [])
  51. def test_no_ending_newline(self):
  52. self.addCleanup(support.unlink, support.TESTFN)
  53. with open(support.TESTFN, "w") as fp:
  54. fp.write(SOURCE_3)
  55. lines = linecache.getlines(support.TESTFN)
  56. self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"])
  57. def test_clearcache(self):
  58. cached = []
  59. for entry in TESTS:
  60. filename = os.path.join(TEST_PATH, entry) + '.py'
  61. cached.append(filename)
  62. linecache.getline(filename, 1)
  63. # Are all files cached?
  64. cached_empty = [fn for fn in cached if fn not in linecache.cache]
  65. self.assertEqual(cached_empty, [])
  66. # Can we clear the cache?
  67. linecache.clearcache()
  68. cached_empty = [fn for fn in cached if fn in linecache.cache]
  69. self.assertEqual(cached_empty, [])
  70. def test_checkcache(self):
  71. getline = linecache.getline
  72. # Create a source file and cache its contents
  73. source_name = support.TESTFN + '.py'
  74. self.addCleanup(support.unlink, source_name)
  75. with open(source_name, 'w') as source:
  76. source.write(SOURCE_1)
  77. getline(source_name, 1)
  78. # Keep a copy of the old contents
  79. source_list = []
  80. with open(source_name) as source:
  81. for index, line in enumerate(source):
  82. self.assertEqual(line, getline(source_name, index + 1))
  83. source_list.append(line)
  84. with open(source_name, 'w') as source:
  85. source.write(SOURCE_2)
  86. # Try to update a bogus cache entry
  87. linecache.checkcache('dummy')
  88. # Check that the cache matches the old contents
  89. for index, line in enumerate(source_list):
  90. self.assertEqual(line, getline(source_name, index + 1))
  91. # Update the cache and check whether it matches the new source file
  92. linecache.checkcache(source_name)
  93. with open(source_name) as source:
  94. for index, line in enumerate(source):
  95. self.assertEqual(line, getline(source_name, index + 1))
  96. source_list.append(line)
  97. def test_memoryerror(self):
  98. lines = linecache.getlines(FILENAME)
  99. self.assertTrue(lines)
  100. def raise_memoryerror(*args, **kwargs):
  101. raise MemoryError
  102. with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
  103. lines2 = linecache.getlines(FILENAME)
  104. self.assertEqual(lines2, lines)
  105. linecache.clearcache()
  106. with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
  107. lines3 = linecache.getlines(FILENAME)
  108. self.assertEqual(lines3, [])
  109. self.assertEqual(linecache.getlines(FILENAME), lines)
  110. def test_main():
  111. support.run_unittest(LineCacheTests)
  112. if __name__ == "__main__":
  113. test_main()