Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Register multiple excepthooks?

25 views
Skip to first unread message

Albert-Jan Roskam

unread,
Jul 31, 2022, 4:13:11 PM7/31/22
to
Hi,
I have a function init_logging.log_uncaught_errors() that I use for
sys.excepthook. Now I also want to call another function (ffi.dlclose())
upon abnormal termination. Is it possible to register multiple
excepthooks, like with atexit.register? Or should I rename/redefine
log_uncaught_errors() so it does both things?
Thanks!
Albert-Jan

avi.e...@gmail.com

unread,
Jul 31, 2022, 10:44:16 PM7/31/22
to
Albert-Jan,

Unless there is something special in your scenario, aren't there many ways
within python proper to create functions that effectively call another
function or more as needed? Think decorators as one example.

Of course if the caller expects some specific result when it calls your
function, running a single function that returns say the value of the last
thing it does, may not meet your needs.

Now assuming all your functions take the same required argument and do not
in any way tamper with the argument, you could indeed define a function that
accepts arguments and internally calls one after another other functions in
some order and intercepts anything they return (or errors) and consolidates
them as needed and returns a result. I suspect there is a module that
provides such functionality.
--
https://mail.python.org/mailman/listinfo/python-list

Dieter Maurer

unread,
Aug 1, 2022, 1:35:17 PM8/1/22
to
Albert-Jan Roskam wrote at 2022-7-31 11:39 +0200:
> I have a function init_logging.log_uncaught_errors() that I use for
> sys.excepthook. Now I also want to call another function (ffi.dlclose())
> upon abnormal termination. Is it possible to register multiple
> excepthooks, like with atexit.register? Or should I rename/redefine
> log_uncaught_errors() so it does both things?

`sys.excepthook` is a single function (not a list of them).
This means: at any moment a single `excepthook` is effective.

If you need a modular design, use a dispatcher function
as your `excepthook` associated with a registry (e.g. a `list`).
The dispatcher can then call all registered function.

Albert-Jan Roskam

unread,
Aug 4, 2022, 3:41:34 AM8/4/22
to
====
Thank you both. I'll give this a try. I think it would be nice if the
standard library function atexit.register would be improved, such that the
registered functions would not only be called upon (a) normal program
termination, but that one could also register functions that are called
(b) upon error (c) unconditionally. Much like (a) try - (b) except - (c)
finally.

2QdxY4Rz...@potatochowder.com

unread,
Aug 4, 2022, 7:14:05 AM8/4/22
to
On 2022-08-04 at 09:41:00 +0200,
Albert-Jan Roskam <sjeik...@hotmail.com> wrote:

> Thank you both. I'll give this a try. I think it would be nice if
> the standard library function atexit.register would be improved,
> such that the registered functions would not only be called upon
> (a) normal program termination, but that one could also register
> functions that are called (b) upon error (c) unconditionally. Much
> like (a) try - (b) except - (c) finally.

There. You've just designed the top level of a better behaved, more
robust application (completely untested):

on_normal_exit_funcs = list()
on_error_exit_funcs = list()
on_any_exit_funcs = list()

def run_exit_funcs(exit_funcs):
for func in exit_funcs:
try: func()
except e: maybe_do_some_logging(e)

try:
run_the_application()
run_exit_funcs(on_normal_exit_funcs)
except:
run_exit_funcs(on_error_exit_funcs)
finally:
run_exit_funcs(on_any_exit_funcs)

def register_normal_exit_func(f):
on_normal_exit_funcs.append(f)

def register_error_exit_func(f):
on_error_exit_funcs.append(f)

def register_any_exit_func(f):
on_any_exit_funcs.append(f)

No, really. There are worse ways to build an extremely generic, fairly
minimalist application framework.

Season to taste,น add it to your personal (or company) toolbox, and
refine and improve it as things come up. You may discover some number
of common exit functions that are useful across appliations, too.

น are you object oriented, functional, imperative, or something else?
do you like long names, short names, names that follow some existing
coding standard, non-English names? do you have a standardized
logging/exception library?
0 new messages