diff --git a/py/log/consumer.py b/py/log/consumer.py index 65b693b8f..c6eaa9ad4 100644 --- a/py/log/consumer.py +++ b/py/log/consumer.py @@ -49,7 +49,7 @@ class Syslog: for priority in "LOG_EMERG LOG_ALERT LOG_CRIT LOG_ERR LOG_WARNING LOG_NOTICE LOG_INFO LOG_DEBUG".split(): try: exec("%s = py.std.syslog.%s" % (priority, priority)) - except ImportError: + except AttributeError: pass def __init__(self, priority = None): diff --git a/py/misc/std.py b/py/misc/std.py index 041b769f8..bef7ae8cc 100644 --- a/py/misc/std.py +++ b/py/misc/std.py @@ -2,11 +2,18 @@ import sys class Std(object): - """ lazily import standard modules """ + """ makes all standard python modules available as a lazily + computed attribute. + """ + def __init__(self): self.__dict__ = sys.modules def __getattr__(self, name): - return __import__(name) + try: + m = __import__(name) + except ImportError: + raise AttributeError("py.std: could not import %s" % name) + return m std = Std() diff --git a/py/misc/testing/test_std.py b/py/misc/testing/test_std.py index dcee51b18..143556a05 100644 --- a/py/misc/testing/test_std.py +++ b/py/misc/testing/test_std.py @@ -6,7 +6,7 @@ def test_os(): assert py.std.os is os def test_import_error_converts_to_attributeerror(): - py.test.raises(ImportError, "py.std.xyzalskdj") + py.test.raises(AttributeError, "py.std.xyzalskdj") def test_std_gets_it(): for x in py.std.sys.modules: