Re: Checking for an exception

2017-06-25 Thread Steve D'Aprano
On Mon, 26 Jun 2017 02:40 am, Skip Montanaro wrote: >> py> isinstance(KeyboardInterrupt(), Exception) >> False >> py> isinstance(ValueError, Exception) >> False >> > > I might have missed something, but don't you want to be using BaseException > as your class/type? Yes I do, which is why I was

Re: Checking for an exception

2017-06-25 Thread D'Arcy Cain
On 06/25/17 12:10, Steve D'Aprano wrote: py> isinstance(KeyboardInterrupt(), Exception) False py> isinstance(ValueError, Exception) False That's because KeyboardInterrupt is not a subclass of Exception. If you want to catch that as well you need to check against BaseException.

Re: Checking for an exception

2017-06-25 Thread Skip Montanaro
> > py> isinstance(KeyboardInterrupt(), Exception) > False > py> isinstance(ValueError, Exception) > False > I might have missed something, but don't you want to be using BaseException as your class/type? Also, Checking isinstance() between two classes isn't likely to work, I don't think. Both

Re: Checking for an exception

2017-06-25 Thread Steve D'Aprano
On Sun, 25 Jun 2017 05:50 pm, Paul Rubin wrote: > Steve D'Aprano writes: >> What's the right/best way to test whether an object is an exception >> ahead of time? (That is, without trying to raise from it.) > > Maybe I'm missing something but >isinstance(obj,

Re: Checking for an exception

2017-06-25 Thread Paul Rubin
Steve D'Aprano writes: > What's the right/best way to test whether an object is an exception > ahead of time? (That is, without trying to raise from it.) Maybe I'm missing something but isinstance(obj, Exception) seems to work. --

Re: Checking for an exception

2017-06-25 Thread Cameron Simpson
On 25Jun2017 13:47, Ben Finney wrote: Steve D'Aprano writes: […] the result of passing a non-exception to raise is to raise an exception, so I cannot trivially distinguish between "caller passes an exception" and "caller passes a

Re: Checking for an exception

2017-06-24 Thread Ben Finney
Steve D'Aprano writes: > […] the result of passing a non-exception to raise is to raise an > exception, so I cannot trivially distinguish between "caller passes an > exception" and "caller passes a non-exception" (result is still an > exception). Yes, hence my

Re: Checking for an exception

2017-06-24 Thread Steve D'Aprano
On Sun, 25 Jun 2017 10:49 am, Ben Finney wrote: > Steve D'Aprano writes: > >> What's the right/best way to test whether an object is an exception >> ahead of time? (That is, without trying to raise from it.) > > This being Python, it is Easier to Ask for Forgiveness

Re: Checking for an exception

2017-06-24 Thread Steve D'Aprano
On Sun, 25 Jun 2017 09:37 am, Cameron Simpson wrote: > On 24Jun2017 20:31, Steve D'Aprano wrote: >>What's the right/best way to test whether an object is an exception ahead of >>time? (That is, without trying to raise from it.) >> >>I have: >> >>return

Re: Checking for an exception

2017-06-24 Thread Ben Finney
Steve D'Aprano writes: > What's the right/best way to test whether an object is an exception > ahead of time? (That is, without trying to raise from it.) This being Python, it is Easier to Ask for Forgiveness than for Permission. The corollary of that is, if you try

Re: Checking for an exception

2017-06-24 Thread Cameron Simpson
On 24Jun2017 20:31, Steve D'Aprano wrote: What's the right/best way to test whether an object is an exception ahead of time? (That is, without trying to raise from it.) I have: return (isinstance(obj, type) and issubclass(obj, BaseException) or

Re: Checking for an exception

2017-06-24 Thread Steve D'Aprano
On Sat, 24 Jun 2017 09:23 pm, mbyrne...@gmail.com wrote: > On Saturday, June 24, 2017 at 11:31:11 AM UTC+1, Steve D'Aprano wrote: >> What's the right/best way to test whether an object is an exception ahead of >> time? (That is, without trying to raise from it.) >> >> I have: >> >> return

Re: Checking for an exception

2017-06-24 Thread mbyrnepr2
On Saturday, June 24, 2017 at 11:31:11 AM UTC+1, Steve D'Aprano wrote: > What's the right/best way to test whether an object is an exception ahead of > time? (That is, without trying to raise from it.) > > I have: > > return (isinstance(obj, type) and issubclass(obj, BaseException) > or

Checking for an exception

2017-06-24 Thread Steve D'Aprano
What's the right/best way to test whether an object is an exception ahead of time? (That is, without trying to raise from it.) I have: return (isinstance(obj, type) and issubclass(obj, BaseException) or isinstance(obj, BaseException)) Any better ideas? -- Steve “Cheer up,” they