zopefoundation / zope.configuration

Extensible system for supporting various kinds of configurations
https://zopeconfiguration.readthedocs.io
Other
1 stars 6 forks source link

Allow customization of which exceptions should pass through Configurationmachine #34

Closed jamadden closed 5 years ago

jamadden commented 5 years ago

Fixes #10.

jamadden commented 5 years ago

I had a thought: I wonder if we would be better off changing the bare except: to except Exception:? In both Python 2 and Python 3, there are only three standard exceptions that are just a BaseException and not an Exception: SystemExit, KeyboardInterrupt, and GeneratorExit. The first two of those we are already catching and re-raising, the latter has interesting semantics.

There are some third-party libraries, like gevent, that do introduce new subclasses of BaseException that they really want to be able to catch themselves. In gevent's case, its for implementing timeouts. Our bare except: breaks that. And of course it produces linter warnings.

This would apply to the xmlconfig module too, I think. Here, in the config module, it would look something like:

class ConfigurationMachine(object):
    pass_through_exceptions = () # Things specifically raised. Empty by default because by default we only catch exception

    def execute_actions(..., testing=False):
        pass_through = self.pass_through_exceptions if not testing else BaseException

        try:

        except pass_through: raise
        except Exception:
              reraise(...)
        # SystemExit, KeyboardInterrupt, GeneratorExit, and other BaseExceptions automatically pass
jamadden commented 5 years ago

I see the plus one and run with it. Behaviour changed and tests added.

mgedmin commented 5 years ago

FWIW the +1 was for except -> except Exception.

I'm +0 for the except: if testing: raise -> except pass_through: raise.

I'm -0.5 for except self.testing: raise.

jamadden commented 5 years ago

Thanks!

I intend to release after merging this, leaving #9 for later.