Hello,
I want to alter the way exceptions print themselves.
More specifically, I'd like to extend the __str__ method of
the exception's class so that it is printed in a different way.
I used to replace the __str__ method on the exception object's
class by a custom method, but that breaks my code on Python 2.5
because of the following error I'm getting:
TypeError: can't set attributes of built-in/extension type 'exceptions.TypeError'
Well, ok. So I tried to improve my code and not alter the class,
but only the exception object. I came up with this:
def __excStr__(self):
return "[[[EXCEPTION! "+self.originalStr()+"]]"
.... some code that raises an exception 'ex' ....
import new
newStr = new.instancemethod( __excStr__, ex, ex.__class__)
ex.__str__=newStr
On Python 2.4 the above code works as I expect, however on Python 2.5
it doesn't seem to have any effect... What am I doing wrong?
Or is there perhaps a different way to do what I want?
Thanks!
--Irmen de Jong 2 1194
Irmen de Jong wrote:
I want to alter the way exceptions print themselves.
More specifically, I'd like to extend the __str__ method of
the exception's class so that it is printed in a different way.
I used to replace the __str__ method on the exception object's
class by a custom method, but that breaks my code on Python 2.5
because of the following error I'm getting:
TypeError: can't set attributes of built-in/extension type
'exceptions.TypeError'
Well, ok. So I tried to improve my code and not alter the class,
but only the exception object. I came up with this:
def __excStr__(self):
return "[[[EXCEPTION! "+self.originalStr()+"]]"
... some code that raises an exception 'ex' ....
import new
newStr = new.instancemethod( __excStr__, ex, ex.__class__)
ex.__str__=newStr
On Python 2.4 the above code works as I expect, however on Python 2.5
it doesn't seem to have any effect... What am I doing wrong?
Nothing. That's just a chain of bad luck. As exceptions have become newstyle
classes __xxx__() special methods aren't looked up in the instance anymore.
Or is there perhaps a different way to do what I want?
Does it suffice to set the excepthook? If so, you might want something less
hackish than
class wrap_exc(object):
def __init__(self, exception):
self.__exception = exception
def __getattr__(self, name):
return getattr(self.exception, name)
def __str__(self):
return "yadda"
def eh(etype, exception, tb):
eh_orig(etype, wrap_exc(exception), tb)
eh_orig = sys.excepthook
Peter
Irmen de Jong wrote:
I want to alter the way exceptions print themselves.
More specifically, I'd like to extend the __str__ method of
the exception's class so that it is printed in a different way.
I used to replace the __str__ method on the exception object's
class by a custom method, but that breaks my code on Python 2.5
because of the following error I'm getting:
TypeError: can't set attributes of built-in/extension type
'exceptions.TypeError'
Well, ok. So I tried to improve my code and not alter the class,
but only the exception object. I came up with this:
def __excStr__(self):
return "[[[EXCEPTION! "+self.originalStr()+"]]"
... some code that raises an exception 'ex' ....
import new
newStr = new.instancemethod( __excStr__, ex, ex.__class__)
ex.__str__=newStr
On Python 2.4 the above code works as I expect, however on Python 2.5
it doesn't seem to have any effect... What am I doing wrong?
Nothing. That's just a chain of bad luck. As exceptions have become newstyle
classes __xxx__() special methods aren't looked up in the instance anymore.
Or is there perhaps a different way to do what I want?
Does it suffice to set the excepthook? If so, you might want something less
hackish than
class wrap_exc(object):
def __init__(self, exception):
self.__exception = exception
def __getattr__(self, name):
return getattr(self.__exception, name)
def __str__(self):
return "yadda"
def eh(etype, exception, tb):
eh_orig(etype, wrap_exc(exception), tb)
eh_orig = sys.excepthook
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vegard Bakke |
last post by:
From whet I can see, Python documentation is lacking a very important
piece of information. Very few functions have documented what
exceptions they raise, and under what cicumstances. Is this a...
|
by: Raymond Hettinger |
last post by:
For your amusement and edification, I'm working on a
series of Python puzzles designed to highlight areas of
the language known only to those who have read the
docs more than once.
Each of the...
|
by: David Turner |
last post by:
Hi all
I noticed something interesting while testing some RAII concepts
ported from C++ in Python. I haven't managed to find any information
about it on the web, hence this post.
The problem...
|
by: Torsten Bronger |
last post by:
Hallöchen!
I write a module, and so I must raise exceptions at several points.
The PEP 8 says that a module should define their own exceptions base
class, derived from "Exception", and...
|
by: mag31 |
last post by:
Is there any way to find out if a particular .net function will throw an
exception without first generating the exception?
I am using structured exception handling i.e. try catch finally blocks...
|
by: a_geek |
last post by:
Hello,
I'd like to catch all exeptions and be able to inspect them.
The simple case: I know which exceptions I'll get:
# standard textbook example:
try:
something()
except ThisException,...
|
by: ej |
last post by:
I'm trying to figure out how to get at some of the internal interpreter
state for exception handlers and debug statements in general.
I just read through Section 8 of the Python Tutorial. I see how...
|
by: Gregory Piñero |
last post by:
How can I catch 2 exceptions at once for example:
try:
self.gses = opener.open(req)
except (urllib2.HTTPError,urllib2.URLError):
do something..
Seems to work, but how do I also get...
|
by: Rafe |
last post by:
Hi,
I've encountered a problem which is making debugging less obvious than
it should be. The @property decorator doesn't always raise exceptions.
It seems like it is bound to the class but...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |