[Python-checkins] cpython: Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.

florent.xicluna python-checkins at python.org
Sat Jul 7 13:17:00 CEST 2012


http://hg.python.org/cpython/rev/37850f083af5
changeset:   77964:37850f083af5
user:        Florent Xicluna <florent.xicluna at gmail.com>
date:        Sat Jul 07 13:16:44 2012 +0200
summary:
  Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.

files:
  Lib/importlib/_bootstrap.py |  29 ++++++++++++------------
  1 files changed, 15 insertions(+), 14 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -140,7 +140,7 @@
 
 
 def _wrap(new, old):
-    """Simple substitute for functools.wraps."""
+    """Simple substitute for functools.update_wrapper."""
     for replace in ['__module__', '__name__', '__qualname__', '__doc__']:
         if hasattr(old, replace):
             setattr(new, replace, getattr(old, replace))
@@ -345,7 +345,7 @@
     """Set __package__ on the returned module."""
     def set_package_wrapper(*args, **kwargs):
         module = fxn(*args, **kwargs)
-        if not hasattr(module, '__package__') or module.__package__ is None:
+        if getattr(module, '__package__', None) is None:
             module.__package__ = module.__name__
             if not hasattr(module, '__path__'):
                 module.__package__ = module.__package__.rpartition('.')[0]
@@ -438,7 +438,7 @@
     """Decorator to verify the named module is built-in."""
     def _requires_builtin_wrapper(self, fullname):
         if fullname not in sys.builtin_module_names:
-            raise ImportError("{0} is not a built-in module".format(fullname),
+            raise ImportError("{} is not a built-in module".format(fullname),
                               name=fullname)
         return fxn(self, fullname)
     _wrap(_requires_builtin_wrapper, fxn)
@@ -449,7 +449,7 @@
     """Decorator to verify the named module is frozen."""
     def _requires_frozen_wrapper(self, fullname):
         if not _imp.is_frozen(fullname):
-            raise ImportError("{0} is not a frozen module".format(fullname),
+            raise ImportError("{} is not a frozen module".format(fullname),
                               name=fullname)
         return fxn(self, fullname)
     _wrap(_requires_frozen_wrapper, fxn)
@@ -511,7 +511,7 @@
     @classmethod
     @_requires_builtin
     def is_package(cls, fullname):
-        """Return None as built-in modules are never packages."""
+        """Return False as built-in modules are never packages."""
         return False
 
 
@@ -936,7 +936,7 @@
         return len(self._recalculate())
 
     def __repr__(self):
-        return "_NamespacePath({0!r})".format(self._path)
+        return "_NamespacePath({!r})".format(self._path)
 
     def __contains__(self, item):
         return item in self._recalculate()
@@ -1198,7 +1198,7 @@
     if len(bits) < level:
         raise ValueError('attempted relative import beyond top-level package')
     base = bits[0]
-    return '{0}.{1}'.format(base, name) if name else base
+    return '{}.{}'.format(base, name) if name else base
 
 
 def _find_module(name, path):
@@ -1228,7 +1228,7 @@
         if not isinstance(package, str):
             raise TypeError("__package__ not set to a string")
         elif package not in sys.modules:
-            msg = ("Parent module {0!r} not loaded, cannot perform relative "
+            msg = ("Parent module {!r} not loaded, cannot perform relative "
                    "import")
             raise SystemError(msg.format(package))
     if not name and level == 0:
@@ -1267,7 +1267,7 @@
         parent_module = sys.modules[parent]
         setattr(parent_module, name.rpartition('.')[2], module)
     # Set __package__ if the loader did not.
-    if not hasattr(module, '__package__') or module.__package__ is None:
+    if getattr(module, '__package__', None) is None:
         try:
             module.__package__ = module.__name__
             if not hasattr(module, '__path__'):
@@ -1336,11 +1336,12 @@
             fromlist = list(fromlist)
             fromlist.remove('*')
             fromlist.extend(module.__all__)
-        for x in (y for y in fromlist if not hasattr(module, y)):
-            try:
-                import_('{0}.{1}'.format(module.__name__, x))
-            except ImportError:
-                pass
+        for x in fromlist:
+            if not hasattr(module, x):
+                try:
+                    import_('{}.{}'.format(module.__name__, x))
+                except ImportError:
+                    pass
     return module
 
 

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


More information about the Python-checkins mailing list