Re: [Python-ideas] exception instantiation philosophy and practice [was: Let try-except check the exception instance]

2018-05-31 Thread Greg Ewing
Ethan Furman wrote: Why is this? Doesn't the exception have to be instantiated at some point, even if just to print to stderr? If it gets caught by an except clause without an else clause, in theory there's no need to instantiate it. However, Python doesn't currently seem to take advantage o

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Steven D'Aprano
On Thu, May 31, 2018 at 11:03:56AM -0700, Brendan Barnwell wrote: > What I don't understand is this: if we believe that, then why was > comprehension-leaking EVER removed? Everything that I've seen > advocating for this kind of leaking seems to me like it is much more > logically consist

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Brendan Barnwell
On 2018-05-31 05:53, Steven D'Aprano wrote: Bottom line is, if you think it is okay that the following assignment to x affects the local scope: results = [] for a in seq: # using "given" to avoid arguments about := y = (x given x = a)+1 results.append(y)

Re: [Python-ideas] exception instantiation philosophy and practice [was: Let try-except check the exception instance]

2018-05-31 Thread Chris Angelico
On Fri, Jun 1, 2018 at 12:52 AM, Antoine Pitrou wrote: > On Thu, 31 May 2018 07:49:58 -0700 > Ethan Furman wrote: >> On 05/31/2018 07:36 AM, Nick Coghlan wrote: >> >> > The exception machinery deliberately attempts to avoid instantiating >> > exception objects whenever it can, but that gets >> >

Re: [Python-ideas] Let try-except check the exception instance

2018-05-31 Thread Alexander Belopolsky
On Thu, May 31, 2018 at 10:37 AM Nick Coghlan wrote: > > The exception machinery deliberately attempts to avoid instantiating exception objects whenever it can, but that gets significantly more difficult if we always need to create the instance before we can decide whether or not the raised except

Re: [Python-ideas] Let try-except check the exception instance

2018-05-31 Thread Eric Snow
On Wed, May 30, 2018 at 10:47 PM, Danilo J. S. Bellini wrote: try: > ... # [...] > ... session.commit() # Here it raises! > ... # [...] > ... except DatabaseError as exc: > ... msg = get_db_error_msg_from_exception(exc) > ... if msg == "beyond_limit": > ... # [...]

Re: [Python-ideas] exception instantiation philosophy and practice [was: Let try-except check the exception instance]

2018-05-31 Thread Antoine Pitrou
On Thu, 31 May 2018 07:49:58 -0700 Ethan Furman wrote: > On 05/31/2018 07:36 AM, Nick Coghlan wrote: > > > The exception machinery deliberately attempts to avoid instantiating > > exception objects whenever it can, but that gets > > significantly more difficult if we always need to create the in

Re: [Python-ideas] exception instantiation philosophy and practice [was: Let try-except check the exception instance]

2018-05-31 Thread Guido van Rossum
On Thu, May 31, 2018 at 7:49 AM, Ethan Furman wrote: > On 05/31/2018 07:36 AM, Nick Coghlan wrote: > > The exception machinery deliberately attempts to avoid instantiating >> exception objects whenever it can, but that gets >> significantly more difficult if we always need to create the instance

[Python-ideas] exception instantiation philosophy and practice [was: Let try-except check the exception instance]

2018-05-31 Thread Ethan Furman
On 05/31/2018 07:36 AM, Nick Coghlan wrote: The exception machinery deliberately attempts to avoid instantiating exception objects whenever it can, but that gets significantly more difficult if we always need to create the instance before we can decide whether or not the raised exception match

Re: [Python-ideas] Let try-except check the exception instance

2018-05-31 Thread Nick Coghlan
On 31 May 2018 at 14:47, Danilo J. S. Bellini wrote: > The idea is to allow catching exceptions beyond checking their MRO, > using a class that checks the exception instance by implementing > a custom __instancecheck__. > The exception machinery deliberately attempts to avoid instantiating excep

Re: [Python-ideas] Let try-except check the exception instance

2018-05-31 Thread Ken Hilton
In one of my own packages, I use the following to catch dynamic error messages: def handle_error(exc): # do stuff with exc with catch('error_code', handle_error): # do stuff that can raise an error The definition of "catch" is as follows, assuming the root exception actual

Re: [Python-ideas] Let try-except check the exception instance

