summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2016-01-15 21:33:03 (GMT)
committerBrett Cannon <brett@python.org>2016-01-15 21:33:03 (GMT)
commit63b8505281f9c26f7304b3e00d658b429b862d5b (patch)
tree07a4d20ed327d8493aa7105bec9ca1d598cc6b7c /Lib/importlib
parent43cfd8240c274b2f4cf0588f3defae83e7379a55 (diff)
downloadcpython-63b8505281f9c26f7304b3e00d658b429b862d5b.zip
cpython-63b8505281f9c26f7304b3e00d658b429b862d5b.tar.gz
cpython-63b8505281f9c26f7304b3e00d658b429b862d5b.tar.bz2
Issue #25791: Raise an ImportWarning when __spec__ or __package__ are
not defined for a relative import. This is the start of work to try and clean up import semantics to rely more on a module's spec than on the myriad attributes that get set on a module. Thanks to Rose Ames for the patch.
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_bootstrap.py6
1 files changed, 6 insertions, 0 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 6f62bb3..9adcf7b 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1032,8 +1032,14 @@ def _calc___package__(globals):
to represent that its proper value is unknown.
"""
+ spec = globals.get('__spec__')
+ if spec is not None:
+ return spec.parent
package = globals.get('__package__')
if package is None:
+ _warnings.warn("can't resolve package from __spec__ or __package__, "
+ "falling back on __name__ and __path__",
+ ImportWarning, stacklevel=3)
package = globals['__name__']
if '__path__' not in globals:
package = package.rpartition('.')[0]