Issue #25791: Warn when __package__ != __spec__.parent.

In a previous change, __spec__.parent was prioritized over
__package__. That is a backwards-compatibility break, but we do
eventually want __spec__ to be the ground truth for module details. So
this change reverts the change in semantics and instead raises an
ImportWarning when __package__ != __spec__.parent to give people time
to adjust to using spec objects.
This commit is contained in:
Brett Cannon 2016-01-22 15:25:50 -08:00
parent 52c854a838
commit 849113af6b
8 changed files with 184 additions and 121 deletions

View File

@ -554,20 +554,30 @@ the module.
details.
This attribute is used instead of ``__name__`` to calculate explicit
relative imports for main modules -- as defined in :pep:`366` --
when ``__spec__`` is not defined.
relative imports for main modules, as defined in :pep:`366`. It is
expected to have the same value as ``__spec__.parent``.
.. versionchanged:: 3.6
The value of ``__package__`` is expected to be the same as
``__spec__.parent``.
.. attribute:: __spec__
The ``__spec__`` attribute must be set to the module spec that was
used when importing the module. This is used primarily for
introspection and during reloading. Setting ``__spec__``
used when importing the module. Setting ``__spec__``
appropriately applies equally to :ref:`modules initialized during
interpreter startup <programs>`. The one exception is ``__main__``,
where ``__spec__`` is :ref:`set to None in some cases <main_spec>`.
When ``__package__`` is not defined, ``__spec__.parent`` is used as
a fallback.
.. versionadded:: 3.4
.. versionchanged:: 3.6
``__spec__.parent`` is used as a fallback when ``__package__`` is
not defined.
.. attribute:: __path__
If the module is a package (either regular or namespace), the module

View File

@ -274,9 +274,9 @@ Changes in the Python API
:mod:`wave`. This means they will export new symbols when ``import *``
is used. See :issue:`23883`.
* When performing a relative import, ``__spec__.parent`` is used
is ``__spec__`` is defined instead of ``__package__``.
(Contributed by Rose Ames in :issue:`25791`.)
* When performing a relative import, if ``__package__`` does not compare equal
to ``__spec__.parent`` then :exc:`ImportWarning` is raised.
(Contributed by Brett Cannon in :issue:`25791`.)
Changes in the C API

View File

@ -1032,11 +1032,17 @@ 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:
spec = globals.get('__spec__')
if package is not None:
if spec is not None and package != spec.parent:
_warnings.warn("__package__ != __spec__.parent "
f"({package!r} != {spec.parent!r})",
ImportWarning, stacklevel=3)
return package
elif spec is not None:
return spec.parent
else:
_warnings.warn("can't resolve package from __spec__ or __package__, "
"falling back on __name__ and __path__",
ImportWarning, stacklevel=3)

View File

@ -5,6 +5,7 @@ of using the typical __path__/__name__ test).
"""
import unittest
import warnings
from .. import util
@ -38,8 +39,8 @@ class Using__package__:
with util.import_state(meta_path=[importer]):
self.__import__('pkg.fake')
module = self.__import__('',
globals=globals_,
fromlist=['attr'], level=2)
globals=globals_,
fromlist=['attr'], level=2)
return module
def test_using___package__(self):
@ -49,7 +50,10 @@ class Using__package__:
def test_using___name__(self):
# [__name__]
module = self.import_module({'__name__': 'pkg.fake', '__path__': []})
with warnings.catch_warnings():
warnings.simplefilter("ignore")
module = self.import_module({'__name__': 'pkg.fake',
'__path__': []})
self.assertEqual(module.__name__, 'pkg')
def test_warn_when_using___name__(self):
@ -58,14 +62,22 @@ class Using__package__:
def test_None_as___package__(self):
# [None]
module = self.import_module({
'__name__': 'pkg.fake', '__path__': [], '__package__': None })
with warnings.catch_warnings():
warnings.simplefilter("ignore")
module = self.import_module({
'__name__': 'pkg.fake', '__path__': [], '__package__': None })
self.assertEqual(module.__name__, 'pkg')
def test_prefers___spec__(self):
globals = {'__spec__': FakeSpec()}
with self.assertRaises(SystemError):
self.__import__('', globals, {}, ['relimport'], 1)
def test_spec_fallback(self):
# If __package__ isn't defined, fall back on __spec__.parent.
module = self.import_module({'__spec__': FakeSpec('pkg.fake')})
self.assertEqual(module.__name__, 'pkg')
def test_warn_when_package_and_spec_disagree(self):
# Raise an ImportWarning if __package__ != __spec__.parent.
with self.assertWarns(ImportWarning):
self.import_module({'__package__': 'pkg.fake',
'__spec__': FakeSpec('pkg.fakefake')})
def test_bad__package__(self):
globals = {'__package__': '<not real>'}
@ -79,7 +91,8 @@ class Using__package__:
class FakeSpec:
parent = '<fake>'
def __init__(self, parent):
self.parent = parent
class Using__package__PEP302(Using__package__):

View File

@ -2,6 +2,8 @@
from .. import util
import sys
import unittest
import warnings
class RelativeImports:
@ -65,9 +67,11 @@ class RelativeImports:
uncache_names.append(name[:-len('.__init__')])
with util.mock_spec(*create) as importer:
with util.import_state(meta_path=[importer]):
for global_ in globals_:
with util.uncache(*uncache_names):
callback(global_)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
for global_ in globals_:
with util.uncache(*uncache_names):
callback(global_)
def test_module_from_module(self):
@ -204,8 +208,10 @@ class RelativeImports:
def test_relative_import_no_globals(self):
# No globals for a relative import is an error.
with self.assertRaises(KeyError):
self.__import__('sys', level=1)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
with self.assertRaises(KeyError):
self.__import__('sys', level=1)
(Frozen_RelativeImports,

View File

@ -26,8 +26,8 @@ Core and Builtins
Python 3.5.1 to hide the exact implementation of atomic C types, to avoid
compiler issues.
- Issue #25791: Trying to resolve a relative import without __spec__ or
__package__ defined now raises an ImportWarning
- Issue #25791: If __package__ != __spec__.parent or if neither __package__ or
__spec__ are defined then ImportWarning is raised.
- Issue #25731: Fix set and deleting __new__ on a class.

View File

@ -1415,60 +1415,79 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
goto error;
}
else if (level > 0) {
package = _PyDict_GetItemId(globals, &PyId___package__);
spec = _PyDict_GetItemId(globals, &PyId___spec__);
if (spec != NULL) {
package = PyObject_GetAttrString(spec, "parent");
}
if (package != NULL && package != Py_None) {
Py_INCREF(package);
if (!PyUnicode_Check(package)) {
PyErr_SetString(PyExc_TypeError, "package must be a string");
goto error;
}
else if (spec != NULL) {
int equal;
PyObject *parent = PyObject_GetAttrString(spec, "parent");
if (parent == NULL) {
goto error;
}
equal = PyObject_RichCompareBool(package, parent, Py_EQ);
Py_DECREF(parent);
if (equal < 0) {
goto error;
}
else if (equal == 0) {
if (PyErr_WarnEx(PyExc_ImportWarning,
"__package__ != __spec__.parent", 1) < 0) {
goto error;
}
}
}
}
else if (spec != NULL) {
package = PyObject_GetAttrString(spec, "parent");
if (package == NULL) {
goto error;
}
else if (!PyUnicode_Check(package)) {
PyErr_SetString(PyExc_TypeError,
"__spec__.parent must be a string");
goto error;
}
}
else {
package = _PyDict_GetItemId(globals, &PyId___package__);
if (package != NULL && package != Py_None) {
Py_INCREF(package);
if (!PyUnicode_Check(package)) {
PyErr_SetString(PyExc_TypeError, "package must be a string");
goto error;
}
if (PyErr_WarnEx(PyExc_ImportWarning,
"can't resolve package from __spec__ or __package__, "
"falling back on __name__ and __path__", 1) < 0) {
goto error;
}
else {
if (PyErr_WarnEx(PyExc_ImportWarning,
"can't resolve package from __spec__ or __package__, "
"falling back on __name__ and __path__", 1) < 0) {
package = _PyDict_GetItemId(globals, &PyId___name__);
if (package == NULL) {
PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
goto error;
}
Py_INCREF(package);
if (!PyUnicode_Check(package)) {
PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
goto error;
}
if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
PyObject *partition = NULL;
PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
if (borrowed_dot == NULL) {
goto error;
}
package = _PyDict_GetItemId(globals, &PyId___name__);
if (package == NULL) {
PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
partition = PyUnicode_RPartition(package, borrowed_dot);
Py_DECREF(package);
if (partition == NULL) {
goto error;
}
package = PyTuple_GET_ITEM(partition, 0);
Py_INCREF(package);
if (!PyUnicode_Check(package)) {
PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
goto error;
}
if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
PyObject *partition = NULL;
PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
if (borrowed_dot == NULL) {
goto error;
}
partition = PyUnicode_RPartition(package, borrowed_dot);
Py_DECREF(package);
if (partition == NULL) {
goto error;
}
package = PyTuple_GET_ITEM(partition, 0);
Py_INCREF(package);
Py_DECREF(partition);
}
Py_DECREF(partition);
}
}

View File

@ -1787,43 +1787,52 @@ const unsigned char _Py_M__importlib[] = {
115,116,228,3,0,0,115,34,0,0,0,0,10,15,1,12,
1,12,1,13,1,15,1,16,1,13,1,15,1,21,1,3,
1,17,1,18,4,21,1,15,1,3,1,26,1,114,195,0,
0,0,99,1,0,0,0,0,0,0,0,3,0,0,0,5,
0,0,0,67,0,0,0,115,128,0,0,0,124,0,0,106,
0,0,100,1,0,131,1,0,125,1,0,124,1,0,100,2,
0,107,9,0,114,34,0,124,1,0,106,1,0,83,124,0,
0,106,0,0,100,3,0,131,1,0,125,2,0,124,2,0,
100,2,0,107,8,0,114,124,0,116,2,0,106,3,0,100,
4,0,116,4,0,100,5,0,100,6,0,131,2,1,1,124,
0,0,100,7,0,25,125,2,0,100,8,0,124,0,0,107,
7,0,114,124,0,124,2,0,106,5,0,100,9,0,131,1,
0,100,10,0,25,125,2,0,124,2,0,83,41,11,122,167,
67,97,108,99,117,108,97,116,101,32,119,104,97,116,32,95,
95,112,97,99,107,97,103,101,95,95,32,115,104,111,117,108,
100,32,98,101,46,10,10,32,32,32,32,95,95,112,97,99,
107,97,103,101,95,95,32,105,115,32,110,111,116,32,103,117,
97,114,97,110,116,101,101,100,32,116,111,32,98,101,32,100,
101,102,105,110,101,100,32,111,114,32,99,111,117,108,100,32,
98,101,32,115,101,116,32,116,111,32,78,111,110,101,10,32,
32,32,32,116,111,32,114,101,112,114,101,115,101,110,116,32,
116,104,97,116,32,105,116,115,32,112,114,111,112,101,114,32,
118,97,108,117,101,32,105,115,32,117,110,107,110,111,119,110,
46,10,10,32,32,32,32,114,95,0,0,0,78,114,134,0,
0,0,122,89,99,97,110,39,116,32,114,101,115,111,108,118,
101,32,112,97,99,107,97,103,101,32,102,114,111,109,32,95,
95,115,112,101,99,95,95,32,111,114,32,95,95,112,97,99,
107,97,103,101,95,95,44,32,102,97,108,108,105,110,103,32,
98,97,99,107,32,111,110,32,95,95,110,97,109,101,95,95,
32,97,110,100,32,95,95,112,97,116,104,95,95,114,140,0,
0,0,233,3,0,0,0,114,1,0,0,0,114,131,0,0,
0,114,121,0,0,0,114,33,0,0,0,41,6,114,42,0,
0,0,114,123,0,0,0,114,142,0,0,0,114,143,0,0,
0,114,176,0,0,0,114,122,0,0,0,41,3,218,7,103,
108,111,98,97,108,115,114,88,0,0,0,114,170,0,0,0,
114,10,0,0,0,114,10,0,0,0,114,11,0,0,0,218,
17,95,99,97,108,99,95,95,95,112,97,99,107,97,103,101,
95,95,4,4,0,0,115,22,0,0,0,0,7,15,1,12,
1,7,1,15,1,12,1,9,2,13,1,10,1,12,1,19,
1,114,198,0,0,0,99,5,0,0,0,0,0,0,0,9,
0,0,99,1,0,0,0,0,0,0,0,3,0,0,0,7,
0,0,0,67,0,0,0,115,214,0,0,0,124,0,0,106,
0,0,100,1,0,131,1,0,125,1,0,124,0,0,106,0,
0,100,2,0,131,1,0,125,2,0,124,1,0,100,3,0,
107,9,0,114,128,0,124,2,0,100,3,0,107,9,0,114,
124,0,124,1,0,124,2,0,106,1,0,107,3,0,114,124,
0,116,2,0,106,3,0,100,4,0,106,4,0,100,5,0,
124,1,0,155,2,0,100,6,0,124,2,0,106,1,0,155,
2,0,100,7,0,103,5,0,131,1,0,116,5,0,100,8,
0,100,9,0,131,2,1,1,124,1,0,83,124,2,0,100,
3,0,107,9,0,114,147,0,124,2,0,106,1,0,83,116,
2,0,106,3,0,100,10,0,116,5,0,100,8,0,100,9,
0,131,2,1,1,124,0,0,100,11,0,25,125,1,0,100,
12,0,124,0,0,107,7,0,114,210,0,124,1,0,106,6,
0,100,13,0,131,1,0,100,14,0,25,125,1,0,124,1,
0,83,41,15,122,167,67,97,108,99,117,108,97,116,101,32,
119,104,97,116,32,95,95,112,97,99,107,97,103,101,95,95,
32,115,104,111,117,108,100,32,98,101,46,10,10,32,32,32,
32,95,95,112,97,99,107,97,103,101,95,95,32,105,115,32,
110,111,116,32,103,117,97,114,97,110,116,101,101,100,32,116,
111,32,98,101,32,100,101,102,105,110,101,100,32,111,114,32,
99,111,117,108,100,32,98,101,32,115,101,116,32,116,111,32,
78,111,110,101,10,32,32,32,32,116,111,32,114,101,112,114,
101,115,101,110,116,32,116,104,97,116,32,105,116,115,32,112,
114,111,112,101,114,32,118,97,108,117,101,32,105,115,32,117,
110,107,110,111,119,110,46,10,10,32,32,32,32,114,134,0,
0,0,114,95,0,0,0,78,218,0,122,32,95,95,112,97,
99,107,97,103,101,95,95,32,33,61,32,95,95,115,112,101,
99,95,95,46,112,97,114,101,110,116,32,40,122,4,32,33,
61,32,250,1,41,114,140,0,0,0,233,3,0,0,0,122,
89,99,97,110,39,116,32,114,101,115,111,108,118,101,32,112,
97,99,107,97,103,101,32,102,114,111,109,32,95,95,115,112,
101,99,95,95,32,111,114,32,95,95,112,97,99,107,97,103,
101,95,95,44,32,102,97,108,108,105,110,103,32,98,97,99,
107,32,111,110,32,95,95,110,97,109,101,95,95,32,97,110,
100,32,95,95,112,97,116,104,95,95,114,1,0,0,0,114,
131,0,0,0,114,121,0,0,0,114,33,0,0,0,41,7,
114,42,0,0,0,114,123,0,0,0,114,142,0,0,0,114,
143,0,0,0,114,115,0,0,0,114,176,0,0,0,114,122,
0,0,0,41,3,218,7,103,108,111,98,97,108,115,114,170,
0,0,0,114,88,0,0,0,114,10,0,0,0,114,10,0,
0,0,114,11,0,0,0,218,17,95,99,97,108,99,95,95,
95,112,97,99,107,97,103,101,95,95,4,4,0,0,115,30,
0,0,0,0,7,15,1,15,1,12,1,27,1,42,2,13,
1,4,1,12,1,7,2,9,2,13,1,10,1,12,1,19,
1,114,200,0,0,0,99,5,0,0,0,0,0,0,0,9,
0,0,0,5,0,0,0,67,0,0,0,115,227,0,0,0,
124,4,0,100,1,0,107,2,0,114,27,0,116,0,0,124,
0,0,131,1,0,125,5,0,110,54,0,124,1,0,100,2,
@ -1870,17 +1879,17 @@ const unsigned char _Py_M__importlib[] = {
111,117,108,100,32,104,97,118,101,32,97,32,39,108,101,118,
101,108,39,32,111,102,32,50,41,46,10,10,32,32,32,32,
114,33,0,0,0,78,114,121,0,0,0,41,8,114,187,0,
0,0,114,198,0,0,0,218,9,112,97,114,116,105,116,105,
0,0,114,200,0,0,0,218,9,112,97,114,116,105,116,105,
111,110,114,168,0,0,0,114,14,0,0,0,114,21,0,0,
0,114,1,0,0,0,114,195,0,0,0,41,9,114,15,0,
0,0,114,197,0,0,0,218,6,108,111,99,97,108,115,114,
0,0,114,199,0,0,0,218,6,108,111,99,97,108,115,114,
193,0,0,0,114,171,0,0,0,114,89,0,0,0,90,8,
103,108,111,98,97,108,115,95,114,170,0,0,0,90,7,99,
117,116,95,111,102,102,114,10,0,0,0,114,10,0,0,0,
114,11,0,0,0,218,10,95,95,105,109,112,111,114,116,95,
95,25,4,0,0,115,26,0,0,0,0,11,12,1,15,2,
95,31,4,0,0,115,26,0,0,0,0,11,12,1,15,2,
24,1,12,1,18,1,6,3,12,1,23,1,6,1,4,4,
35,3,40,2,114,201,0,0,0,99,1,0,0,0,0,0,
35,3,40,2,114,203,0,0,0,99,1,0,0,0,0,0,
0,0,2,0,0,0,3,0,0,0,67,0,0,0,115,53,
0,0,0,116,0,0,106,1,0,124,0,0,131,1,0,125,
1,0,124,1,0,100,0,0,107,8,0,114,43,0,116,2,
@ -1891,8 +1900,8 @@ const unsigned char _Py_M__importlib[] = {
0,0,114,77,0,0,0,114,150,0,0,0,41,2,114,15,
0,0,0,114,88,0,0,0,114,10,0,0,0,114,10,0,
0,0,114,11,0,0,0,218,18,95,98,117,105,108,116,105,
110,95,102,114,111,109,95,110,97,109,101,60,4,0,0,115,
8,0,0,0,0,1,15,1,12,1,16,1,114,202,0,0,
110,95,102,114,111,109,95,110,97,109,101,66,4,0,0,115,
8,0,0,0,0,1,15,1,12,1,16,1,114,204,0,0,
0,99,2,0,0,0,0,0,0,0,12,0,0,0,12,0,
0,0,67,0,0,0,115,74,1,0,0,124,1,0,97,0,
0,124,0,0,97,1,0,116,2,0,116,1,0,131,1,0,
@ -1937,7 +1946,7 @@ const unsigned char _Py_M__importlib[] = {
0,114,21,0,0,0,218,5,105,116,101,109,115,114,178,0,
0,0,114,76,0,0,0,114,151,0,0,0,114,82,0,0,
0,114,161,0,0,0,114,132,0,0,0,114,137,0,0,0,
114,1,0,0,0,114,202,0,0,0,114,5,0,0,0,114,
114,1,0,0,0,114,204,0,0,0,114,5,0,0,0,114,
77,0,0,0,41,12,218,10,115,121,115,95,109,111,100,117,
108,101,218,11,95,105,109,112,95,109,111,100,117,108,101,90,
11,109,111,100,117,108,101,95,116,121,112,101,114,15,0,0,
@ -1948,10 +1957,10 @@ const unsigned char _Py_M__importlib[] = {
101,97,100,95,109,111,100,117,108,101,90,14,119,101,97,107,
114,101,102,95,109,111,100,117,108,101,114,10,0,0,0,114,
10,0,0,0,114,11,0,0,0,218,6,95,115,101,116,117,
112,67,4,0,0,115,50,0,0,0,0,9,6,1,6,3,
112,73,4,0,0,115,50,0,0,0,0,9,6,1,6,3,
12,1,28,1,15,1,15,1,9,1,15,1,9,2,3,1,
15,1,17,3,13,1,13,1,15,1,15,2,13,1,20,3,
3,1,16,1,13,2,11,1,16,3,12,1,114,206,0,0,
3,1,16,1,13,2,11,1,16,3,12,1,114,208,0,0,
0,99,2,0,0,0,0,0,0,0,3,0,0,0,3,0,
0,0,67,0,0,0,115,87,0,0,0,116,0,0,124,0,
0,124,1,0,131,2,0,1,116,1,0,106,2,0,106,3,
@ -1963,15 +1972,15 @@ const unsigned char _Py_M__importlib[] = {
112,111,114,116,108,105,98,32,97,115,32,116,104,101,32,105,
109,112,108,101,109,101,110,116,97,116,105,111,110,32,111,102,
32,105,109,112,111,114,116,46,114,33,0,0,0,78,41,11,
114,206,0,0,0,114,14,0,0,0,114,175,0,0,0,114,
114,208,0,0,0,114,14,0,0,0,114,175,0,0,0,114,
113,0,0,0,114,151,0,0,0,114,161,0,0,0,218,26,
95,102,114,111,122,101,110,95,105,109,112,111,114,116,108,105,
98,95,101,120,116,101,114,110,97,108,114,119,0,0,0,218,
8,95,105,110,115,116,97,108,108,114,21,0,0,0,114,1,
0,0,0,41,3,114,204,0,0,0,114,205,0,0,0,114,
207,0,0,0,114,10,0,0,0,114,10,0,0,0,114,11,
0,0,0,114,208,0,0,0,114,4,0,0,115,12,0,0,
0,0,2,13,2,16,1,16,3,12,1,6,1,114,208,0,
0,0,0,41,3,114,206,0,0,0,114,207,0,0,0,114,
209,0,0,0,114,10,0,0,0,114,10,0,0,0,114,11,
0,0,0,114,210,0,0,0,120,4,0,0,115,12,0,0,
0,0,2,13,2,16,1,16,3,12,1,6,1,114,210,0,
0,0,41,51,114,3,0,0,0,114,119,0,0,0,114,12,
0,0,0,114,16,0,0,0,114,17,0,0,0,114,59,0,
0,0,114,41,0,0,0,114,48,0,0,0,114,31,0,0,
@ -1987,8 +1996,8 @@ const unsigned char _Py_M__importlib[] = {
0,114,172,0,0,0,114,174,0,0,0,114,177,0,0,0,
114,182,0,0,0,114,192,0,0,0,114,183,0,0,0,114,
185,0,0,0,114,186,0,0,0,114,187,0,0,0,114,195,
0,0,0,114,198,0,0,0,114,201,0,0,0,114,202,0,
0,0,114,206,0,0,0,114,208,0,0,0,114,10,0,0,
0,0,0,114,200,0,0,0,114,203,0,0,0,114,204,0,
0,0,114,208,0,0,0,114,210,0,0,0,114,10,0,0,
0,114,10,0,0,0,114,10,0,0,0,114,11,0,0,0,
218,8,60,109,111,100,117,108,101,62,8,0,0,0,115,96,
0,0,0,6,17,6,2,12,8,12,4,19,20,6,2,6,
@ -1996,6 +2005,6 @@ const unsigned char _Py_M__importlib[] = {
8,12,11,12,12,12,16,12,36,19,27,19,101,24,26,9,
3,18,45,18,60,12,18,12,17,12,25,12,29,12,23,12,
16,19,73,19,77,19,13,12,9,12,9,15,40,12,17,6,
1,10,2,12,27,12,6,18,24,12,32,12,21,24,35,12,
1,10,2,12,27,12,6,18,24,12,32,12,27,24,35,12,
7,12,47,
};