Skip to main content

Convenience facilities for managing exceptions.

Project description

Convenience facilities for managing exceptions.

Latest release 20250306: Drop @safe_property, will be using @property @unattributable instead.

exc_fold(*da, **dkw)

Decorator to catch specific exception types and return a defined default value.

logexc(*da, **dkw)

Decorator to log exceptions and reraise.

logexc_gen(*da, **dkw)

Decorator to log exceptions and reraise for generators.

LogExceptions(log=None, conceal=False)

Wrapper for NoExceptions which reports exceptions and optionally suppresses them.

noexc(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 returns None. My primary use case is actually to wrap logging functions, which I have had abort otherwise sensible code.

noexc_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.

Class NoExceptions

A context manager to catch all exceptions 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 calls return_exc_info() is probably better.

NoExceptions.__init__(self, handler): Initialise the NoExceptions context manager.

The handler is a callable which expects (exc_type,exc_value,traceback) and returns True or False for the __exit__ method of the manager. If handler is None, the __exit__ method always returns True, suppressing any exception.

return_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_info is a 3-tuple of (exc_type, exc_value, exc_traceback) as returned by sys.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.

returns_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_info is a 3-tuple of (exc_type, exc_value, exc_traceback) as returned by sys.exc_info().

transmute(*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 raises AttributeError then 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.

unattributable(func)

Decorator to transmute AttributeError into a RuntimeError.

unimplemented(func)

Decorator for stub methods that must be implemented by a stub class.

Release Log

Release 20250306: Drop @safe_property, will be using @property @unattributable instead.

Release 20240630:

  • LogExceptions: new optinal log param for the lgging call.
  • @logexc: use @decorator like other civilised decorators, pass decorator params to LogExceptions.

Release 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 updates

Release 20150110: Initial distinfo for pypi release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs_excutils-20250306.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs_excutils-20250306-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cs_excutils-20250306.tar.gz.

File metadata

  • Download URL: cs_excutils-20250306.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for cs_excutils-20250306.tar.gz
Algorithm Hash digest
SHA256 c1484cee7a660e0dfcccf48122d21d2b68571656204406466132474d949d661f
MD5 0f8c25a52aa729c2b453125540edf1c2
BLAKE2b-256 52eadc383529605576ade2ff35e5fa73d9c69c6b2e9c8d2762ad551afea6f82d

See more details on using hashes here.

File details

Details for the file cs_excutils-20250306-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cs_excutils-20250306-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 de39447be822afdee6e093930fe70034476cb7a58dc2ed6696a387c978540738
MD5 c6eea1746256b1a4290179e2f37c4312
BLAKE2b-256 caced775550b9288be7eea534eabb5b4f1d209dd70d0cb27fa9bef3b7ca65c43

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page