Python local variable mismatch problem

I have the following code snippet:

def isolation_level(level):
    def decorator(fn):
        def recur(level, *args, **kwargs):
            if connection.inside_block:
                if connection.isolation_level < level:
                    raise IsolationLevelError(connection)
                else:
                    fn(*args, **kwargs)
            else:
                connection.enter_block()
                try:
                    connection.set_isolation_level(level)
                    fn(*args, **kwargs)
                    connection.commit()
                except IsolationLevelError, e:
                    connection.rollback()
                    recur(e.level, *args, **kwargs)
                finally:
                    connection.leave_block()
        def newfn(*args, **kwargs):
            if level is None: # <<<< ERROR MESSAGE HERE, Unbound local variable `level`
                if len(args):
                    if hasattr(args[0], 'isolation_level'):
                        level = args[0].isolation_level
                elif kwargs.has_key('self'):
                    if hasattr(kwargs['self'], 'isolation_level'):
                        level = kwargs.pop('self', 1) 
            if connection.is_dirty():
                connection.commit()
            recur(level, *args, **kwargs)
        return newfn
    return decorator

It does not matter what he does, however I post it in the original form, as I was not able to recreate the situation with anything simpler.

The problem is that when I call isolation_level(1)(some_func)(some, args, here), I get an exception Unbound local variableon line 21 (marked in the listing). I do not understand why. I tried to recreate the same structure of functions and function calls that do not contain all implementation details in order to find out what is wrong. However, I do not receive an exception message. For example, the following works:

def outer(x=None):
    def outer2(y):
        def inner(x, *args, **kwargs):
            print x
            print y
            print args
            print kwargs
        def inner2(*args, **kwargs):
            if x is None:
                print "I'm confused"
            inner(x, *args, **kwargs)
        return inner2
    return outer2

outer(1)(2)(3, z=4)

Print

1
2
(3,)
{'z': 4}

What am I missing?

EDIT

So, the problem is that in the first version I actually perform the assignment of a variable. Python detects this and therefore assumes the variable is local.

+5
1

: level , . ,

if level is None:

level , . Python 3.x ,

nonlocal level

, . .

+9

All Articles