473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Altering the way exceptions print themselves

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
Oct 22 '06 #1
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
Oct 22 '06 #2
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
Oct 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
12
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...
16
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...
2
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...
24
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...
4
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,...
7
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...
2
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...
14
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.