bpo-42133: update parts of the stdlib to fall back to __spec__.loader when __loader__ is missing (#22929)

This commit is contained in:
Brett Cannon 2020-11-06 18:45:56 -08:00 committed by GitHub
parent 7c01f1540f
commit 825ac38332
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 14 deletions

View file

@ -707,10 +707,13 @@ def getsourcefile(object):
if os.path.exists(filename):
return filename
# only return a non-existent filename if the module has a PEP 302 loader
if getattr(getmodule(object, filename), '__loader__', None) is not None:
module = getmodule(object, filename)
if getattr(module, '__loader__', None) is not None:
return filename
elif getattr(getattr(module, "__spec__", None), "loader", None) is not None:
return filename
# or it is in the linecache
if filename in linecache.cache:
elif filename in linecache.cache:
return filename
def getabsfile(object, _filename=None):