[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/classmodule/__init__.py Merge revision 26067 from the trunk:

Fred L. Drake, Jr. fred at zope.com
Fri Jul 2 16:54:55 EDT 2004


Log message for revision 26074:
Merge revision 26067 from the trunk:
Re-write test to allow re-running in a loop.

This test assumes that a module from the standard library has not been
imported, which means it is very fragile and could be broken by unrelated
changes elsewhere.  Instead, create a temporary module specifically for
the test, and clean it up when the test is done.



-=-
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/classmodule/__init__.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/classmodule/__init__.py	2004-07-02 20:53:13 UTC (rev 26073)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/classmodule/__init__.py	2004-07-02 20:54:55 UTC (rev 26074)
@@ -642,7 +642,7 @@
 
 
 def safe_import(path, default=None):
-    """Import a given path as efficiently as possible and without failure.
+    r"""Import a given path as efficiently as possible and without failure.
 
     First we try to find the path in 'sys.modules', since this lookup is much
     more efficient than importing it. If it was not found, we go back and try
@@ -655,13 +655,32 @@
       >>> safe_import('zope.app') is sys.modules['zope.app']
       True
 
-      >>> 'shelve' in sys.modules
+      >>> safe_import('weirdname') is None
+      True
+
+    For this example, we'll create a dummy module:
+
+      >>> here = os.path.dirname(__file__)
+      >>> filename = os.path.join(here, 'testmodule.py')
+      >>> f = open(filename, 'w')
+      >>> f.write('# dummy module\n')
+      >>> f.close()
+
+    The temporary module is not already imported, but will be once
+    we've called safe_import():
+
+      >>> module_name = __name__ + '.testmodule'
+      >>> module_name in sys.modules
       False
-      >>> safe_import('shelve').__name__
-      'shelve'
+      >>> safe_import(module_name).__name__ == module_name
+      True
+      >>> module_name in sys.modules
+      True
+      >>> del sys.modules[module_name]
 
-      >>> safe_import('weirdname') is None
-      True
+    Now clean up the temporary module, just to play nice:
+
+      >>> os.unlink(filename)
     """
     module = sys.modules.get(path, default)
     if module is default:



More information about the Zope3-Checkins mailing list