no_except¶
Exception-safe function execution utilities.
This module provides functions that wrap other functions to catch and handle exceptions, returning the exception object instead of propagating it. The logging module is used directly (rather than alog) to keep this a low-level module with minimal local dependencies.
Example
res = no_except.no_except(int, 'x') # ValueError object (not raised)
no_except(fn, *args, **kwargs)
¶
Call fn with the given args, catching and returning any exception.
Executes fn(*args, **kwargs). If an exception occurs, logs it at WARNING level and returns the exception object instead of propagating it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
The callable to execute. |
required | |
*args
|
Positional arguments to pass to fn. |
()
|
|
**kwargs
|
Keyword arguments to pass to fn. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The return value of fn on success, or the Exception object if an |
|
|
exception was raised. |
Example
res = no_except.no_except(int, 'x') # ValueError - not raised
res = no_except.no_except(int, '42') # 42
Source code in py_misc_utils/no_except.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
qno_except(fn, *args, **kwargs)
¶
Call fn with the given args, quietly returning any exception.
Like no_except() but without logging. If fn raises, the exception object is returned directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
The callable to execute. |
required | |
*args
|
Positional arguments to pass to fn. |
()
|
|
**kwargs
|
Keyword arguments to pass to fn. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The return value of fn on success, or the Exception object if an |
|
|
exception was raised. |
Example
res = no_except.qno_except(int, 'x') # ValueError, not raised/logged
Source code in py_misc_utils/no_except.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
xwrap_fn(fn, *args, **kwargs)
¶
Wrap fn so it can be called later with no arguments, catching exceptions.
Returns a zero-argument callable that, when invoked, calls no_except(fn, *args, **kwargs). The returned callable never raises; it returns either fn's result or the exception object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
The callable to wrap. |
required | |
*args
|
Positional arguments to pass to fn. |
()
|
|
**kwargs
|
Keyword arguments to pass to fn. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A zero-argument callable fwrap such that fwrap() == no_except(fn, |
|
|
*args, **kwargs). |
Source code in py_misc_utils/no_except.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |