[Python-checkins] r55955 - sandbox/trunk/import_in_py/pseudocode.py

brett.cannon python-checkins at python.org
Wed Jun 13 07:53:19 CEST 2007


Author: brett.cannon
Date: Wed Jun 13 07:53:18 2007
New Revision: 55955

Modified:
   sandbox/trunk/import_in_py/pseudocode.py
Log:
Add support for __path__ searching.


Modified: sandbox/trunk/import_in_py/pseudocode.py
==============================================================================
--- sandbox/trunk/import_in_py/pseudocode.py	(original)
+++ sandbox/trunk/import_in_py/pseudocode.py	Wed Jun 13 07:53:18 2007
@@ -25,10 +25,17 @@
     # Since loaders are required to set the module in sys.modules, a successful
     # import should be followed by 'continue' to let the next module be
     # imported.
-    for parent in each_parent(name):
+    for name in each_parent(name):
         # If the module is already cached in sys.modules then move along.
-        if parent in sys.modules:
+        if name in sys.modules:
             continue
+        # Try to find a __path__ attribute on the (possibly non-existent)
+        # parent.
+        immediate_parent = name.rsplit('.', 1)[0]
+        try:
+            path = sys.modules[immediate_parent].__path__
+        except (KeyError, AttributeError):
+            path = None
         # Search sys.meta_path.
         for meta_importer in sys.meta_path:
             loader = meta_importer.find_module(name, path)
@@ -43,8 +50,9 @@
                     loader.load_module(name)
                     continue
         # With sys.meta_path, built-ins, and frozen modules checked, now look
-        # at sys.path.
-        for path_entry in sys.path:
+        # at sys.path or parent.__path__.
+        search_path = path if path else sys.path
+        for path_entry in search_path
             # Look for a cached importer.
             if path_entry in sys.path_importer_cache:
                 importer = sys.path_importer_cache[path_entry]


More information about the Python-checkins mailing list