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

@ -165,9 +165,14 @@ def lazycache(filename, module_globals):
if not filename or (filename.startswith('<') and filename.endswith('>')):
return False
# Try for a __loader__, if available
if module_globals and '__loader__' in module_globals:
name = module_globals.get('__name__')
loader = module_globals['__loader__']
if module_globals and '__name__' in module_globals:
name = module_globals['__name__']
if (loader := module_globals.get('__loader__')) is None:
if spec := module_globals.get('__spec__'):
try:
loader = spec.loader
except AttributeError:
pass
get_source = getattr(loader, 'get_source', None)
if name and get_source: