| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 80a0c7f commit f088e5e
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -346,7 +346,7 @@ def format_stack_entry(self, frame_lineno, lprefix=': '): | |||
| 346 | 346 | rv = frame.f_locals['__return__'] | |
| 347 | 347 | s = s + '->' | |
| 348 | 348 | s = s + reprlib.repr(rv) | |
| 349 | - line = linecache.getline(filename, lineno) | ||
| 349 | + line = linecache.getline(filename, lineno, frame.f_globals) | ||
| 350 | 350 | if line: s = s + lprefix + line.strip() | |
| 351 | 351 | return s | |
| 352 | 352 | ||
@@ -588,7 +588,7 @@ def user_line(self, frame): | |||
| 588 | 588 | name = frame.f_code.co_name | |
| 589 | 589 | if not name: name = '???' | |
| 590 | 590 | fn = self.canonic(frame.f_code.co_filename) | |
| 591 | - line = linecache.getline(fn, frame.f_lineno) | ||
| 591 | + line = linecache.getline(fn, frame.f_lineno, frame.f_globals) | ||
| 592 | 592 | print('+++', fn, frame.f_lineno, name, ':', line.strip()) | |
| 593 | 593 | def user_return(self, frame, retval): | |
| 594 | 594 | print('+++ return', retval) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,21 +89,20 @@ def updatecache(filename, module_globals=None): | |||
| 89 | 89 | get_source = getattr(loader, 'get_source', None) | |
| 90 | 90 | ||
| 91 | 91 | if name and get_source: | |
| 92 | - if basename.startswith(name.split('.')[-1]+'.'): | ||
| 93 | - try: | ||
| 94 | - data = get_source(name) | ||
| 95 | - except (ImportError, IOError): | ||
| 96 | - pass | ||
| 97 | - else: | ||
| 98 | - if data is None: | ||
| 99 | - # No luck, the PEP302 loader cannot find the source | ||
| 100 | - # for this module. | ||
| 101 | - return [] | ||
| 102 | - cache[filename] = ( | ||
| 103 | - len(data), None, | ||
| 104 | - [line+'\n' for line in data.splitlines()], fullname | ||
| 105 | - ) | ||
| 106 | - return cache[filename][2] | ||
| 92 | + try: | ||
| 93 | + data = get_source(name) | ||
| 94 | + except (ImportError, IOError): | ||
| 95 | + pass | ||
| 96 | + else: | ||
| 97 | + if data is None: | ||
| 98 | + # No luck, the PEP302 loader cannot find the source | ||
| 99 | + # for this module. | ||
| 100 | + return [] | ||
| 101 | + cache[filename] = ( | ||
| 102 | + len(data), None, | ||
| 103 | + [line+'\n' for line in data.splitlines()], fullname | ||
| 104 | + ) | ||
| 105 | + return cache[filename][2] | ||
| 107 | 106 | ||
| 108 | 107 | # Try looking through the module search path. | |
| 109 | 108 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -438,7 +438,7 @@ def checkline(self, filename, lineno): | |||
| 438 | 438 | Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank | |
| 439 | 439 | line or EOF). Warning: testing is not comprehensive. | |
| 440 | 440 | """ | |
| 441 | - line = linecache.getline(filename, lineno) | ||
| 441 | + line = linecache.getline(filename, lineno, self.curframe.f_globals) | ||
| 442 | 442 | if not line: | |
| 443 | 443 | print('End of file', file=self.stdout) | |
| 444 | 444 | return 0 | |
@@ -768,7 +768,7 @@ def do_list(self, arg): | |||
| 768 | 768 | breaklist = self.get_file_breaks(filename) | |
| 769 | 769 | try: | |
| 770 | 770 | for lineno in range(first, last+1): | |
| 771 | - line = linecache.getline(filename, lineno) | ||
| 771 | + line = linecache.getline(filename, lineno, self.curframe.f_globals) | ||
| 772 | 772 | if not line: | |
| 773 | 773 | print('[EOF]', file=self.stdout) | |
| 774 | 774 | break | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,13 +65,14 @@ def _run_module_code(code, init_globals=None, | |||
| 65 | 65 | ||
| 66 | 66 | # This helper is needed due to a missing component in the PEP 302 | |
| 67 | 67 | # loader protocol (specifically, "get_filename" is non-standard) | |
| 68 | + # Since we can't introduce new features in maintenance releases, | ||
| 69 | + # support was added to zipimporter under the name '_get_filename' | ||
| 68 | 70 | def _get_filename(loader, mod_name): | |
| 69 | - try: | ||
| 70 | - get_filename = loader.get_filename | ||
| 71 | - except AttributeError: | ||
| 72 | - return None | ||
| 73 | - else: | ||
| 74 | - return get_filename(mod_name) | ||
| 71 | + for attr in ("get_filename", "_get_filename"): | ||
| 72 | + meth = getattr(loader, attr, None) | ||
| 73 | + if meth is not None: | ||
| 74 | + return meth(mod_name) | ||
| 75 | + return None | ||
| 75 | 76 | ||
| 76 | 77 | # Helper to get the loader, code and filename for a module | |
| 77 | 78 | def _get_module_details(mod_name): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,36 +75,66 @@ def _compile_test_script(script_name): | |||
| 75 | 75 | compiled_name = script_name + 'o' | |
| 76 | 76 | return compiled_name | |
| 77 | 77 | ||
| 78 | - def _make_test_zip(zip_dir, zip_basename, script_name): | ||
| 78 | + def _make_test_zip(zip_dir, zip_basename, script_name, name_in_zip=None): | ||
| 79 | 79 | zip_filename = zip_basename+os.path.extsep+"zip" | |
| 80 | 80 | zip_name = os.path.join(zip_dir, zip_filename) | |
| 81 | 81 | zip_file = zipfile.ZipFile(zip_name, 'w') | |
| 82 | - zip_file.write(script_name, os.path.basename(script_name)) | ||
| 82 | + if name_in_zip is None: | ||
| 83 | + name_in_zip = os.path.basename(script_name) | ||
| 84 | + zip_file.write(script_name, name_in_zip) | ||
| 83 | 85 | zip_file.close() | |
| 84 | - # if verbose: | ||
| 86 | + #if verbose: | ||
| 85 | 87 | # zip_file = zipfile.ZipFile(zip_name, 'r') | |
| 86 | 88 | # print("Contents of %r:" % zip_name) | |
| 87 | 89 | # zip_file.printdir() | |
| 88 | 90 | # zip_file.close() | |
| 89 | - return zip_name | ||
| 91 | + return zip_name, os.path.join(zip_name, name_in_zip) | ||
| 90 | 92 | ||
| 91 | 93 | def _make_test_pkg(pkg_dir): | |
| 92 | 94 | os.mkdir(pkg_dir) | |
| 93 | 95 | _make_test_script(pkg_dir, '__init__', '') | |
| 94 | 96 | ||
| 97 | + def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, | ||
| 98 | + source=test_source, depth=1): | ||
| 99 | + init_name = _make_test_script(zip_dir, '__init__', '') | ||
| 100 | + init_basename = os.path.basename(init_name) | ||
| 101 | + script_name = _make_test_script(zip_dir, script_basename, source) | ||
| 102 | + pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] | ||
| 103 | + script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) | ||
| 104 | + zip_filename = zip_basename+os.extsep+'zip' | ||
| 105 | + zip_name = os.path.join(zip_dir, zip_filename) | ||
| 106 | + zip_file = zipfile.ZipFile(zip_name, 'w') | ||
| 107 | + for name in pkg_names: | ||
| 108 | + init_name_in_zip = os.path.join(name, init_basename) | ||
| 109 | + zip_file.write(init_name, init_name_in_zip) | ||
| 110 | + zip_file.write(script_name, script_name_in_zip) | ||
| 111 | + zip_file.close() | ||
| 112 | + os.unlink(init_name) | ||
| 113 | + os.unlink(script_name) | ||
| 114 | + #if verbose: | ||
| 115 | + # zip_file = zipfile.ZipFile(zip_name, 'r') | ||
| 116 | + # print 'Contents of %r:' % zip_name | ||
| 117 | + # zip_file.printdir() | ||
| 118 | + # zip_file.close() | ||
| 119 | + return zip_name, os.path.join(zip_name, script_name_in_zip) | ||
| 120 | + | ||
| 95 | 121 | # There's no easy way to pass the script directory in to get | |
| 96 | 122 | # -m to work (avoiding that is the whole point of making | |
| 97 | 123 | # directories and zipfiles executable!) | |
| 98 | 124 | # So we fake it for testing purposes with a custom launch script | |
| 99 | 125 | launch_source = """\ | |
| 100 | 126 | import sys, os.path, runpy | |
| 101 | - sys.path[0:0] = os.path.dirname(__file__) | ||
| 127 | + sys.path.insert(0, %s) | ||
| 102 | 128 | runpy._run_module_as_main(%r) | |
| 103 | 129 | """ | |
| 104 | 130 | ||
| 105 | - def _make_launch_script(script_dir, script_basename, module_name): | ||
| 106 | - return _make_test_script(script_dir, script_basename, | ||
| 107 | - launch_source % module_name) | ||
| 131 | + def _make_launch_script(script_dir, script_basename, module_name, path=None): | ||
| 132 | + if path is None: | ||
| 133 | + path = "os.path.dirname(__file__)" | ||
| 134 | + else: | ||
| 135 | + path = repr(path) | ||
| 136 | + source = launch_source % (path, module_name) | ||
| 137 | + return _make_test_script(script_dir, script_basename, source) | ||
| 108 | 138 | ||
| 109 | 139 | class CmdLineTest(unittest.TestCase): | |
| 110 | 140 | def _check_script(self, script_name, expected_file, | |
@@ -155,24 +185,35 @@ def test_directory_compiled(self): | |||
| 155 | 185 | def test_zipfile(self): | |
| 156 | 186 | with temp_dir() as script_dir: | |
| 157 | 187 | script_name = _make_test_script(script_dir, '__main__') | |
| 158 | - zip_name = _make_test_zip(script_dir, 'test_zip', script_name) | ||
| 159 | - self._check_script(zip_name, None, zip_name, '') | ||
| 188 | + zip_name, run_name = _make_test_zip(script_dir, 'test_zip', script_name) | ||
| 189 | + self._check_script(zip_name, run_name, zip_name, '') | ||
| 160 | 190 | ||
| 161 | 191 | def test_zipfile_compiled(self): | |
| 162 | 192 | with temp_dir() as script_dir: | |
| 163 | 193 | script_name = _make_test_script(script_dir, '__main__') | |
| 164 | 194 | compiled_name = _compile_test_script(script_name) | |
| 165 | - zip_name = _make_test_zip(script_dir, 'test_zip', compiled_name) | ||
| 166 | - self._check_script(zip_name, None, zip_name, '') | ||
| 195 | + zip_name, run_name = _make_test_zip(script_dir, 'test_zip', compiled_name) | ||
| 196 | + self._check_script(zip_name, run_name, zip_name, '') | ||
| 167 | 197 | ||
| 168 | 198 | def test_module_in_package(self): | |
| 169 | 199 | with temp_dir() as script_dir: | |
| 170 | 200 | pkg_dir = os.path.join(script_dir, 'test_pkg') | |
| 171 | 201 | _make_test_pkg(pkg_dir) | |
| 172 | 202 | script_name = _make_test_script(pkg_dir, 'script') | |
| 173 | 203 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') | |
| 174 | - self._check_script(launch_name, script_name, | ||
| 175 | - script_name, 'test_pkg') | ||
| 204 | + self._check_script(launch_name, script_name, script_name, 'test_pkg') | ||
| 205 | + | ||
| 206 | + def test_module_in_package_in_zipfile(self): | ||
| 207 | + with temp_dir() as script_dir: | ||
| 208 | + zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') | ||
| 209 | + launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name) | ||
| 210 | + self._check_script(launch_name, run_name, run_name, 'test_pkg') | ||
| 211 | + | ||
| 212 | + def test_module_in_subpackage_in_zipfile(self): | ||
| 213 | + with temp_dir() as script_dir: | ||
| 214 | + zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) | ||
| 215 | + launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name) | ||
| 216 | + self._check_script(launch_name, run_name, run_name, 'test_pkg.test_pkg') | ||
| 176 | 217 | ||
| 177 | 218 | ||
| 178 | 219 | def test_main(): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,9 @@ | |||
| 6 | 6 | import doctest | |
| 7 | 7 | import warnings | |
| 8 | 8 | ||
| 9 | + # NOTE: There are some additional tests relating to interaction with | ||
| 10 | + # zipimport in the test_zipimport_support test module. | ||
| 11 | + | ||
| 9 | 12 | ###################################################################### | |
| 10 | 13 | ## Sample Objects (used by test cases) | |
| 11 | 14 | ###################################################################### | |
@@ -369,7 +372,7 @@ def test_DocTestFinder(): r""" | |||
| 369 | 372 | >>> tests = finder.find(sample_func) | |
| 370 | 373 | ||
| 371 | 374 | >>> print(tests) # doctest: +ELLIPSIS | |
| 372 | - [<DocTest sample_func from ...:13 (1 example)>] | ||
| 375 | + [<DocTest sample_func from ...:16 (1 example)>] | ||
| 373 | 376 | ||
| 374 | 377 | The exact name depends on how test_doctest was invoked, so allow for | |
| 375 | 378 | leading path components. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,9 @@ | |||
| 18 | 18 | # getclasstree, getargspec, getargvalues, formatargspec, formatargvalues, | |
| 19 | 19 | # currentframe, stack, trace, isdatadescriptor | |
| 20 | 20 | ||
| 21 | + # NOTE: There are some additional tests relating to interaction with | ||
| 22 | + # zipimport in the test_zipimport_support test module. | ||
| 23 | + | ||
| 21 | 24 | modfile = mod.__file__ | |
| 22 | 25 | if modfile.endswith(('c', 'o')): | |
| 23 | 26 | modfile = modfile[:-1] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -211,16 +211,24 @@ def testZipImporterMethods(self): | |||
| 211 | 211 | zi = zipimport.zipimporter(TEMP_ZIP) | |
| 212 | 212 | self.assertEquals(zi.archive, TEMP_ZIP) | |
| 213 | 213 | self.assertEquals(zi.is_package(TESTPACK), True) | |
| 214 | - zi.load_module(TESTPACK) | ||
| 214 | + mod = zi.load_module(TESTPACK) | ||
| 215 | + self.assertEquals(zi._get_filename(TESTPACK), mod.__file__) | ||
| 215 | 216 | ||
| 216 | 217 | self.assertEquals(zi.is_package(packdir + '__init__'), False) | |
| 217 | 218 | self.assertEquals(zi.is_package(packdir + TESTPACK2), True) | |
| 218 | 219 | self.assertEquals(zi.is_package(packdir2 + TESTMOD), False) | |
| 219 | 220 | ||
| 220 | - mod_name = packdir2 + TESTMOD | ||
| 221 | - mod = __import__(module_path_to_dotted_name(mod_name)) | ||
| 221 | + mod_path = packdir2 + TESTMOD | ||
| 222 | + mod_name = module_path_to_dotted_name(mod_path) | ||
| 223 | + pkg = __import__(mod_name) | ||
| 224 | + mod = sys.modules[mod_name] | ||
| 222 | 225 | self.assertEquals(zi.get_source(TESTPACK), None) | |
| 223 | - self.assertEquals(zi.get_source(mod_name), None) | ||
| 226 | + self.assertEquals(zi.get_source(mod_path), None) | ||
| 227 | + self.assertEquals(zi._get_filename(mod_path), mod.__file__) | ||
| 228 | + # To pass in the module name instead of the path, we must use the right importer | ||
| 229 | + loader = mod.__loader__ | ||
| 230 | + self.assertEquals(loader.get_source(mod_name), None) | ||
| 231 | + self.assertEquals(loader._get_filename(mod_name), mod.__file__) | ||
| 224 | 232 | ||
| 225 | 233 | # test prefix and archivepath members | |
| 226 | 234 | zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK) | |
@@ -248,15 +256,23 @@ def testZipImporterMethodsInSubDirectory(self): | |||
| 248 | 256 | self.assertEquals(zi.archive, TEMP_ZIP) | |
| 249 | 257 | self.assertEquals(zi.prefix, packdir) | |
| 250 | 258 | self.assertEquals(zi.is_package(TESTPACK2), True) | |
| 251 | - zi.load_module(TESTPACK2) | ||
| 259 | + mod = zi.load_module(TESTPACK2) | ||
| 260 | + self.assertEquals(zi._get_filename(TESTPACK2), mod.__file__) | ||
| 252 | 261 | ||
| 253 | 262 | self.assertEquals(zi.is_package(TESTPACK2 + os.sep + '__init__'), False) | |
| 254 | 263 | self.assertEquals(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False) | |
| 255 | 264 | ||
| 256 | - mod_name = TESTPACK2 + os.sep + TESTMOD | ||
| 257 | - mod = __import__(module_path_to_dotted_name(mod_name)) | ||
| 265 | + mod_path = TESTPACK2 + os.sep + TESTMOD | ||
| 266 | + mod_name = module_path_to_dotted_name(mod_path) | ||
| 267 | + pkg = __import__(mod_name) | ||
| 268 | + mod = sys.modules[mod_name] | ||
| 258 | 269 | self.assertEquals(zi.get_source(TESTPACK2), None) | |
| 259 | - self.assertEquals(zi.get_source(mod_name), None) | ||
| 270 | + self.assertEquals(zi.get_source(mod_path), None) | ||
| 271 | + self.assertEquals(zi._get_filename(mod_path), mod.__file__) | ||
| 272 | + # To pass in the module name instead of the path, we must use the right importer | ||
| 273 | + loader = mod.__loader__ | ||
| 274 | + self.assertEquals(loader.get_source(mod_name), None) | ||
| 275 | + self.assertEquals(loader._get_filename(mod_name), mod.__file__) | ||
| 260 | 276 | finally: | |
| 261 | 277 | z.close() | |
| 262 | 278 | os.remove(TEMP_ZIP) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments