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

S

Silfheed

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.
 
J

James Stroud

Silfheed said:
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'


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


James
 
S

Silfheed

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'

py> import myModule
py> raise myMo
myModule myModule.py myModule.pyc myModule.py~
py> raise myModule.Ex
myModule.ExampleError myModule.ExampleType
py> raise 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:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
myModule.ExampleError
 
P

Peter Otten

Silfheed said:
It doesnt appear to work for me.
Same exact code as you have but I still get:

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
.... __module__ = None
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MyError: oops

Peter
 
S

Silfheed

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

Try


... __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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top