Re: Re-raising exceptions with modified message

2007-07-16 Thread Gabriel Genellina
En Mon, 16 Jul 2007 13:50:50 -0300, fumanchu <[EMAIL PROTECTED]> escribió: > On Jul 15, 2:55 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: >> Here is a simple solution, but it depends >> on the existence of the args attribute that >> "will eventually be deprecated" according >> to the docs >

Re: Re-raising exceptions with modified message

2007-07-16 Thread fumanchu
On Jul 15, 2:55 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Here is a simple solution, but it depends > on the existence of the args attribute that > "will eventually be deprecated" according > to the docs If you don't mind using .args, then the solution is usually as simple as: try:

Re: Re-raising exceptions with modified message

2007-07-15 Thread Christoph Zwerschke
Christoph Zwerschke wrote: > Here is a simple solution, but it depends on the existence of the args > attribute that "will eventually be deprecated" according to the docs: Just found another amazingly simple solution that does neither use teh .args (docs: "will eventually be deprecated") attribu

Re: Re-raising exceptions with modified message

2007-07-15 Thread Christoph Zwerschke
Christoph Zwerschke wrote: > Here is a simple solution, but it depends on the existence of the args > attribute that "will eventually be deprecated" according to the docs: Ok, here is another solution that does not depend on args: def PoliteException(e): E = e.__class__ class PoliteExc

Re: Re-raising exceptions with modified message

2007-07-15 Thread Christoph Zwerschke
Christoph Zwerschke wrote: > But my __getattr__ solution does not work either, since the attributes > are set to None when initialized, so __getattr__ is never called. Here is a simple solution, but it depends on the existence of the args attribute that "will eventually be deprecated" according

Re: Re-raising exceptions with modified message

2007-07-15 Thread Christoph Zwerschke
samwyse wrote: > NewStyle.__name__ = old.__class__.__name__ Simple, but that does the trick! > new.__dict__ = old.__dict__.copy() Unfortunately, that does not work, since the attributes are not writeable and thus do not appear in __dict__. But my __getattr__ solution does not work either, si

Re: Re-raising exceptions with modified message

2007-07-13 Thread samwyse
On Jul 13, 12:45 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > samwyse wrote: > > TypeError: __class__ must be set to a class > > > Excpt ceratinly appears to be a class. Does anyone smarter than me > > know what's going on here? > > Not that I want to appear smarter, but I think the proble

Re: Re-raising exceptions with modified message

2007-07-12 Thread Christoph Zwerschke
samwyse wrote: > TypeError: __class__ must be set to a class > > Excpt ceratinly appears to be a class. Does anyone smarter than me > know what's going on here? Not that I want to appear smarter, but I think the problem here is that exceptions are new-style classes now, whereas Empty is an old-

Re: Re-raising exceptions with modified message

2007-07-12 Thread samwyse
On Jul 12, 6:31 am, samwyse <[EMAIL PROTECTED]> wrote: > On Jul 8, 8:50 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > > > With Py 2.5 I get: > > > new.__class__ = old.__class__ > > TypeError: __class__ must be set to a class Hmmm, under Python 2.4.X, printing repr(old.__class__) gives

Re: Re-raising exceptions with modified message

2007-07-12 Thread samwyse
On Jul 8, 8:50 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Did you run this? > With Py < 2.5 I get a syntax error, and with Py 2.5 I get: > > new.__class__ = old.__class__ > TypeError: __class__ must be set to a class > > -- Chris Damn, I'd have sworn I ran the final copy that I post

Re: Re-raising exceptions with modified message

2007-07-08 Thread Christoph Zwerschke
samwyse wrote: > def test(code): > try: > code() > except Exception, e: > try: > raise e.__class__, str(e) + ", sorry!" > except TypeError: > raise SorryFactory(e)() Ok, you're suggestig the naive approach if it works and the factory approach I came up with last as a f

Re: Re-raising exceptions with modified message

2007-07-08 Thread Christoph Zwerschke
Did you run this? With Py < 2.5 I get a syntax error, and with Py 2.5 I get: new.__class__ = old.__class__ TypeError: __class__ must be set to a class -- Chris -- http://mail.python.org/mailman/listinfo/python-list

Re: Re-raising exceptions with modified message

2007-07-07 Thread samwyse
On Jul 5, 8:53 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > What is the best way to re-raise any exception with a message > supplemented with additional information (e.g. line number in a > template)? Let's say for simplicity I just want to add "sorry" to every > exception message. OK, thi

Re: Re-raising exceptions with modified message

2007-07-07 Thread samwyse
On Jul 7, 4:13 pm, samwyse <[EMAIL PROTECTED]> wrote: > On Jul 5, 8:53 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > > > What is the best way to re-raise any exception with a message > > supplemented with additional information (e.g. line number in a > > template)? [...] > That leaves the is

Re: Re-raising exceptions with modified message

2007-07-07 Thread samwyse
On Jul 5, 8:53 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > What is the best way to re-raise any exception with a message > supplemented with additional information (e.g. line number in a > template)? Let's say for simplicity I just want to add "sorry" to every > exception message. My naive

Re: Re-raising exceptions with modified message

