[Zope3-checkins] SVN: Zope3/trunk/src/zope/importtool/ - be a
little more careful in reporting failures to import;
Fred L. Drake, Jr.
fred at zope.com
Wed Jun 2 15:51:07 EDT 2004
Log message for revision 25201:
- be a little more careful in reporting failures to import;
weird edge case
- add a function written while trying to figure out import.c from
Python; not yet sure if this will be useful, but want to save it
-=-
Modified: Zope3/trunk/src/zope/importtool/hook.py
===================================================================
--- Zope3/trunk/src/zope/importtool/hook.py 2004-06-02 19:37:55 UTC (rev 25200)
+++ Zope3/trunk/src/zope/importtool/hook.py 2004-06-02 19:51:07 UTC (rev 25201)
@@ -77,7 +77,33 @@
if fromlist:
imported = getattr(v, "__name__", None)
else:
- mod = self.previous(name, globals, locals, ("foo",))
+ try:
+ mod = self.previous(name, globals, locals, ("foo",))
+ except:
+ self.reporter.exception(importer, name, fromlist,
+ sys.exc_info())
+ raise
imported = getattr(mod, "__name__", None)
self.reporter.found(importer, imported, fromlist)
return v
+
+
+def get_package_name(globals):
+ """Return the package name that produced `globals`, or None.
+
+ If `globals` is the dict for a module in a package, the name of
+ the package the module is contained in is returned.
+
+ If `globals` is the dict for a module that isn't in a package,
+ None is returned.
+
+ """
+ name = globals.get("__name__")
+ if name:
+ if "__path__" in globals:
+ # this is a package's __init__, just return the name
+ return name
+ elif "." in name:
+ # it's a module in a package; return the package name
+ return name[:name.rfind(".")]
+ return None
Modified: Zope3/trunk/src/zope/importtool/tests/test_hook.py
===================================================================
--- Zope3/trunk/src/zope/importtool/tests/test_hook.py 2004-06-02 19:37:55 UTC (rev 25200)
+++ Zope3/trunk/src/zope/importtool/tests/test_hook.py 2004-06-02 19:51:07 UTC (rev 25201)
@@ -24,6 +24,9 @@
from zope.importtool.tests import sample
+THIS_PACKAGE = __name__[:__name__.rfind(".")]
+PARENT_PACKAGE = THIS_PACKAGE[:THIS_PACKAGE.rfind(".")]
+
real__import__ = __import__
@@ -202,5 +205,19 @@
self.exc_info = None
+class HelpFunctionTestCase(unittest.TestCase):
+
+ def test_get_package_name(self):
+ self.assertEqual(hook.get_package_name(globals()),
+ THIS_PACKAGE)
+ self.assertEqual(hook.get_package_name(sys.__dict__), None)
+ self.assertEqual(hook.get_package_name(reporter.__dict__),
+ PARENT_PACKAGE)
+ import logging
+ self.assertEqual(hook.get_package_name(logging.__dict__), "logging")
+
+
def test_suite():
- return unittest.makeSuite(HookTestCase)
+ suite = unittest.makeSuite(HookTestCase)
+ suite.addTest(unittest.makeSuite(HelpFunctionTestCase))
+ return suite
More information about the Zope3-Checkins
mailing list