]> granicus.if.org Git - python/commitdiff
Issue #27419: Standard __import__() no longer look up "__import__" in globals
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 17 Jul 2016 09:51:34 +0000 (12:51 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 17 Jul 2016 09:51:34 +0000 (12:51 +0300)
or builtins for importing submodules or "from import".  Fixed a crash if
raise a warning about unabling to resolve package from __spec__ or
__package__.

1  2 
Misc/NEWS
Python/import.c

diff --cc Misc/NEWS
index 848700ac0f723d061ad5a670aa5b6817d4ee3c73,714bf6a25705f9b9b98fe80ff050a7129ff179c4..1d6f5c36401615cf3a55bb29a2838c6743fe7e3e
+++ b/Misc/NEWS
@@@ -10,6 -10,10 +10,11 @@@ What's New in Python 3.6.0 alpha 
  Core and Builtins
  -----------------
  
 -  or builtins for importing submodules or "from import".  Fixed handling an
 -  error of non-string package name.
+ - 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__.
  - 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 bdc7e4cfa77fa72be0d51885bad9ba7c332f6568,c1dc9a06c53336bea553814e645b643464b60dba..7a2c92eef94c0519f252dc8f56febcb8d1a7c115
@@@ -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");