diff options
author | Jason Madden <jamadden@gmail.com> | 2018-09-25 16:48:25 -0500 |
---|---|---|
committer | Jason Madden <jamadden@gmail.com> | 2018-09-25 16:48:25 -0500 |
commit | 13ffadc2f097bc463a597f577fc932ced13fa3b7 (patch) | |
tree | e32eab5e85789d0b05da043429208fd378d739f8 | |
parent | 6fa6e4824182f63e23d0ee36c06f4af2d5af50b2 (diff) | |
download | zope-configuration-issue10.tar.gz |
Allow customization of which exceptions should pass through ConfigurationMachine.issue10
Fixes #10.
-rw-r--r-- | CHANGES.rst | 7 | ||||
-rw-r--r-- | src/zope/configuration/config.py | 15 | ||||
-rw-r--r-- | src/zope/configuration/tests/test_config.py | 22 |
3 files changed, 40 insertions, 4 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 99874f1..eb5e594 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -46,6 +46,13 @@ Changes ``fromUnicode``. See `issue 28 <https://github.com/zopefoundation/zope.configuration/issues/28>`_. +- Add ``ConfigurationMachine.pass_through_exceptions`` to allow + customizing the exceptions that + ``ConfigurationMachine.execute_actions`` wraps in a + ``ConfigurationExecutionError``. See `issue 10 + <https://github.com/zopefoundation/zope.configuration/issues/10>`_. + + 4.1.0 (2017-04-26) ------------------ diff --git a/src/zope/configuration/config.py b/src/zope/configuration/config.py index 774b265..61c37a8 100644 --- a/src/zope/configuration/config.py +++ b/src/zope/configuration/config.py @@ -333,6 +333,14 @@ class ConfigurationMachine(ConfigurationAdapterRegistry, ConfigurationContext): includepath = () info = '' + #: These exceptions are allowed to be raised from `execute_actions` + #: without being re-wrapped into a `ConfigurationExecutionError`. + #: Users of instances of this class may modify this before calling `execute_actions` + #: if they need to propagate specific exceptions. + #: + #: .. versionadded:: 4.2.0 + pass_through_exceptions = (KeyboardInterrupt, SystemExit) + def __init__(self): super(ConfigurationMachine, self).__init__() self.actions = [] @@ -367,6 +375,9 @@ class ConfigurationMachine(ConfigurationAdapterRegistry, ConfigurationContext): This calls the action callables after resolving conflicts. """ + pass_through_exceptions = self.pass_through_exceptions + if testing: + pass_through_exceptions = BaseException try: for action in resolveConflicts(self.actions): callable = action['callable'] @@ -377,11 +388,9 @@ class ConfigurationMachine(ConfigurationAdapterRegistry, ConfigurationContext): info = action['info'] try: callable(*args, **kw) - except (KeyboardInterrupt, SystemExit): # pragma: no cover + except pass_through_exceptions: raise except: - if testing: - raise t, v, tb = sys.exc_info() try: reraise(ConfigurationExecutionError(t, v, info), diff --git a/src/zope/configuration/tests/test_config.py b/src/zope/configuration/tests/test_config.py index 4835df9..42e7e2b 100644 --- a/src/zope/configuration/tests/test_config.py +++ b/src/zope/configuration/tests/test_config.py @@ -766,10 +766,30 @@ class ConfigurationMachineTests(_ConformsToIConfigurationContext, cm.action(None, _err) with self.assertRaises(ConfigurationExecutionError) as exc: cm.execute_actions() - self.assertTrue(exc.exception.etype is ValueError) + self.assertIs(exc.exception.etype, ValueError) self.assertEqual(str(exc.exception.evalue), "XXX") self.assertEqual(exc.exception.info, "INFO") + def _check_execute_actions_w_errors_wo_testing(self, ex_kind): + ex = ex_kind('XXX') + def _err(*args, **kw): + raise ex + cm = self._makeOne() + cm.info = 'INFO' + cm.action(None, _err) + with self.assertRaises(ex_kind) as exc: + cm.execute_actions() + + self.assertIs(exc.exception, ex) + + def test_execute_actions_w_errors_wo_testing_SystemExit(self): + # It gets passed through as-is + self._check_execute_actions_w_errors_wo_testing(SystemExit) + + def test_execute_actions_w_errors_wo_testing_KeyboardInterrupt(self): + # It gets passed through as-is + self._check_execute_actions_w_errors_wo_testing(KeyboardInterrupt) + def test_keyword_handling(self): # This is really an integraiton test. from zope.configuration.config import metans |