diff options
author | Brett Cannon <brett@python.org> | 2016-01-22 23:25:50 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2016-01-22 23:25:50 (GMT) |
commit | 849113af6ba96c36ffaaa9896c9a337eb3253b89 (patch) | |
tree | cbcbf462841260fbe63c30ed11776028c39ba74e /Python | |
parent | 52c854a83819f1e5ec4aa907cdcc875b02a7ea07 (diff) | |
download | cpython-849113af6ba96c36ffaaa9896c9a337eb3253b89.zip cpython-849113af6ba96c36ffaaa9896c9a337eb3253b89.tar.gz cpython-849113af6ba96c36ffaaa9896c9a337eb3253b89.tar.bz2 |
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.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/import.c | 97 | ||||
-rw-r--r-- | Python/importlib.h | 117 |
2 files changed, 121 insertions, 93 deletions
diff --git a/Python/import.c b/Python/import.c index c1071c0..22f9d21 100644 --- a/Python/import.c +++ b/Python/import.c @@ -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) { - goto error; - } - package = _PyDict_GetItemId(globals, &PyId___name__); - if (package == NULL) { - PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); - goto error; - } + 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"); + 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; } - - 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); + 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); } } diff --git a/Python/importlib.h b/Python/importlib.h index 709c5fd..e4ef835 100644 --- a/Python/importlib.h +++ b/Python/importlib.h @@ -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, }; |