Package: cs.excutils
Convenience facilities for managing exceptions.Latest release 20230212.1:
Fix imports.Functionexc_fold(*da, **dkw)Decorator to catch specific exception types and return a defined default value.Functionlogexc(func)Decorator to log exceptions and reraise.Functionlogexc_gen(genfunc)Decorator to log exceptions and reraise for generators.FunctionLogExceptions(conceal=False)Wrapper forNoExceptionswhich reports exceptions and optionally
suppresses them.Functionnoexc(func)Decorator to wrap a function which should never raise an exception.
Instead, any raised exception is attempted to be logged.A significant side effect is of course that if the function raises an
exception it now returnsNone.
My primary use case is actually to wrap logging functions,
which I have had abort otherwise sensible code.Functionnoexc_gen(func)Decorator to wrap a generator which should never raise an exception.
Instead, any raised exception is attempted to be logged and iteration ends.My primary use case is wrapping generators chained in a pipeline,
as in cs.later.Later.pipeline.ClassNoExceptionsA context manager to catchallexceptions and log them.Arguably this should be a bare try...except but that's syntacticly
noisy and separates the catch from the top.
For simple function callsreturn_exc_info()is probably better.MethodNoExceptions.__init__(self, handler):
Initialise theNoExceptionscontext manager.Thehandleris a callable which
expects(exc_type,exc_value,traceback)and returnsTrueorFalsefor the__exit__method of the manager.
IfhandlerisNone, the__exit__method
always returnsTrue, suppressing any exception.Functionreturn_exc_info(func, *args, **kwargs)Run the supplied function and arguments.
Return(func_return, None)in the case of successful operation
and(None, exc_info)in the case of an exception.exc_infois a 3-tuple of(exc_type, exc_value, exc_traceback)as returned bysys.exc_info().
If you need to protect a whole suite and would rather not move it
into its own function, consider the NoExceptions context manager.Functionreturns_exc_info(func)Decorator function to wrap functions whose exceptions should be caught,
such as inside event loops or worker threads.It causes a function to return(func_return, None)in the case of successful operation
and(None, exc_info)in the case of an exception.exc_infois a 3-tuple of(exc_type, exc_value, exc_traceback)as returned bysys.exc_info().Functionsafe_property(func)Substitute for @property which lets AttributeErrors escape as RuntimeErrors.Functiontransmute(*da, **dkw)Decorator to transmute an inner exception to another exception type.The motivating use case is properties in a class with a__getattr__method;
if some inner operation of the property function raisesAttributeErrorthen the property is bypassed in favour of__getattr__.
Confusion ensues.In principle this can be an issue with any exception raised
from "deeper" in the call chain, which can be mistaken for a
"shallow" exception raised by the function itself.Functionunattributable(func)Decorator to transmuteAttributeErrorinto aRuntimeError.Functionunimplemented(func)Decorator for stub methods that must be implemented by a stub class.Release LogRelease 20230212.1:
Fix imports.Release 20230212:
Import cs.gimmicks instead of cs.logutils, reduces dependencies.Release 20221228:
Get warning etc from cs.gimmicks, breaks circular import with cs.logutils.Release 20221207:
@unattributable: bugfix decorator construction to accomodate callable() @decorator precheck.Release 20210123:
@transmute: refactor to raise chained exceptions in Python 3+.Release 20190812:
LogExceptions: drop stack trace noise.Release 20190220:
New decorator @exc_fold to catch particular exceptions, log an error and return a defined value.Release 20190101:
@logexc: handle missing func.name.Release 20170904:
Minor updates, improved docstring.Release 20160828:@unattributable and @safe_property decorators, used to protect properties from inner AttributeErrors.Improved exception practices.Release 20150118:
metadata updatesRelease 20150110:
Initial distinfo for pypi release.