[Zope3-checkins] SVN: Zope3/trunk/src/zope/importtool/ Adjust
reporting API to deal with errors during import.
Fred L. Drake, Jr.
fred at zope.com
Fri May 28 23:06:18 EDT 2004
Log message for revision 25116:
Adjust reporting API to deal with errors during import.
- add exception() method to report an exception during import; this allows
a reporter implementation to maintain a stack of "active" imports
- in the tests, the "reports" ivar is not a stack, and cannot be treated
as one (it is a serial event log)
-=-
Modified: Zope3/trunk/src/zope/importtool/hook.py
===================================================================
--- Zope3/trunk/src/zope/importtool/hook.py 2004-05-29 02:26:54 UTC (rev 25115)
+++ Zope3/trunk/src/zope/importtool/hook.py 2004-05-29 03:06:17 UTC (rev 25116)
@@ -68,7 +68,12 @@
globals = sys._getframe(1).f_globals
importer = globals.get("__name__")
self.reporter.request(importer, name, fromlist)
- v = self.previous(name, globals, locals, fromlist)
+ try:
+ v = self.previous(name, globals, locals, fromlist)
+ except:
+ self.reporter.exception(importer, name, fromlist,
+ sys.exc_info())
+ raise
if fromlist:
imported = getattr(v, "__name__", None)
else:
Added: Zope3/trunk/src/zope/importtool/tests/error.py
===================================================================
--- Zope3/trunk/src/zope/importtool/tests/error.py 2004-05-29 02:26:54 UTC (rev 25115)
+++ Zope3/trunk/src/zope/importtool/tests/error.py 2004-05-29 03:06:17 UTC (rev 25116)
@@ -0,0 +1,23 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Module that raises an error on import.
+
+This is used by the tests.
+
+$Id$
+"""
+
+from zope.importtool.tests.test_hook import TestException
+
+raise TestException(__name__)
Property changes on: Zope3/trunk/src/zope/importtool/tests/error.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-29 02:26:54 UTC (rev 25115)
+++ Zope3/trunk/src/zope/importtool/tests/test_hook.py 2004-05-29 03:06:17 UTC (rev 25116)
@@ -61,17 +61,20 @@
reporter = self
return hook.ReportingHook(reporter)
+ # reporter methods
+
def request(self, importer, name, fromlist):
- self.reports.append(name)
+ self.reports.append(("<request>", importer, name))
def found(self, importer, imported, fromlist):
- name = self.reports.pop()
- self.reports.append((importer, imported, name, fromlist))
+ self.reports.append(("<found>", importer, imported, fromlist))
- def raise_error(self, *args):
- self.reports.append(args)
- raise TestException()
+ def exception(self, importer, name, fromlist, exc_info):
+ self.reports.append(("<exception>", importer, name, fromlist))
+ self.exc_info = exc_info
+ # test methods
+
def test_normal_installation(self):
h = self.get_hook()
self.failIf(h.active)
@@ -113,13 +116,16 @@
import sys
import sys
from sample import THE_ANSWER
- self.assertEqual(
- self.reports,
- [(__name__, "sys", "sys", None),
- (__name__, "sys", "sys", None),
- (__name__, "zope.importtool.tests.sample", "sample",
- ("THE_ANSWER",)),
- ])
+ expected = [
+ ("<request>", __name__, "sys"),
+ ("<found>", __name__, "sys", None),
+ ("<request>", __name__, "sys"),
+ ("<found>", __name__, "sys", None),
+ ("<request>", __name__, "sample"),
+ ("<found>", __name__, "zope.importtool.tests.sample",
+ ("THE_ANSWER",)),
+ ]
+ self.assertEqual(self.reports, expected)
def test_exception_on_request(self):
h = self.get_hook(ReporterRaiseOnRequest())
@@ -148,24 +154,37 @@
h.install()
m = __import__("sys")
self.assertEqual(self.reports[-1],
- (__name__, "sys", "sys", None))
+ ("<found>", __name__, "sys", None))
self.failUnless(m is sys)
m = __import__("sample")
self.assertEqual(self.reports[-1],
- (__name__, "zope.importtool.tests.sample", "sample",
+ ("<found>", __name__, "zope.importtool.tests.sample",
None))
self.failUnless(m is sample)
m = __import__("sys", {"__name__": "foo.bar"})
self.assertEqual(self.reports[-1],
- ("foo.bar", "sys", "sys", None))
+ ("<found>", "foo.bar", "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")))
+ ("<found>", "foo.bar", "sys", ("splat", "splurt")))
m = __import__("zope.importtool.tests.sample", globals(), {},
("THE_ANSWER",))
self.failUnless(m is sample)
+ def test_reporting_exception_during_import(self):
+ h = self.get_hook()
+ h.install()
+ try:
+ import error
+ except TestException, e:
+ self.failUnless(e is self.exc_info[1])
+ self.assertEqual(self.reports[-1],
+ ("<exception>", __name__, "error", None))
+ else:
+ self.fail("expected TestException")
+ self.exc_info = None
+
def test_suite():
return unittest.makeSuite(HookTestCase)
More information about the Zope3-Checkins
mailing list