Note that there are some explanatory texts on larger screens.

plurals
  1. POExceptions where the exception handler is in a separate module
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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 </p> <p>Is this a bug in Python 2.7 or is it intended behaviour?</p> <p>simply get both files and run err.py to see what I mean..</p> <p>The first file:</p> <pre><code>#prog.py from err import * class prog(object): def func1(self): raise MySubError def func2(self): doError() </code></pre> <p>And the second file:</p> <pre><code>#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" </code></pre> <p>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.....</p> <p>Output:</p> <pre><code>&lt;class '__main__.MySubError'&gt; Captured 1 &lt;class 'test.MySubError'&gt; Not captured 2 &lt;class 'test.MySubError'&gt; Not captured 3 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload