[Python-checkins] cpython: Issue #18065: For frozen packages set __path__ to [].

brett.cannon python-checkins at python.org
Sat Jun 1 05:18:49 CEST 2013


http://hg.python.org/cpython/rev/82db02a2e023
changeset:   84003:82db02a2e023
user:        Brett Cannon <brett at python.org>
date:        Fri May 31 23:18:39 2013 -0400
summary:
  Issue #18065: For frozen packages set __path__ to [].

Previously __path__ was set to [__name__], but that could lead to bad
results if someone managed to circumvent the frozen importer and
somehow ended up with a finder that thought __name__ was a legit
directory/location.

files:
  Doc/whatsnew/3.4.rst                          |  7 +++++++
  Lib/test/test_importlib/frozen/test_loader.py |  2 +-
  Misc/NEWS                                     |  6 ++++++
  Python/import.c                               |  6 ++----
  4 files changed, 16 insertions(+), 5 deletions(-)


diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst
--- a/Doc/whatsnew/3.4.rst
+++ b/Doc/whatsnew/3.4.rst
@@ -259,3 +259,10 @@
   ``__package__`` unconditionally to properly support reloading. If this is not
   desired then you will need to set these attributes manually. You can use
   :func:`importlib.util.module_to_load` for module management.
+
+* Import now resets relevant attributes (e.g. ``__name__``, ``__loader__``,
+  ``__package__``, ``__file__``, ``__cached__``) unconditionally when reloading.
+
+* Frozen packages no longer set ``__path__`` to a list containg the package name
+  but an empty list instead. Determing if a module is a package should be done
+  using ``hasattr(module, '__path__')``.
diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py
--- a/Lib/test/test_importlib/frozen/test_loader.py
+++ b/Lib/test/test_importlib/frozen/test_loader.py
@@ -24,7 +24,7 @@
             module = machinery.FrozenImporter.load_module('__phello__')
             check = {'__name__': '__phello__',
                      '__package__': '__phello__',
-                     '__path__': ['__phello__'],
+                     '__path__': [],
                      '__loader__': machinery.FrozenImporter,
                      }
             for attr, value in check.items():
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,12 @@
 Core and Builtins
 -----------------
 
+- Issue #18065: Don't set __path__ to the package name for frozen packages.
+
+- Issue #18088: When reloading a module, unconditionally reset all relevant
+  attributes on the module (e.g. __name__, __loader__, __package__, __file__,
+  __cached__).
+
 - Issue #17937: Try harder to collect cyclic garbage at shutdown.
 
 - Issue #12370: Prevent class bodies from interfering with the __class__
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1107,19 +1107,17 @@
         goto err_return;
     }
     if (ispackage) {
-        /* Set __path__ to the package name */
+        /* Set __path__ to the empty list */
         PyObject *d, *l;
         int err;
         m = PyImport_AddModuleObject(name);
         if (m == NULL)
             goto err_return;
         d = PyModule_GetDict(m);
-        l = PyList_New(1);
+        l = PyList_New(0);
         if (l == NULL) {
             goto err_return;
         }
-        Py_INCREF(name);
-        PyList_SET_ITEM(l, 0, name);
         err = PyDict_SetItemString(d, "__path__", l);
         Py_DECREF(l);
         if (err != 0)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list