Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions where 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 separate out 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 called, the exception is caught or not.

It seems that even though prog.py imports err objects in a "from err import *" statement, the module name is still inferred (wrongly?), but seems the err it refers to is not the same one as the module itself

Is this a bug in Python 2.7 or is it intended behaviour?

simply get both files and run err.py to see what I mean..

The first file:

#prog.py

from err import *



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

    def func2(self):
        doError()

And the second file:

#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"

It seems to me as though somehow err should know what module it is and the exception should be err.MySubError, rather than just MySubError. I can get the module, but not the instance.....

Output:

<class '__main__.MySubError'>
Captured 1
<class 'test.MySubError'>
Not captured 2
<class 'test.MySubError'>
Not captured 3
like image 658
Jason M Avatar asked Mar 06 '26 19:03

Jason M


1 Answers

The issue here is that err actually acts as two different modules, essentially.

The main module that is run in Python is called __main__. Everything executed is put into it's namespace. What is happening here is you are then importing the module into your other script and it's called err, so they are not considered the same.

This is a little bit weird, but it's necessary to keep the way the Python import system works consistent. The best answer here is to move your exceptions outside of your script being called as __main__. This is good design anyway, as you are essentially creating a circular import here.

like image 99
Gareth Latty Avatar answered Mar 09 '26 08:03

Gareth Latty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!