Skip to content

[WIP][DO NOT REVIEW] Add pyc filepath to co_filename #17448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None):
_bootstrap._verbose_message('code object from {!r}', bytecode_path)
if source_path is not None:
_imp._fix_co_filename(code, source_path)
else:
_imp._fix_co_filename(code, bytecode_path)
return code
else:
raise ImportError('Non-code object in {!r}'.format(bytecode_path),
Expand Down
5 changes: 5 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ def getsourcefile(object):
Return None if no way can be identified to get the source.
"""
filename = getfile(object)
orig_filename = filename
all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
if any(filename.endswith(s) for s in all_bytecode_suffixes):
Expand All @@ -704,6 +705,10 @@ def getsourcefile(object):
return None
if os.path.exists(filename):
return filename
# If py file does not exit, check if the original file name exists
if any(orig_filename.endswith(s) for s in all_bytecode_suffixes):
if os.path.exists(orig_filename):
return None
# only return a non-existent filename if the module has a PEP 302 loader
if getattr(getmodule(object, filename), '__loader__', None) is not None:
return filename
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ def test_d_runtime_error(self):
os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
os.remove(bazfn)
rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
self.assertRegex(err, b'File "dinsdale')
self.assertRegex(err, b'Traceback')
self.assertRegex(err, b'baz.pyc')

def test_include_bad_file(self):
rc, out, err = self.assertRunNotOK(
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,8 @@ def test_module_without_source(self):
importlib.invalidate_caches()
mod = self.import_module()
self.assertEqual(mod.module_filename, pyc_file)
self.assertEqual(mod.code_filename, target)
self.assertEqual(mod.func_filename, target)
self.assertEqual(mod.code_filename, pyc_file)
self.assertEqual(mod.func_filename, pyc_file)

def test_foreign_code(self):
py_compile.compile(self.file_name)
Expand Down
Loading