2007-07-07 Thread Christoph Zwerschke
Gerard Flanagan wrote: > Would a decorator work here? Depends on how you want to use that functionality. In my use case I only need to catch the excpetion once. Note that in your code the exception has not the right type which is what I targeted in my last posting. I.e. the following will raise

Re: Re-raising exceptions with modified message

2007-07-06 Thread Gerard Flanagan
On Jul 6, 12:18 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Sorry for the soliloquy, but what I am really using is the following so > that the re-raised excpetion has the same type: > > def PoliteException(e): > class PoliteException(e.__class__): > def __init__(self, e): >

Re: Re-raising exceptions with modified message

2007-07-06 Thread Neil Cerutti
On 2007-07-06, Alex Popescu <[EMAIL PROTECTED]> wrote: > On Jul 6, 4:20 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: >> Alex Popescu wrote: >> > Probably the simplest solution would be to create a new exception and >> > wrapping the old one and the additional info. Unfortunately, this >> > ma

Re: Re-raising exceptions with modified message

2007-07-06 Thread Alex Popescu
On Jul 6, 4:20 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Alex Popescu wrote: > > Probably the simplest solution would be to create a new exception and > > wrapping the old one and the additional info. Unfortunately, this > > may have a huge impact on 3rd party code that was catching the

Re: Re-raising exceptions with modified message

2007-07-06 Thread Alex Popescu
On Jul 6, 4:20 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Alex Popescu wrote: > -- http://mail.python.org/mailman/listinfo/python-list

Re: Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
Alex Popescu wrote: > Probably the simplest solution would be to create a new exception and > wrapping the old one and the additional info. Unfortunately, this > may have a huge impact on 3rd party code that was catching the > original exception. So, I think you should create an utility > factor

Re: Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
Sorry for the soliloquy, but what I am really using is the following so that the re-raised excpetion has the same type: def PoliteException(e): class PoliteException(e.__class__): def __init__(self, e): self._e = e def __getattr__(self, name): retu

Re: Re-raising exceptions with modified message

2007-07-05 Thread Alex Popescu
On Jul 6, 12:21 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Kay Schluehr wrote: > > If you are sure that the exception isn't caught on another level just > > use the following showtraceback() function, manipulate it's output > > slightly and terminate your program with sys.exit() > > That'

Re: Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
Seems that no simple solution exists, so for now, I will be using something like this: class PoliteException(Exception): def __init__(self, e): self._e = e def __getattr__(self, name): return getattr(self._e, name) def __str__(self): if isinstance(self._e,

Re: Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
Kay Schluehr wrote: > If you are sure that the exception isn't caught on another level just > use the following showtraceback() function, manipulate it's output > slightly and terminate your program with sys.exit() That's what I want to avoid. In my case the error is displayed and evaluated in a

Re: Re-raising exceptions with modified message

2007-07-05 Thread Kay Schluehr
On Jul 5, 3:53 pm, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > What is the best way to re-raise any exception with a message > supplemented with additional information (e.g. line number in a > template)? Let's say for simplicity I just want to add "sorry" to every > exception message. My naive

Re: Re-raising exceptions with modified message

2007-07-05 Thread Neil Cerutti
On 2007-07-05, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Neil Cerutti wrote: >> You may need the traceback module to get at the error message, if >> trying to read e.message can fail. >> >> Something like this mess here: ;) >> >>... >>except Exception, e: >> etype, evalue, etb

Re: Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
Neil Cerutti wrote: > You may need the traceback module to get at the error message, if > trying to read e.message can fail. > > Something like this mess here: ;) > >... >except Exception, e: > etype, evalue, etb = sys.exc_info() > ex = traceback.format_exception_only(etype, eva

Re: Re-raising exceptions with modified message

2007-07-05 Thread Neil Cerutti
On 2007-07-05, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Neil Cerutti wrote: >> The documentation for BaseException contains something that might >> be relevant: >> >>[...] If more data needs to be attached to the exception, >>attach it through arbitrary attributes on the instance.

Re: Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
Neil Cerutti wrote: > The documentation for BaseException contains something that might > be relevant: > >[...] If more data needs to be attached to the exception, >attach it through arbitrary attributes on the instance. All > > Users could get at the extra info you attached, but it wouldn

Re: Re-raising exceptions with modified message

2007-07-05 Thread Neil Cerutti
On 2007-07-05, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > Thomas Heller wrote: >> I have the impression that you do NOT want to change the >> exceptions, instead you want to print the traceback in a >> customized way. But I may be wrong... > > No, I really want to modify the exception, suppl

Re: Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
Thomas Heller wrote: > I have the impression that you do NOT want to change the exceptions, > instead you want to print the traceback in a customized way. But I may be > wrong... No, I really want to modify the exception, supplementing its message with additional information about the state of

Re: Re-raising exceptions with modified message

2007-07-05 Thread Thomas Heller
Christoph Zwerschke schrieb: > What is the best way to re-raise any exception with a message > supplemented with additional information (e.g. line number in a > template)? I have the impression that you do NOT want to change the exceptions, instead you want to print the traceback in a customized

Re-raising exceptions with modified message

2007-07-05 Thread Christoph Zwerschke
What is the best way to re-raise any exception with a message supplemented with additional information (e.g. line number in a template)? Let's say for simplicity I just want to add "sorry" to every exception message. My naive solution was this: try: ... except Exception, e: raise e.__