New Idea: A safe keyword to execute the following statement in a failsafe mode.

Sometimes, we need to execute a statement, which might throw an exception and we want to return None if it causes an exception. In short, we want to run that command as failsafe mode, so that it doesn't cause abnormal termination in case of any exception. *Approach till now:* def log_to_telegram(text): ''' Suppose you want to send some log to some telegram channel using telegram bot api. It might raise Network Error, or Invalid Bot token error. ''' send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a function wrapper for telegram bot api. def some_func(): try: log_to_telegram('This is a not important message. It's okay even if it isn\'t delivered.') except: pass # Next work *Suggested approach:* # consider the same log_to_telegram function as defined in previous example def some_func(): safe log_to_telegram('This is a not important message. It's okay even if it isn\'t delivered.') # safe keyword ensures that it raises no exception and the process isn't terminated even if log_to_telegram function raises an exception. *Other examples:* *1. By default, it will return None.* * Example: * try: file = open('some_file') except: file = None * Can be rewritten as:* file = safe open('some_file') *2. We can provide a return value explicitly which should be returned in case of some exception.* * Example: * try: division = a/b except: division = 0 * Can be rewritten as:* division = safe(0) a/b *3. We can set what exceptions we want to be 'safed'.* * Example: * try: element = some_list[index]/divisor except ZeroDivisionError: element = 0 # It will make element zero if divisor is zero, but it will not except IndexError in case index is out of range for some_list * Can be rewritten as:* element = safe(0, ZeroDivisionError) some_list[index]/divisor *I think this keyword should be there in python, it will make code short and readable. *

sounds very much like https://www.python.org/dev/peps/pep-0463/#rejection-notice I'm concerned with the `safe` defaulting to a bare `except:` which will also catch CancelledError other errors that should be re-raised also ``` file = safe open('some_file') ``` does not provide a way to manage the file with a context manager: ``` f = safe open("some_file") if f is None: # do something else: with f: # do something else ``` seems no improvement than the current: ``` try: f = open("some_file") except OSError: # do something else: with f: # do something else ```

I think you can already do all of this with a custom exception-swallowing decorator function. Something like this: from functools import wraps def swallow(*exceptions, default=Exception, result=None): if not exceptions: exceptions = Exception, def decorator(func): @wraps(func) def wrapped(*args, **kwargs): try: return func(*args, **kwargs) except exceptions: return result return wrapped return decorator Then just write: @swallower() def some_func(): log_to_telegram('This is a not important message. It's okay even if it isn\'t delivered.') For divide by zero errors you need to put the division operation in a lambda function: safe_divide = swallow(DivideByZero, result=0) division = safe_divide(lambda: a/b) I didn't test any of this but it should be working. On Sun, May 23, 2021, 8:54 AM Shivam Saini <shivamsn97@gmail.com> wrote:

That wont be an oneliner still. We can add decorator to a function, and that don't even be very readable. If we can convert that decorator to an inbuilt keyword that would work as an one liner and would be very readable too. On Sun, 23 May 2021, 18:56 Ricky Teachey, <ricky@teachey.org> wrote:

FYI, Something very similar already exists in the standard library, contextlib.suppress: https://docs.python.org/3/library/contextlib.html#contextlib.suppress It makes a nice 2+ liner for a lot of situations: with suppress(Exception): ... Seems more flexible than OPs keyword suggestion as you can fit an entire block of code in there, not just a single expression. And it's quite semantically expressive. Damian (he/him) On Sun, May 23, 2021 at 9:28 AM Ricky Teachey via Python-ideas < python-ideas@python.org> wrote:

23.05.21 12:42, Shivam Saini пише:
except: pass
Don't do this. Never write a bare except handler which does not re-raise an exception. There are few exceptions of this rule, but it is unlikely that you will see them in first years of your practice. It is an anti-pattern, and a feature that facilitates its use will never be added. Always specify exceptions which you expect to catch. If you want to look cool, you can use contextlib.suppress(), although it can make further refactoring more difficult.

sounds very much like https://www.python.org/dev/peps/pep-0463/#rejection-notice I'm concerned with the `safe` defaulting to a bare `except:` which will also catch CancelledError other errors that should be re-raised also ``` file = safe open('some_file') ``` does not provide a way to manage the file with a context manager: ``` f = safe open("some_file") if f is None: # do something else: with f: # do something else ``` seems no improvement than the current: ``` try: f = open("some_file") except OSError: # do something else: with f: # do something else ```

I think you can already do all of this with a custom exception-swallowing decorator function. Something like this: from functools import wraps def swallow(*exceptions, default=Exception, result=None): if not exceptions: exceptions = Exception, def decorator(func): @wraps(func) def wrapped(*args, **kwargs): try: return func(*args, **kwargs) except exceptions: return result return wrapped return decorator Then just write: @swallower() def some_func(): log_to_telegram('This is a not important message. It's okay even if it isn\'t delivered.') For divide by zero errors you need to put the division operation in a lambda function: safe_divide = swallow(DivideByZero, result=0) division = safe_divide(lambda: a/b) I didn't test any of this but it should be working. On Sun, May 23, 2021, 8:54 AM Shivam Saini <shivamsn97@gmail.com> wrote:

That wont be an oneliner still. We can add decorator to a function, and that don't even be very readable. If we can convert that decorator to an inbuilt keyword that would work as an one liner and would be very readable too. On Sun, 23 May 2021, 18:56 Ricky Teachey, <ricky@teachey.org> wrote:

FYI, Something very similar already exists in the standard library, contextlib.suppress: https://docs.python.org/3/library/contextlib.html#contextlib.suppress It makes a nice 2+ liner for a lot of situations: with suppress(Exception): ... Seems more flexible than OPs keyword suggestion as you can fit an entire block of code in there, not just a single expression. And it's quite semantically expressive. Damian (he/him) On Sun, May 23, 2021 at 9:28 AM Ricky Teachey via Python-ideas < python-ideas@python.org> wrote:

23.05.21 12:42, Shivam Saini пише:
except: pass
Don't do this. Never write a bare except handler which does not re-raise an exception. There are few exceptions of this rule, but it is unlikely that you will see them in first years of your practice. It is an anti-pattern, and a feature that facilitates its use will never be added. Always specify exceptions which you expect to catch. If you want to look cool, you can use contextlib.suppress(), although it can make further refactoring more difficult.
participants (6)
-
Damian Shaw
-
Ricky Teachey
-
Serhiy Storchaka
-
Shivam Saini
-
Stestagg
-
Thomas Grainger