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

2018-06-01 Thread Antoine Pitrou
On Thu, 31 May 2018 14:00:24 -0400 Alexander Belopolsky wrote: > > Is this really true? Consider the following simple code > > class E(Exception): > def __init__(self): > print("instantiated") > > try: > raise E > except E: > pass > > Is it truly necessary to instantiate

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

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] 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

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

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

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

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: ...     #

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

2018-05-30 Thread Franklin? Lee
Would guards (such as from the switch-case discussions) be a good fit? try: ... except RuntimeError as e if e.message == "tie": ... On Thu, May 31, 2018, 00:48 Danilo J. S. Bellini wrote: > Hi! > I was working on handling some exceptions from external software > (e.g. database

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

2018-05-30 Thread Steven D'Aprano
On Thu, May 31, 2018 at 01:47:17AM -0300, 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": > ...

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

2018-05-30 Thread Danilo J. S. Bellini
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: ... # [...] ... session.commit() # Here it raises!