| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent eaa84ef commit 991f920
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,7 +80,7 @@ def updatecache(filename, module_globals=None): | |||
| 80 | 80 | try: | |
| 81 | 81 | stat = os.stat(fullname) | |
| 82 | 82 | except os.error as msg: | |
| 83 | - basename = os.path.split(filename)[1] | ||
| 83 | + basename = filename | ||
| 84 | 84 | ||
| 85 | 85 | # Try for a __loader__, if available | |
| 86 | 86 | if module_globals and '__loader__' in module_globals: | |
@@ -104,7 +104,10 @@ def updatecache(filename, module_globals=None): | |||
| 104 | 104 | ) | |
| 105 | 105 | return cache[filename][2] | |
| 106 | 106 | ||
| 107 | - # Try looking through the module search path. | ||
| 107 | + # Try looking through the module search path, which is only useful | ||
| 108 | + # when handling a relative filename. | ||
| 109 | + if os.path.isabs(filename): | ||
| 110 | + return [] | ||
| 108 | 111 | ||
| 109 | 112 | for dirname in sys.path: | |
| 110 | 113 | try: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,129 @@ | |||
| 1 | + """ Tests for the linecache module """ | ||
| 2 | + | ||
| 3 | + import linecache | ||
| 4 | + import unittest | ||
| 5 | + import os.path | ||
| 6 | + from test import support | ||
| 7 | + | ||
| 8 | + | ||
| 9 | + FILENAME = linecache.__file__ | ||
| 10 | + INVALID_NAME = '!@$)(!@#_1' | ||
| 11 | + EMPTY = '' | ||
| 12 | + TESTS = 'cjkencodings_test inspect_fodder inspect_fodder2 mapping_tests' | ||
| 13 | + TESTS = TESTS.split() | ||
| 14 | + TEST_PATH = os.path.dirname(support.__file__) | ||
| 15 | + MODULES = "linecache unittest".split() | ||
| 16 | + MODULE_PATH = os.path.dirname(FILENAME) | ||
| 17 | + | ||
| 18 | + SOURCE_1 = ''' | ||
| 19 | + " Docstring " | ||
| 20 | + | ||
| 21 | + def function(): | ||
| 22 | + return result | ||
| 23 | + | ||
| 24 | + ''' | ||
| 25 | + | ||
| 26 | + SOURCE_2 = ''' | ||
| 27 | + def f(): | ||
| 28 | + return 1 + 1 | ||
| 29 | + | ||
| 30 | + a = f() | ||
| 31 | + | ||
| 32 | + ''' | ||
| 33 | + | ||
| 34 | + class LineCacheTests(unittest.TestCase): | ||
| 35 | + | ||
| 36 | + def test_getline(self): | ||
| 37 | + getline = linecache.getline | ||
| 38 | + | ||
| 39 | + # Bad values for line number should return an empty string | ||
| 40 | + self.assertEquals(getline(FILENAME, 2**15), EMPTY) | ||
| 41 | + self.assertEquals(getline(FILENAME, -1), EMPTY) | ||
| 42 | + | ||
| 43 | + # Float values currently raise TypeError, should it? | ||
| 44 | + self.assertRaises(TypeError, getline, FILENAME, 1.1) | ||
| 45 | + | ||
| 46 | + # Bad filenames should return an empty string | ||
| 47 | + self.assertEquals(getline(EMPTY, 1), EMPTY) | ||
| 48 | + self.assertEquals(getline(INVALID_NAME, 1), EMPTY) | ||
| 49 | + | ||
| 50 | + # Check whether lines correspond to those from file iteration | ||
| 51 | + for entry in TESTS: | ||
| 52 | + filename = os.path.join(TEST_PATH, entry) + '.py' | ||
| 53 | + for index, line in enumerate(open(filename)): | ||
| 54 | + self.assertEquals(line, getline(filename, index + 1)) | ||
| 55 | + | ||
| 56 | + # Check module loading | ||
| 57 | + for entry in MODULES: | ||
| 58 | + filename = os.path.join(MODULE_PATH, entry) + '.py' | ||
| 59 | + for index, line in enumerate(open(filename)): | ||
| 60 | + self.assertEquals(line, getline(filename, index + 1)) | ||
| 61 | + | ||
| 62 | + # Check that bogus data isn't returned (issue #1309567) | ||
| 63 | + empty = linecache.getlines('a/b/c/__init__.py') | ||
| 64 | + self.assertEquals(empty, []) | ||
| 65 | + | ||
| 66 | + def test_clearcache(self): | ||
| 67 | + cached = [] | ||
| 68 | + for entry in TESTS: | ||
| 69 | + filename = os.path.join(TEST_PATH, entry) + '.py' | ||
| 70 | + cached.append(filename) | ||
| 71 | + linecache.getline(filename, 1) | ||
| 72 | + | ||
| 73 | + # Are all files cached? | ||
| 74 | + cached_empty = [fn for fn in cached if fn not in linecache.cache] | ||
| 75 | + self.assertEquals(cached_empty, []) | ||
| 76 | + | ||
| 77 | + # Can we clear the cache? | ||
| 78 | + linecache.clearcache() | ||
| 79 | + cached_empty = [fn for fn in cached if fn in linecache.cache] | ||
| 80 | + self.assertEquals(cached_empty, []) | ||
| 81 | + | ||
| 82 | + def test_checkcache(self): | ||
| 83 | + getline = linecache.getline | ||
| 84 | + try: | ||
| 85 | + # Create a source file and cache its contents | ||
| 86 | + source_name = os.path.join(TEST_PATH, 'linecache_test.py') | ||
| 87 | + source = open(source_name, 'w') | ||
| 88 | + source.write(SOURCE_1) | ||
| 89 | + source.close() | ||
| 90 | + getline(source_name, 1) | ||
| 91 | + | ||
| 92 | + # Keep a copy of the old contents | ||
| 93 | + source_list = [] | ||
| 94 | + source = open(source_name) | ||
| 95 | + for index, line in enumerate(source): | ||
| 96 | + self.assertEquals(line, getline(source_name, index + 1)) | ||
| 97 | + source_list.append(line) | ||
| 98 | + source.close() | ||
| 99 | + | ||
| 100 | + source = open(source_name, 'w') | ||
| 101 | + source.write(SOURCE_2) | ||
| 102 | + source.close() | ||
| 103 | + | ||
| 104 | + # Try to update a bogus cache entry | ||
| 105 | + linecache.checkcache('dummy') | ||
| 106 | + | ||
| 107 | + # Check that the cache matches the old contents | ||
| 108 | + for index, line in enumerate(source_list): | ||
| 109 | + self.assertEquals(line, getline(source_name, index + 1)) | ||
| 110 | + | ||
| 111 | + # Update the cache and check whether it matches the new source file | ||
| 112 | + linecache.checkcache(source_name) | ||
| 113 | + source = open(source_name) | ||
| 114 | + for index, line in enumerate(source): | ||
| 115 | + self.assertEquals(line, getline(source_name, index + 1)) | ||
| 116 | + source_list.append(line) | ||
| 117 | + source.close() | ||
| 118 | + | ||
| 119 | + finally: | ||
| 120 | + try: | ||
| 121 | + source.close() | ||
| 122 | + finally: | ||
| 123 | + support.unlink(source_name) | ||
| 124 | + | ||
| 125 | + def test_main(): | ||
| 126 | + support.run_unittest(LineCacheTests) | ||
| 127 | + | ||
| 128 | + if __name__ == "__main__": | ||
| 129 | + test_main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -118,6 +118,9 @@ Installation | |||
| 118 | 118 | Library | |
| 119 | 119 | ------- | |
| 120 | 120 | ||
| 121 | + - Issue #1309567: Fix linecache behavior of stripping subdirectories when | ||
| 122 | + looking for files given by a relative filename. | ||
| 123 | + | ||
| 121 | 124 | - Issue #5923: Update the ``turtle`` module to version 1.1, add two new | |
| 122 | 125 | turtle demos in Demo/turtle. | |
| 123 | 126 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments