Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Exceptions and modules

Reply
Thread Tools

Exceptions and modules

 
 
sam
Guest
Posts: n/a
 
      07-04-2003
Hello,

consider the following code:

<module1>
import sys
import threading

class MyException(Exception):
pass

#from module1 import MyException

class runner(threading.Thread):
def __init__(self, callableObj):
self.callableObject=callableObj
threading.Thread.__init__(self)
def run(self):
try:
self.callableObject.makeCall()
except MyException, err:
print "MyException"
print sys.exc_type, err
except Exception, err:
print "Exception"
print sys.exc_type, err

if __name__ == '__main__':
if len(sys.argv) != 3:
print "Usage <module> <class>"
else:
callableObj = getattr(__import__(sys.argv[1]), sys.argv[2])()
thread=runner(callableObj)
thread.start()
</module1>
<module2>
from module1 import MyException

class Callable:
def makeCall(self):
raise MyException("MyException")
</module2>

Now I start the process:

python module1.py module2 Callable

And the result is:

Exception
module1.MyException MyException

So the Exception isn't recognised even though it has the same
namespace and name as the exception defined in the module. I have to
uncomment the 7th line to get my example to behave as I would like it
to.

Is this a bug/feature? Is there any reason why it shouldn't work the
way I expect it to?

Regards,

Sam Owen
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      07-04-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (sam) wrote in
news:(E-Mail Removed) om:

> So the Exception isn't recognised even though it has the same
> namespace and name as the exception defined in the module. I have to
> uncomment the 7th line to get my example to behave as I would like it
> to.
>
> Is this a bug/feature? Is there any reason why it shouldn't work the
> way I expect it to?


It's a feature, and it probably ought to be in the FAQ in some form.

When you run 'python module1.py' the code in the source file module1.py is
compiled into a module called __main__. That means that the line:

except MyException, err:

is actually short for:

except __main__.MyException, err:

When module2 imports module1 the code is recompiled into a new module. Same
source code, but otherwise no relation. You then raise module1.MyException
which doesn't match __main__.MyException.

--
Duncan Booth (E-Mail Removed)
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments