[issue2378] UnboundLocalError when trying to raise exceptions inside execfile

Jerry Seutter report at bugs.python.org
Tue Mar 18 03:55:45 CET 2008


New submission from Jerry Seutter <jseutter at gmail.com>:

Found a bug when trying to integrate figleaf coverage into trunk.  I
have ripped the code down to the smallest subset that still causes the
behaviour.  The code works on the latest release of Python 2.5 but is
broken on trunk.  It comes in two files.  The first is the caller (figleaf):

import os
import sys

def foo(f, e, o):
    pass
    
sys.settrace(foo)

import __main__
execfile('test_broken.py', __main__.__dict__)



The second file is the test (test_broken.py):

# This code breaks on trunk

def test_foo():
    class CustomException(Exception):
        pass

    class SomeClass:
        def foo(self):
            raise CustomException

    # The error only appears with enough nested blocks.
    if (True == True):
        try:
            raise IOError
        except CustomException:
            pass



It should raise IOError.  When run, it gives the following output:

jerry-seutters-computer:~/code/python/core_wierd_bug_minimal jseutter$
../core/python.exe figleaf                
Traceback (most recent call last):
  File "figleaf", line 10, in <module>
    execfile('test_broken.py', __main__.__dict__)
  File "test_broken.py", line 18, in <module>
    test_foo()
  File "test_broken.py", line 15, in test_foo
    except CustomException:
UnboundLocalError: local variable 'CustomException' referenced before
assignment
[10019 refs]

----------
files: core_wierd_bug_minimal.zip
messages: 63856
nosy: jseutter
severity: normal
status: open
title: UnboundLocalError when trying to raise exceptions inside execfile
type: behavior
versions: Python 2.6
Added file: http://bugs.python.org/file9716/core_wierd_bug_minimal.zip

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2378>
__________________________________


More information about the Python-bugs-list mailing list