473,394 Members | 1,843 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,394 software developers and data experts.

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 1521
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Miller | last post by:
Is there any particular good reason to inherit from exceptions.Exception? I've never seen any code that depends on what the base class(es) of a raised exception is (are). My use case is in a...
0
by: Philip Rittenhouse | last post by:
I have one last issue with pywin32 to report. The problem occurs with Python COM servers raising COM exceptions across a custom interface. Using the dispatch interface works fine, but in the...
4
by: Ken Fine | last post by:
No joy on Macromedia's boards after a few days; maybe someone can help me here. I got an excellent string handling function off of planet-source-code.com that converts text strings to proper...
8
by: Shawn Berg | last post by:
I have created a class with the following constructor: Public Sub New(ByVal ID As Integer) 'Use ID to get info from data store and set all properties End Sub I need to somehow handle an...
4
by: Paul Wilson | last post by:
I want to use Err.Raise() method to raise my own exceptions. Is this the right way of raising my own exceptions ? (i think this is the only way). What is the Error number i can safely use,...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
45
by: Gregory Petrosyan | last post by:
1) From 2.4.2 documentation: There are two new valid (semantic) forms for the raise statement: raise Class, instance raise instance 2) In python: >>> raise NameError Traceback (most recent...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
28
by: Christoph Zwerschke | last post by:
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.