2018-05-31 Thread Danilo J. S. Bellini
On 31 May 2018 at 02:41, Steven D'Aprano wrote: > Since error messages are rarely part of the exception API, testing for > them is fragile and prone to errors. For example, what if the message > changes to "no money" or "out of funds" or "insufficient funds" or > "keine Mittel"? Since the error m

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Chris Angelico
On Thu, May 31, 2018 at 11:23 PM, Peter O'Connor wrote: > On Thu, May 31, 2018 at 2:55 PM, Chris Angelico wrote: >> >> [process(tx, y) for x in xs for tx in [transform(x)] for y in yz] >> >> ... >> >> I think Serhiy was trying to establish this form as a standard idiom, >> with optimization in th

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Peter O'Connor
On Thu, May 31, 2018 at 2:55 PM, Chris Angelico wrote: > [process(tx, y) for x in xs for tx in [transform(x)] for y in yz] > ... I think Serhiy was trying to establish this form as a standard idiom, > with optimization in the interpreter to avoid constructing a list and > iterating over it (so i

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Chris Angelico
On Thu, May 31, 2018 at 10:24 PM, Steven D'Aprano wrote: > Do you have an equally compelling example for your given-comprehension > syntax? I didn't think your example was obviously better than what we > can already do: > > # calculate tx only once per x loop > [process(tx, y) for x in xs

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Steven D'Aprano
On Thu, May 31, 2018 at 02:22:21PM +0200, Peter O'Connor wrote: > There seems to be a lot of controversy about updating variables defined > outside a comprehension within a comprehension. Seems like it could lead > to a lot of bugs and unintended consequences, and that it's safer to not > allow s

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Steven D'Aprano
On Thu, May 31, 2018 at 04:44:18AM -0400, Neil Girdhar wrote: > Yes, you're right. That's the ambiguity I mentioned in my last message. > It's too bad because I want given for expressions and given for > comprehensions. Why? So far you haven't given (heh, pun intended) any examples of something y

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Peter O'Connor
On Thu, May 31, 2018 at 1:55 PM, Neil Girdhar wrote: > Why wouldn't you want to just put the outer given outside the entire > comprehension? > retval = [expr(name, x) given name=update(name, x) for x in seq] > given name=something > There seems to be a lot of controversy about updating varia

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Neil Girdhar
On Thu, May 31, 2018 at 5:39 AM Peter O'Connor wrote: > Well, there need not be any ambiguity if you think of "B given A" as > "execute A before B", and remember that "given" has a lower precedence than > "for" (So [B given A for x in seq] is parsed as [(B given A) for x in seq] > > Then > >> >>

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Peter O'Connor
Well, there need not be any ambiguity if you think of "B given A" as "execute A before B", and remember that "given" has a lower precedence than "for" (So [B given A for x in seq] is parsed as [(B given A) for x in seq] Then > > retval = [expr(name) given name=something(x) for x in seq] > Is

Re: [Python-ideas] Let try-except check the exception instance

2018-05-31 Thread Stephan Houben
Current documentation says: "An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception." https://docs.python.org/3/reference/compound_stmts.html#the-try-statement It is, in my opinion, not very

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Neil Girdhar
Yes, you're right. That's the ambiguity I mentioned in my last message. It's too bad because I want given for expressions and given for comprehensions. But if you have both, there's ambiguity and you would at least need parentheses: [(y given y=2*x) for x in range(3)] That might be fine. On Thu,

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Peter O'Connor
* Sorry, message sent too early: On Thu, May 31, 2018 at 4:50 AM, Neil Girdhar wrote: > > >> [expression given name=something for x in seq] >> > > retval = [] > name = something > for x in seq: > retval.append(expression) > return retval > That's a little confusing then, because, given t

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Peter O'Connor
On Thu, May 31, 2018 at 4:50 AM, Neil Girdhar wrote: > > >> [expression given name=something for x in seq] >> > > retval = [] > name = something > for x in seq: > retval.append(expression) > return retval > That's a little strange confusing then, because, given the way given is used outsi

Re: [Python-ideas] A real life example of "given"

2018-05-31 Thread Neil Girdhar
Okay, I though about it some more, and I think I'm mistaken about the possibility of adding both rules to the grammar since in that case it is ambiguous whether given binds more tightly to a trailing expression or to the comp_iter. It's too bad. On Wed, May 30, 2018 at 10:50 PM Neil Girdhar wrot

Re: [Python-ideas] Let try-except check the exception instance

2018-05-31 Thread Terry Reedy
On 5/31/2018 12:47 AM, Danilo J. S. Bellini wrote: Hi! I was working on handling some exceptions from external software (e.g. database constraint triggers) switching the handler based on the messages that had been sent. Today we can do something like (running on Python 3.6.5): try: ...     #