• Register multiple excepthooks?

    From Albert-Jan Roskam@21:1/5 to All on Sun Jul 31 11:39:54 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From avi.e.gross@gmail.com@21:1/5 to All on Sun Jul 31 22:43:49 2022
    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.




    -----Original Message-----
    From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Albert-Jan Roskam
    Sent: Sunday, July 31, 2022 5:40 AM
    To: Python-list@python.org
    Subject: Register multiple excepthooks?

    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
    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Albert-Jan Roskam on Mon Aug 1 19:34:49 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Albert-Jan Roskam@21:1/5 to All on Thu Aug 4 09:41:00 2022
    On Aug 1, 2022 19:34, Dieter Maurer <dieter@handshake.de> wrote:

    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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From 2QdxY4RzWzUUiLuE@potatochowder.com@21:1/5 to Albert-Jan Roskam on Thu Aug 4 06:13:28 2022
    On 2022-08-04 at 09:41:00 +0200,
    Albert-Jan Roskam <sjeik_appie@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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)