Exceptions when the exception handler is in a separate module

Consider the following two modules: prog.py and err.py. both of which are examples of a much larger program that exhibits this problem. I need to split the functionality into smaller source files, each with a test bench.

Err.py includes a test bench. When it creates a prog object, depending on how the exception is raised, the exception hits or not.

It seems that although prog.py imports err objects in the "from err import" expression, the module name is still displayed (erroneous?), But it seems to erroneously refer not to the module itself

Is this a bug in Python 2.7 or is this the intended behavior?

just get both files and run err.py to figure out what i mean ..

First file:

#prog.py

from err import *



class prog(object):
    def func1(self):
        raise MySubError

    def func2(self):
        doError()

:

#err.py
import prog
import inspect
import sys

class myError(Exception):
    pass

class MySubError(myError):
    pass

def doError():
    raise MySubError

if __name__=="__main__":

    p=prog.prog()


    try:
        doError()
    except MySubError as er:
        print type(er)
        print "Captured 1"
    except Exception as er:
        print type(er)
        print "Not Captured 1"        

    try:
        p.func1()
    except MySubError as er:
        print type(er)
        print "Captured 2"
    except Exception as er:
        print type(er)
        print "Not captured 2"


    try:
        p.func2()
    except MySubError as er:
        print type(er)
        print "Captured 3"
    except Exception as er:
        print type(er)
        print "Not captured 3"

, - err , , err.MySubError, MySubError. , .....

:

<class '__main__.MySubError'>
Captured 1
<class 'test.MySubError'>
Not captured 2
<class 'test.MySubError'>
Not captured 3
+3
2

, python , err.py script. , script , python , err.py . ( - , , @Latty ).

script:

# File: recur.py
import recur
print "This is the module"

if __name__ == '__main__':
    print "This is main"

, , . " " .

script ( ), . A B, : Try import A, , .

: , script , :

# err.py
...
if __name__=="__main__":
    from prog.err import *     # add this line

script , .

+1

, err .

, Python, __main__. . , script, err, .

, Python. - script, __main__. , .

+2

All Articles