Nick Coghlan <[email protected]> added the comment:
As a concrete proposal for 3.8, how about:
1. Add the following utility functions to importlib.util (names adjusted to
match PEP 451 rather than my initial suggestions above):
def get_location(module):
try:
return module.__file__
except AttributeError:
pass
spec = module.__spec__
if spec.has_location:
return spec.origin
return None
def get_cached_location(module):
try:
return module.__cached__
except AttributeError:
pass
spec = module.__spec__
if spec.has_location:
return spec.cached
return None
def get_loader(module):
try:
return module.__loader__
except AttributeError:
pass
return module.__spec__.loader
2. Explicitly make __file__/__cached__/__loader__ optional, and require 3rd
party tools to use the relevant `importlib.util.get_*` APIs when they're
defined rather than accessing the dunder-attributes directly.
I think those 3 are clear, but the right API for replacing direct access to
__package__ is less clear, since there are two cases for querying the package
name: whether you want actual packages to return their own name, or whether you
want the name of their parent.
If we decide the relevant use case is "get the base package name for relative
imports", then the following helper API may work:
def get_base_package_name(module):
"""Get the package name to pass to resolve_name() for this module"""
try:
return module.__package__
except AttributeError:
pass
modname = module.__name__
try:
path = module.__path__
except AttributeError:
pass
else:
return modname
return module.__spec__.parent
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue33277>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com