[Zope3-checkins] SVN: Zope3/trunk/src/zope/importtool/ make sure the import hook can be called all the same ways the default

Fred L. Drake, Jr. fred at zope.com
Fri May 28 11:54:35 EDT 2004


Log message for revision 25088:
make sure the import hook can be called all the same ways the default
implementation of the hook can be called, and returns the same results



-=-
Modified: Zope3/trunk/src/zope/importtool/hook.py
===================================================================
--- Zope3/trunk/src/zope/importtool/hook.py	2004-05-28 15:49:00 UTC (rev 25087)
+++ Zope3/trunk/src/zope/importtool/hook.py	2004-05-28 15:54:35 UTC (rev 25088)
@@ -29,6 +29,7 @@
 """
 
 import __builtin__
+import sys
 
 __all__ = "install_reporter", "uninstall_reporter"
 
@@ -43,7 +44,9 @@
     if previous__import__ is not None:
         raise RuntimeError("import reporting hook already installed")
 
-    def importhook(name, globals, locals, fromlist):
+    def importhook(name, globals=None, locals=None, fromlist=None):
+        if globals is None:
+            globals = sys._getframe(1).f_globals
         importer = globals.get("__name__")
         reporter.request(importer, name, fromlist)
         v = previous__import__(name, globals, locals, fromlist)

Modified: Zope3/trunk/src/zope/importtool/tests/test_hook.py
===================================================================
--- Zope3/trunk/src/zope/importtool/tests/test_hook.py	2004-05-28 15:49:00 UTC (rev 25087)
+++ Zope3/trunk/src/zope/importtool/tests/test_hook.py	2004-05-28 15:54:35 UTC (rev 25088)
@@ -16,10 +16,12 @@
 $Id$
 """
 import __builtin__
+import sys
 import unittest
 
 from zope.importtool import hook
 from zope.importtool import reporter
+from zope.importtool.tests import sample
 
 
 real__import__ = __import__
@@ -125,6 +127,30 @@
         else:
             self.fail("expected TestException")
 
+    def test_direct_calls(self):
+        # make sure the hook function can be called directly as well,
+        # and behave the way the default __import__() works
+        hook.install_reporter(self)
+        m = __import__("sys")
+        self.assertEqual(self.reports[-1],
+                         (__name__, "sys", "sys", None))
+        self.failUnless(m is sys)
+        m = __import__("sample")
+        self.assertEqual(self.reports[-1],
+                         (__name__, "zope.importtool.tests.sample", "sample",
+                          None))
+        self.failUnless(m is sample)
+        m = __import__("sys", {"__name__": "foo.bar"})
+        self.assertEqual(self.reports[-1],
+                         ("foo.bar", "sys", "sys", None))
+        self.failUnless(m is sys)
+        __import__("sys", {"__name__": "foo.bar"}, {}, ("splat", "splurt"))
+        self.assertEqual(self.reports[-1],
+                         ("foo.bar", "sys", "sys", ("splat", "splurt")))
+        m = __import__("zope.importtool.tests.sample", globals(), {},
+                       ("THE_ANSWER",))
+        self.failUnless(m is sample)
 
+
 def test_suite():
     return unittest.makeSuite(HookTestCase)




More information about the Zope3-Checkins mailing list