[Python-ideas] Re: Better exception handling hygiene

2021-10-02 Thread Soni L.
On 2021-10-01 8:26 a.m., Steven D'Aprano wrote: > I wish your proposals and ideas would be more *precise* in their > specifications. This is not the first time that I have found it very > hard to work out precisely what you are suggesting and what you are not. > > Thanks for insisting on cont

[Python-ideas] Re: Better exception handling hygiene

2021-10-01 Thread Soni L.
On 2021-10-01 8:26 a.m., Steven D'Aprano wrote: > On Thu, Sep 30, 2021 at 01:25:42PM -0300, Soni L. wrote: > > >     def foo(): > >     raise Bar > >     def baz() with Bar: > >     foo() > >     baz() > > > > would make a RuntimeError, because foo raised a Bar and the VM sees that > > B

[Python-ideas] Re: Better exception handling hygiene

2021-10-01 Thread Steven D'Aprano
On Fri, Oct 01, 2021 at 06:43:40AM -0300, Soni L. wrote: > Look for the clarification post we posted as a reply to the original post. Who is "we"? If you are posting this in collaboration with other people, it would be polite to introduce them. -- Steve ___

[Python-ideas] Re: Better exception handling hygiene

2021-10-01 Thread Steven D'Aprano
On Thu, Sep 30, 2021 at 01:25:42PM -0300, Soni L. wrote: >     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. So the syntax "with SomeException" in a functio

[Python-ideas] Re: Better exception handling hygiene

2021-10-01 Thread Soni L.
On 2021-10-01 2:04 a.m., Steven D'Aprano wrote: > Let's get to the fundamental problem with this. It is DWIM magic, and > you haven't (as far as I have seen) yet specified how we are supposed to > use it or predict how it is supposed to work. > > Here is your syntax again: > > > > def a_pot

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Chris Angelico
On Fri, Oct 1, 2021 at 3:02 PM Steven D'Aprano wrote: > We might take the point of view that StopIteration is reserved for use > by iterators, and that the interpreter reserves the use to treat *any* > use of StopIteration in any other context as undefined behaviour, and > that we ought to be grat

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Steven D'Aprano
Let's get to the fundamental problem with this. It is DWIM magic, and you haven't (as far as I have seen) yet specified how we are supposed to use it or predict how it is supposed to work. Here is your syntax again: > > def a_potentially_recursive_function(some, args) with > > ExceptionWeC

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Steven D'Aprano
On Thu, Sep 30, 2021 at 07:41:47AM -0300, Soni L. wrote: > You misnderstand exception hygiene. It isn't about "do the least stuff > in try blocks", but about "don't shadow unrelated exceptions into your > public API". I disagree with that. I don't think that "exception hygiene" is a common term

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Soni L.
On 2021-09-30 6:08 p.m., Chris Angelico wrote: > I still think that your use of LookupError is confusing the issue > somewhat, partly because it's a base class rather than something > that's ever raised per se. > > If you were using a custom exception type, how likely is it that that > exception

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Chris Angelico
On Fri, Oct 1, 2021 at 6:12 AM Soni L. wrote: > > Congratulations, you showed me some extremely complicated code, and > > then got miffed when I messed up something in trying to simplify it. > > Well done, you successfully tripped me up in my attempt to make code > > better. > > Fair enough, we we

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Soni L.
On 2021-09-30 5:34 p.m., Barry Scott wrote: > > >> On 30 Sep 2021, at 17:25, Soni L. > > wrote: >> >> 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: >> >

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Barry Scott
> On 30 Sep 2021, at 17:25, Soni L. wrote: > > 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, simil

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Soni L.
On 2021-09-30 2:04 p.m., Chris Angelico wrote: > On Fri, Oct 1, 2021 at 1:10 AM Soni L. wrote: > > > > > > > > On 2021-09-30 11:23 a.m., Chris Angelico wrote: > > > > For example this: (real code) > > > > > > > > def get_property_values(self, prop): > > > > try: > > > > f

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Brendan Barnwell
On 2021-09-30 09:25, Soni L. wrote: 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()

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Chris Angelico
On Fri, Oct 1, 2021 at 1:10 AM Soni L. wrote: > > > > On 2021-09-30 11:23 a.m., Chris Angelico wrote: > > > For example this: (real code) > > > > > > def get_property_values(self, prop): > > > try: > > > factory = self.get_supported_properties()[prop] > > > except K

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Soni L.
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 chec

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Soni L.
On 2021-09-30 11:23 a.m., Chris Angelico wrote: > > For example this: (real code) > > > > def get_property_values(self, prop): > > try: > > factory = self.get_supported_properties()[prop] > > except KeyError as exc: raise PropertyError from exc > > iterator

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Chris Angelico
On Fri, Oct 1, 2021 at 12:03 AM Soni L. wrote: > > > But what you're talking about doesn't have this clear distinction, > > other than in *your own definitions*. You have deemed that, in some > > areas, a certain exception should be turned into a RuntimeError; but > > in other areas, it shouldn't.

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Soni L.
On 2021-09-30 10:08 a.m., Chris Angelico wrote: > On Thu, Sep 30, 2021 at 8:43 PM Soni L. wrote: > > You misnderstand exception hygiene. It isn't about "do the least stuff > > in try blocks", but about "don't shadow unrelated exceptions into your > > public API". > > > > For example, generators

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Chris Angelico
On Thu, Sep 30, 2021 at 8:43 PM Soni L. wrote: > You misnderstand exception hygiene. It isn't about "do the least stuff > in try blocks", but about "don't shadow unrelated exceptions into your > public API". > > For example, generators don't allow you to manually raise StopIteration > anymore: > >

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Soni L.
On 2021-09-30 4:15 a.m., Steven D'Aprano wrote: > On Thu, Sep 30, 2021 at 12:03:37AM -0300, Soni L. wrote: > > > > Only some.user_code is guarded by the try block. If it turns out that > > > code_we_assume_is_safe is not actually safe, and fails with an > > > exception, it won't be caught by th

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Steven D'Aprano
On further thought, maybe something like this is what you are looking for? https://code.activestate.com/recipes/580808-guard-against-an-exception-in-the-wrong-place/ This allows you to replace one or more kinds of exceptions with another specified exception (defaults to RuntimeError) using a co

[Python-ideas] Re: Better exception handling hygiene

2021-09-30 Thread Steven D'Aprano
On Thu, Sep 30, 2021 at 12:03:37AM -0300, Soni L. wrote: > > Only some.user_code is guarded by the try block. If it turns out that > > code_we_assume_is_safe is not actually safe, and fails with an > > exception, it won't be caught by the try block and you will know about > > it. > > Except no

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Soni L.
On 2021-09-29 11:46 p.m., Steven D'Aprano wrote: > In Soni's original code snippet, there is a clear separation of code > that is inside the try block from code that is outside the try block: > > > def a_potentially_recursive_function(some, args): > >   try: > > some.user_code() > > ex

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Steven D'Aprano
In Soni's original code snippet, there is a clear separation of code that is inside the try block from code that is outside the try block: > def a_potentially_recursive_function(some, args): >   try: > some.user_code() > except ExceptionWeCareAbout as exc: > raise RuntimeError fro

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Steven D'Aprano
On Wed, Sep 29, 2021 at 10:01:52PM -0300, 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: Is that the Royal We or are you actually speaking on behalf of other pe

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Soni L.
On 2021-09-29 10:09 p.m., Chris Angelico wrote: > On Thu, Sep 30, 2021 at 11:03 AM 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_potent

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Chris Angelico
On Thu, Sep 30, 2021 at 11:03 AM 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