473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 FailUnlessRaise s 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__.MyErro r: 'oops!'

(or even test_thingie.My Error 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 1534
Silfheed wrote:
Heyas

So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaise s 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__.MyErro r: 'oops!'

(or even test_thingie.My Error 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(typ e):
def __repr__(cls):
return cls.__name__

class ExampleError(Ex ception):
__metaclass__ = ExampleType
__name__ = 'ExampleError'
def __repr__(self):
return 'ExampleError'
pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.Exampl eError myModule.Exampl eType
pyraise myModule.Exampl eError
------------------------------------------------------------
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.uc la.eduwrote:
Silfheed wrote:
Heyas
So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaise s 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__.MyErro r: 'oops!'
(or even test_thingie.My Error 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(typ e):
def __repr__(cls):
return cls.__name__

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

pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.Exampl eError myModule.Exampl eType
pyraise myModule.Exampl eError
------------------------------------------------------------
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.Exampl eError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
myModule.Exampl eError

Aug 21 '07 #3
Silfheed wrote:
On Aug 21, 1:32 am, James Stroud <jstr...@mbi.uc la.eduwrote:
>Silfheed wrote:
Heyas
So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaise s 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__.MyErro r: 'oops!'
(or even test_thingie.My Error 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(typ e):
def __repr__(cls):
return cls.__name__

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

pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.Examp leError myModule.Exampl eType
pyraise myModule.Exampl eError
------------------------------------------------------------
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.Exampl eError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
myModule.Exampl eError
James tested his code in the ipython console which obviously uses a
different routine to produce the traceback.

Try
>>class MyError(Excepti on):
.... __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.uc la.eduwrote:
Silfheed wrote:
Heyas
So this probably highlights my lack of understanding of how naming
works in python, but I'm currently using FailUnlessRaise s 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__.MyErro r: 'oops!'
(or even test_thingie.My Error 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(typ e):
def __repr__(cls):
return cls.__name__
class ExampleError(Ex ception):
__metaclass__ = ExampleType
__name__ = 'ExampleError'
def __repr__(self):
return 'ExampleError'
pyimport myModule
pyraise myMo
myModule myModule.py myModule.pyc myModule.py~
pyraise myModule.Ex
myModule.Exampl eError myModule.Exampl eType
pyraise myModule.Exampl eError
------------------------------------------------------------
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.Exampl eError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
myModule.Exampl eError

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

Try
>class MyError(Excepti on):

... __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
7283
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 game I am writing. The code for my Game class contains the following: class Game (object): def start (self):
0
1274
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 custom interface the exception's error information is lost. The problem appears to be in PyGatewayBase::InterfaceSupportsErrorInfo() in...
4
1887
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 case, i.e. "the REMAINS of the DAY" is converted to "The Remains Of The Day". This function is particularly good in that it handles certain...
8
3236
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 invalid ID being passed to the constructor. By invalid, I mean one that does not have an associated record in the data store. I believe that throwing an...
4
1819
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, ensuring that it isn't used by ..NET. Ie, can i use -100 as a error number?? Ie, -100, -101,-102... etc. I want to konw if .NET uses negative values as...
14
3455
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 public member with a return type that is derived from System.Exception? I understand the importance of having clean, concise code that follows...
45
3414
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 call last): File "<stdin>", line 1, in ? NameError
3
3393
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 exception constructors? I've noticed that, f.ex., ArgumentException accepts null arguments without raising ArgumentNullException. Obviously, if nothing is...
28
5857
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 exception message. My naive solution was this: try: ... except Exception, e: raise e.__class__, str(e) + ", sorry!"
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3663
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3655
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
954
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.