[Zope3-checkins] SVN: Zope3/trunk/src/zope/importtool/ revise the
interface for the reporter; there are now opportunities to intercept
Fred L. Drake, Jr.
fred at zope.com
Thu May 27 22:29:09 EDT 2004
Log message for revision 25073:
revise the interface for the reporter; there are now opportunities to intercept
the import both before and after the actual import has been performed
-=-
Modified: Zope3/trunk/src/zope/importtool/hook.py
===================================================================
--- Zope3/trunk/src/zope/importtool/hook.py 2004-05-28 02:27:35 UTC (rev 25072)
+++ Zope3/trunk/src/zope/importtool/hook.py 2004-05-28 02:29:08 UTC (rev 25073)
@@ -33,26 +33,26 @@
__all__ = "install_reporter", "uninstall_reporter"
-
previous__import__ = None
current__import__ = None
-def install_reporter(report):
+def install_reporter(reporter):
global current__import__
global previous__import__
if previous__import__ is not None:
raise RuntimeError("import reporting hook already installed")
def importhook(name, globals, locals, fromlist):
+ importer = globals.get("__name__")
+ reporter.request(importer, name, fromlist)
v = previous__import__(name, globals, locals, fromlist)
- importer = globals.get("__name__")
if fromlist:
imported = getattr(v, "__name__", None)
else:
mod = previous__import__(name, globals, locals, ("foo",))
imported = getattr(mod, "__name__", None)
- report(importer, imported, name, fromlist)
+ reporter.found(importer, imported, fromlist)
return v
previous__import__ = __builtin__.__import__
Added: Zope3/trunk/src/zope/importtool/reporter.py
===================================================================
--- Zope3/trunk/src/zope/importtool/reporter.py 2004-05-28 02:27:35 UTC (rev 25072)
+++ Zope3/trunk/src/zope/importtool/reporter.py 2004-05-28 02:29:08 UTC (rev 25073)
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+
+$Id$
+"""
+
+class Reporter:
+ """Trivial implementation of the reporter interface."""
+
+ def request(self, importer, name, fromlist):
+ pass
+
+ def found(self, importer, imported, fromlist):
+ pass
Property changes on: Zope3/trunk/src/zope/importtool/reporter.py
___________________________________________________________________
Name: svn:mime-type
+ text/x-python
Name: svn:eol-style
+ native
Modified: Zope3/trunk/src/zope/importtool/tests/test_hook.py
===================================================================
--- Zope3/trunk/src/zope/importtool/tests/test_hook.py 2004-05-28 02:27:35 UTC (rev 25072)
+++ Zope3/trunk/src/zope/importtool/tests/test_hook.py 2004-05-28 02:29:08 UTC (rev 25073)
@@ -19,6 +19,7 @@
import unittest
from zope.importtool import hook
+from zope.importtool import reporter
real__import__ = __import__
@@ -32,6 +33,18 @@
"""Exception raised in the tests."""
+class ReporterRaiseOnFound(reporter.Reporter):
+
+ def found(self, *args):
+ raise TestException("found")
+
+
+class ReporterRaiseOnRequest(reporter.Reporter):
+
+ def request(self, *args):
+ raise TestException("request")
+
+
class HookTestCase(unittest.TestCase):
def setUp(self):
@@ -40,45 +53,49 @@
def tearDown(self):
hook.reset()
- def report(self, *args):
- self.reports.append(args)
+ def request(self, importer, name, fromlist):
+ self.reports.append(name)
+ def found(self, importer, imported, fromlist):
+ name = self.reports.pop()
+ self.reports.append((importer, imported, name, fromlist))
+
def raise_error(self, *args):
self.reports.append(args)
raise TestException()
def test_normal_installation(self):
self.failIf(hook.active())
- hook.install_reporter(self.report)
+ hook.install_reporter(self)
self.failIf(not hook.active())
hook.uninstall_reporter()
self.failIf(hook.active())
# now do it again, to make sure we really can re-install the hook
- hook.install_reporter(self.report)
+ hook.install_reporter(self)
self.failIf(not hook.active())
def test_reinstall_fails_if_active(self):
- hook.install_reporter(self.report)
- self.assertRaises(RuntimeError, hook.install_reporter, self.report)
+ hook.install_reporter(self)
+ self.assertRaises(RuntimeError, hook.install_reporter, self)
def test_uninstall_fails_if_never_active(self):
self.assertRaises(RuntimeError, hook.uninstall_reporter)
def test_uninstall_fails_if_no_longer_active(self):
- hook.install_reporter(self.report)
+ hook.install_reporter(self)
hook.uninstall_reporter()
self.assertRaises(RuntimeError, hook.uninstall_reporter)
def test_wrap_other_hook(self):
__builtin__.__import__ = alternate_hook
- hook.install_reporter(self.report)
+ hook.install_reporter(self)
self.failUnless(hook.active())
hook.uninstall_reporter()
self.failIf(hook.active())
self.failUnless(__builtin__.__import__ is alternate_hook)
def test_report_record(self):
- hook.install_reporter(self.report)
+ hook.install_reporter(self)
import sys
import sys
from sample import THE_ANSWER
@@ -90,15 +107,24 @@
("THE_ANSWER",)),
])
- def test_exception_from_reporter(self):
- hook.install_reporter(self.raise_error)
+ def test_exception_on_request(self):
+ hook.install_reporter(ReporterRaiseOnRequest())
try:
import sys
- except TestException:
- self.assertEqual(len(self.reports), 1)
+ except TestException, e:
+ self.assertEqual(e[0], "request")
else:
self.fail("expected TestException")
+ def test_exception_on_found(self):
+ hook.install_reporter(ReporterRaiseOnFound())
+ try:
+ import sys
+ except TestException, e:
+ self.assertEqual(e[0], "found")
+ else:
+ self.fail("expected TestException")
+
def test_suite():
return unittest.makeSuite(HookTestCase)
More information about the Zope3-Checkins
mailing list