summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIhar Hrachyshka <ihrachys@redhat.com>2017-06-02 13:15:55 -0700
committerIhar Hrachyshka <ihrachys@redhat.com>2017-06-02 13:25:16 -0700
commit14d2f881036b7555d3a0d8fe2a8c59d9e99b5f95 (patch)
treec0080a949ef696906faa29dbbed915f3e6c2ef4c
parent897144678e4f2f3ff328651375690ec2465c4cbc (diff)
downloadoslo-log-14d2f881036b7555d3a0d8fe2a8c59d9e99b5f95.tar.gz
formatter: skip ImportError when adding error_summary
In some projects, we conditionally import modules that may be not installed, for example in Neutron where we import from ibm_db_alembic but ignore ImportError in alembic env.py. It turned out that ImportError is not cleared by except: pass in py2 (but is correctly cleared in py3), as can be seen in the snippet below (kudos to @dhellmann for providing it): apu:~$ cat testme.py import sys print('before:', sys.exc_info()[0]) try: import nonesuch except ImportError: pass print('after:', sys.exc_info()[0]) apu:~$ python2 ./testme.py ('before:', None) ('after:', <type 'exceptions.ImportError'>) apu:~$ python3 ./testme.py before: None after: None The patch makes the formatter skip ImportErrors. We could in theory limit it to py2 only but to simplify matters and to stick to identical behavior across python runtimes, we will just unconditionally skip it for all library consumers. This is a follow up to Ifecb831c0adf4086a9d39feb3eb7e3865db780e6 ("skip built-in exceptions when adding error_summary") that already skipped some builtin exceptions. Change-Id: Ia8f7e2501a49345b8d828ec3ee4a7cc870c8a0a8
-rw-r--r--oslo_log/formatters.py2
-rw-r--r--oslo_log/tests/unit/test_log.py5
2 files changed, 5 insertions, 2 deletions
diff --git a/oslo_log/formatters.py b/oslo_log/formatters.py
index 6195deb..2f9f979 100644
--- a/oslo_log/formatters.py
+++ b/oslo_log/formatters.py
@@ -119,7 +119,7 @@ def _get_error_summary(record):
if not exc_info[0]:
exc_info = None
elif exc_info[0] in (TypeError, ValueError,
- KeyError, AttributeError):
+ KeyError, AttributeError, ImportError):
# NOTE(dhellmann): Do not include information about
# common built-in exceptions used to detect cases of
# bad or missing data. We don't use isinstance() here
diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py
index 1df8a02..a946d9e 100644
--- a/oslo_log/tests/unit/test_log.py
+++ b/oslo_log/tests/unit/test_log.py
@@ -702,7 +702,10 @@ class ContextFormatterTestCase(LogTestBase):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = self.trans_fixture.lazy('test ' + six.unichr(128))
- for ignore in [ValueError, TypeError, KeyError, AttributeError]:
+ ignored_exceptions = [
+ ValueError, TypeError, KeyError, AttributeError, ImportError
+ ]
+ for ignore in ignored_exceptions:
try:
raise ignore('test_exception_logging')
except ignore as e: