mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
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.
This commit is contained in:
parent
43cfd8240c
commit
63b8505281
7 changed files with 381 additions and 315 deletions
|
@ -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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue