469,573 Members | 1,684 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

Adjusting the names of custom exceptions (since raising strings is deprecated)

Heyas

So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaises in a unit
test and raising exceptions with a string exception. It's working
pretty well, except that I get the deprecation warning that raising a
string exception is going to go away. So my question is, how do I
mangle the name of my exception class enough that it doesnt stick the
name of the module before the name of the exception?

Namely I'd like to get the following

***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MyError: 'oops!'

instead of

***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.MyError: 'oops!'

(or even test_thingie.MyError as is usually the case).
Creating a class in a separate file and then doing

***
from module import MyError
raise MyError
still gives

***
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
module.MyError
Anyway, any help appreciated.

Aug 21 '07 #1
4 1391
Silfheed wrote:
Heyas

So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaises in a unit
test and raising exceptions with a string exception. It's working
pretty well, except that I get the deprecation warning that raising a
string exception is going to go away. So my question is, how do I
mangle the name of my exception class enough that it doesnt stick the
name of the module before the name of the exception?

Namely I'd like to get the following

***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MyError: 'oops!'

instead of

***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.MyError: 'oops!'

(or even test_thingie.MyError as is usually the case).
Creating a class in a separate file and then doing

***
from module import MyError
raise MyError
still gives

***
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
module.MyError
Anyway, any help appreciated.
Would it be cheating to use metaclasses?

# myModule.py
class ExampleType(type):
def __repr__(cls):
return cls.__name__

class ExampleError(Exception):
__metaclass__ = ExampleType
__name__ = 'ExampleError'
def __repr__(self):
return 'ExampleError'
pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.ExampleError myModule.ExampleType
pyraise myModule.ExampleError
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ExampleError
James
Aug 21 '07 #2
On Aug 21, 1:32 am, James Stroud <jstr...@mbi.ucla.eduwrote:
Silfheed wrote:
Heyas
So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaises in a unit
test and raising exceptions with a string exception. It's working
pretty well, except that I get the deprecation warning that raising a
string exception is going to go away. So my question is, how do I
mangle the name of my exception class enough that it doesnt stick the
name of the module before the name of the exception?
Namely I'd like to get the following
***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MyError: 'oops!'
instead of
***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.MyError: 'oops!'
(or even test_thingie.MyError as is usually the case).
Creating a class in a separate file and then doing
***
from module import MyError
raise MyError
still gives
***
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
module.MyError
Anyway, any help appreciated.

Would it be cheating to use metaclasses?

# myModule.py
class ExampleType(type):
def __repr__(cls):
return cls.__name__

class ExampleError(Exception):
__metaclass__ = ExampleType
__name__ = 'ExampleError'
def __repr__(self):
return 'ExampleError'

pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.ExampleError myModule.ExampleType
pyraise myModule.ExampleError
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ExampleError

James
It doesnt appear to work for me.
Same exact code as you have but I still get:
>>raise myModule.ExampleError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
myModule.ExampleError

Aug 21 '07 #3
Silfheed wrote:
On Aug 21, 1:32 am, James Stroud <jstr...@mbi.ucla.eduwrote:
>Silfheed wrote:
Heyas
So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaises in a unit
test and raising exceptions with a string exception. It's working
pretty well, except that I get the deprecation warning that raising a
string exception is going to go away. So my question is, how do I
mangle the name of my exception class enough that it doesnt stick the
name of the module before the name of the exception?
Namely I'd like to get the following
***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MyError: 'oops!'
instead of
***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.MyError: 'oops!'
(or even test_thingie.MyError as is usually the case).
Creating a class in a separate file and then doing
***
from module import MyError
raise MyError
still gives
***
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
module.MyError
Anyway, any help appreciated.

Would it be cheating to use metaclasses?

# myModule.py
class ExampleType(type):
def __repr__(cls):
return cls.__name__

class ExampleError(Exception):
__metaclass__ = ExampleType
__name__ = 'ExampleError'
def __repr__(self):
return 'ExampleError'

pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.ExampleError myModule.ExampleType
pyraise myModule.ExampleError
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ExampleError

James

It doesnt appear to work for me.
Same exact code as you have but I still get:
>>>raise myModule.ExampleError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
myModule.ExampleError
James tested his code in the ipython console which obviously uses a
different routine to produce the traceback.

Try
>>class MyError(Exception):
.... __module__ = None
....
>>raise MyError("oops")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MyError: oops

Peter
Aug 21 '07 #4
On Aug 21, 1:53 pm, Peter Otten <__pete...@web.dewrote:
Silfheed wrote:
On Aug 21, 1:32 am, James Stroud <jstr...@mbi.ucla.eduwrote:
Silfheed wrote:
Heyas
So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaises in a unit
test and raising exceptions with a string exception. It's working
pretty well, except that I get the deprecation warning that raising a
string exception is going to go away. So my question is, how do I
mangle the name of my exception class enough that it doesnt stick the
name of the module before the name of the exception?
Namely I'd like to get the following
***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MyError: 'oops!'
instead of
***
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.MyError: 'oops!'
(or even test_thingie.MyError as is usually the case).
Creating a class in a separate file and then doing
***
from module import MyError
raise MyError
still gives
***
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
module.MyError
Anyway, any help appreciated.
Would it be cheating to use metaclasses?
# myModule.py
class ExampleType(type):
def __repr__(cls):
return cls.__name__
class ExampleError(Exception):
__metaclass__ = ExampleType
__name__ = 'ExampleError'
def __repr__(self):
return 'ExampleError'
pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.ExampleError myModule.ExampleType
pyraise myModule.ExampleError
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ExampleError
James
It doesnt appear to work for me.
Same exact code as you have but I still get:
>>raise myModule.ExampleError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
myModule.ExampleError

James tested his code in the ipython console which obviously uses a
different routine to produce the traceback.

Try
>class MyError(Exception):

... __module__ = None
...>>raise MyError("oops")

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MyError: oops

Peter
Ah ha! Thanks, that worked great!

Aug 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Philip Rittenhouse | last post: by
8 posts views Thread by Shawn Berg | last post: by
4 posts views Thread by Paul Wilson | last post: by
14 posts views Thread by dcassar | last post: by
45 posts views Thread by Gregory Petrosyan | last post: by
28 posts views Thread by Christoph Zwerschke | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.