diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -563,21 +563,23 @@ class ModuleFinder: % (original_filename,)) self.processed_paths.append(original_filename) consts = list(co.co_consts) for i in range(len(consts)): if isinstance(consts[i], type(co)): consts[i] = self.replace_paths_in_code(consts[i]) - return types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize, - co.co_flags, co.co_code, tuple(consts), co.co_names, - co.co_varnames, new_filename, co.co_name, - co.co_firstlineno, co.co_lnotab, - co.co_freevars, co.co_cellvars) + return types.CodeType(co.co_argcount, co.co_kwonlyargcount, + co.co_nlocals, co.co_stacksize, + co.co_flags, co.co_code, tuple(consts), + co.co_names, co.co_varnames, + new_filename, co.co_name, + co.co_firstlineno, co.co_lnotab, + co.co_freevars, co.co_cellvars) def test(): # Parse command line import getopt try: opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:") except getopt.error as msg: diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py --- a/Lib/test/test_modulefinder.py +++ b/Lib/test/test_modulefinder.py @@ -240,21 +240,22 @@ def create_package(source): ofi.close() ofi = open_file(os.path.join(TEST_DIR, line.strip())) finally: if ofi: ofi.close() class ModuleFinderTest(unittest.TestCase): - def _do_test(self, info, report=False): + def _do_test(self, info, report=False, debug=0, replace_paths=[]): import_this, modules, missing, maybe_missing, source = info create_package(source) try: - mf = modulefinder.ModuleFinder(path=TEST_PATH) + mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug, + replace_paths=replace_paths) mf.import_hook(import_this) if report: mf.report() ## # This wouldn't work in general when executed several times: ## opath = sys.path[:] ## sys.path = TEST_PATH ## try: ## __import__(import_this) @@ -303,14 +304,24 @@ class ModuleFinderTest(unittest.TestCase source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0] bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0] with open_file(source_path) as file: file.write('testing_modulefinder = True\n') py_compile.compile(source_path, cfile=bytecode_path) os.remove(source_path) self._do_test(bytecode_test) + def test_replace_paths(self): + old_path = os.path.join(TEST_DIR, 'a', 'module.py') + new_path = os.path.join(TEST_DIR, 'a', 'spam.py') + with support.captured_stdout() as output: + self._do_test(maybe_test, debug=2, + replace_paths=[(old_path, new_path)]) + output = output.getvalue() + expected = "co_filename '%s' changed to '%s'" % (old_path, new_path) + self.assertIn(expected, output) + def test_main(): support.run_unittest(ModuleFinderTest) if __name__ == "__main__": unittest.main()