[Python-checkins] r53035 - sandbox/trunk/import_in_py/importer.py

brett.cannon python-checkins at python.org
Thu Dec 14 22:50:09 CET 2006


Author: brett.cannon
Date: Thu Dec 14 22:50:08 2006
New Revision: 53035

Modified:
   sandbox/trunk/import_in_py/importer.py
Log:
Add a context manager for the import lock.

Also add notes in the docstring of what tests fail and why.


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Thu Dec 14 22:50:08 2006
@@ -2,7 +2,9 @@
 
 XXX See http://docs.google.com/View?docid=dg7fctr4_4d8tdbq on current status.
 
-References on import:
+====================
+References on import
+====================
 * Language reference
       http://docs.python.org/ref/import.html
 * __import__ function
@@ -16,7 +18,33 @@
 * PEP 302: New Import Hooks
       http://www.python.org/dev/peps/pep-0302/
 * PEP 328: Imports: Multi-line and Absolute/Relative
-      http://www.python.org/dev/peps/pep-0328    
+      http://www.python.org/dev/peps/pep-0328
+
+============
+Known Issues
+============
+* runpy ('-m' command-line option for Python) does not work.
+    + Requires get_code to be implemented for loaders.
+    + Uses pkgutil.get_loader which fakes a loader if __loader__ is not defined.
+    + New loaders do define __loader__ but not get_code, and thus dies on and
+      AttributeError.
+    + Fix
+        - Implement optional interface for loaders.
+* warnings and stack level.
+    + 'warnings' assumes that the import code does not show up in the call
+      stack.
+    + Because import now in Python, import does show up in the call stack.
+    + Trick of specifying that going back two levels will cause the warning
+      to be raised in the caller for an import statement no longer holds true.
+    + Fixes
+        - Special module deprecation function.
+        - Code in warnings.warn to handle import case.
+        - Flag on warnings.warn that warning is for an import and ignore stack
+          level argument.
+              * Could also infer from type of warning.
+* test_pkg
+    + Old-style test that compares output.
+    + Setting of __loader__ leads to different output.
 
 """
 from __future__ import with_statement
@@ -455,7 +483,20 @@
         if package is not None:
             module.__path__ = [package]
         return module
-                                
+
+
+class ImportLockContext(object):
+
+    """Context manager for the import lock."""
+    
+    def __enter__(self):
+        """Acquire the import lock."""
+        imp.acquire_lock()
+        
+    def __exit__(self, exc_type, exc_value, exc_traceback):
+        """Release the import lock regardless of any raised exceptions."""
+        imp.release_lock()
+
 
 class Import(object):
 
@@ -733,8 +774,7 @@
             raise ValueError("Empty module name")
         is_pkg = True if '__path__' in globals else False
         caller_name = globals.get('__name__')
-        imp.acquire_lock()
-        try:
+        with ImportLockContext():
             if level and caller_name:
                 # Handle the classic style of import: relative first, then
                 # absolute.
@@ -766,5 +806,3 @@
                 self.import_full_module(name)
             relative_name = '' if imported_name == name else name
             return self.return_module(imported_name, relative_name, fromlist)
-        finally:
-            imp.release_lock()


More information about the Python-checkins mailing list