Alright, some ppl asked us to rephrase this, so: The plan is to take the function syntax:
def name(args): and add an optional "with" to it: def name(args) with exceptions: these then get added to the function, similar to e.g. default args. when an exception is thrown*, the VM then checks these and converts relevant exceptions into RuntimeError, e.g.: def foo(): raise Bar def baz() with Bar: foo() baz() would make a RuntimeError, because foo raised a Bar and the VM sees that Bar is in baz's with. *except "raise" opcodes SKIP checking these (within the context of the function), so the following: def baz() with Bar: raise Bar baz() raises a Bar, not a RuntimeError from a Bar. You can then document your exceptions as you normally do: "This raises Bar when so and so are invalid", and then add "with Bar" to your function. the VM then makes sure that Bar is only raised when so and so are invalid, but under no other circumstances. e.g. someone monkeypatched your function to try and raise a Bar outside of the place it was meant to raise a Bar? it'll raise a RuntimeError instead. this gives you far more control over what your API exceptions, the exceptions you documented in your function's documentation, are, and especially under which conditions they'll actually be raised (and be catchable by the caller). this is what we mean by exception hygiene. On 2021-09-29 10:01 p.m., Soni L. wrote: > So uh, this is a hardly at all fleshed out idea, but one thing we really > don't like about python is having to do stuff like this so as to not > swallow exceptions: > > def a_potentially_recursive_function(some, args): > """ > Does stuff and things. > Raises ExceptionWeCareAbout under so and so conditions. > """ > try: some.user_code() except ExceptionWeCareAbout as exc: raise > RuntimeError from exc > code_we_assume_is_safe() > if args.something and some_condition: > raise ExceptionWeCareAbout > > It'd be nice if there was a way to... make this easier to deal with. > Perhaps something where, something like this: > > def a_potentially_recursive_function(some, args) with ExceptionWeCareAbout: > """ > Does stuff and things. > Raises ExceptionWeCareAbout under so and so conditions. > """ > some.user_code() > code_we_assume_is_safe() > if args.something and some_condition: > raise ExceptionWeCareAbout > > becomes: > > def a_potentially_recursive_function(some, args): > """ > Does stuff and things. > Raises ExceptionWeCareAbout under so and so conditions. > """ > try: > some.user_code() > code_we_assume_is_safe() > if args.something and some_condition: > let_exception_through = True > raise ExceptionWeCareAbout > except ExceptionWeCareAbout as exc: > if let_exception_through: > raise > else: > raise RuntimeError from exc > > (or equivalent) > > and something like: > > def foo() with Bar: > try: baz() except Bar: raise > > becomes: > > def foo(): > try: > try: > baz() > except Bar: > allow_exception = True > raise > except Bar as exc: > if allow_exception: > raise > else: > raise RuntimeError from exc > > (thus allowing explicit exception propagation) > > Additionally, note that it would only apply to the raise itself - > something like `raise may_raise_the_same_error()` would desugar as if: > > exception = may_raise_the_same_error() > allow_exception = True > raise exception > > Obviously this doesn't solve exception handling, doesn't require the > caller to catch the exceptions, etc etc. It does, however, encourage > better exception hygiene. Chances are something like this would > significantly reduce the amount of swallowed exceptions, if it gets > widely adopted. We know something like this would've saved us a lot of > trouble, so we're sharing the idea in the hopes it can, in the future, > help others. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/KMSLDPNK3UVP42YMWUWFR3RK6JXFZFGK/ Code of Conduct: http://python.org/psf/codeofconduct/