From: Serhiy Storchaka Date: Sun, 17 Jul 2016 09:51:34 +0000 (+0300) Subject: Issue #27419: Standard __import__() no longer look up "__import__" in globals X-Git-Tag: v3.6.0a4~160 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7905f99a27c82fb31218805d4b859fc0dbefbcec;p=python Issue #27419: Standard __import__() no longer look up "__import__" in globals or builtins for importing submodules or "from import". Fixed a crash if raise a warning about unabling to resolve package from __spec__ or __package__. --- 7905f99a27c82fb31218805d4b859fc0dbefbcec diff --cc Misc/NEWS index 848700ac0f,714bf6a257..1d6f5c3640 --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -10,6 -10,10 +10,11 @@@ What's New in Python 3.6.0 alpha Core and Builtins ----------------- + - Issue #27419: Standard __import__() no longer look up "__import__" in globals - or builtins for importing submodules or "from import". Fixed handling an - error of non-string package name. ++ or builtins for importing submodules or "from import". Fixed a crash if ++ raise a warning about unabling to resolve package from __spec__ or ++ __package__. + - Issue #27083: Respect the PYTHONCASEOK environment variable under Windows. - Issue #27514: Make having too many statically nested blocks a SyntaxError diff --cc Python/import.c index bdc7e4cfa7,c1dc9a06c5..7a2c92eef9 --- a/Python/import.c +++ b/Python/import.c @@@ -1420,44 -1429,8 +1420,45 @@@ PyImport_ImportModuleLevelObject(PyObje PyErr_SetString(PyExc_TypeError, "package must be a string"); goto error; } + else if (spec != NULL && spec != Py_None) { + 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 && spec != Py_None) { + 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 = NULL; + 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");