This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Suspected bug in python optimizer with decorators
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Brian Smith, r.david.murray
Priority: normal Keywords:

Created on 2016-11-03 21:49 by Brian Smith, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit Brian Smith, 2016-11-03 21:49 Short python program demonstrating bug
Messages (2)
msg280025 - (view) Author: Brian Smith (Brian Smith) Date: 2016-11-03 21:49
While using decorators in python 3.5.2, I ran into a surprising bug where the decorator sometimes lost context of the outer scope.  The attached file demonstrates this problem.

In this file, we have 2 decorators.  They are identical, except that the first has one line (at line 11) commented out.

When I run the program, I get the following output:

dev:~/dev/route105/workspace/ir/play$ python
Trying dec1:
in dec1: {'tags': ['foo:1'], 'name': 'foo'}
inside dec1.decorator: {'tags': {'tags': ['foo:1', ['name:subsystem']], 'name': 'foo'}, 'func': <function f1 at 0x7ff03d60cb70>}

Trying dec2:
in dec2: {'tags': ['foo:1'], 'name': 'foo'}
inside dec2.decorator: {'func': <function f2 at 0x7ff03d60cc80>}
Traceback (most recent call last):
  File "", line 42, in <module>
    @dec2(name="foo", tags=["foo:1"])
  File "", line 27, in decorator
    name = tags["name"]
UnboundLocalError: local variable 'tags' referenced before assignment

There are two issues here:
1) In dec1, the keyword argument 'name' exists in the outer scope, but not in the inner scope.  For some reason, the keyword argument 'tags' exists in both scopes.

2) In dec2, Adding the line 
causes the keyword argument 'tags' to disappear from the inner scope. 

The only thing I could think of was a compiler or optimizer bug.
msg280035 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-11-04 00:08
The statement in question causes the compiler to make 'tags' a local variable in the function, and thus you get the error on the assignment.  See
Date User Action Args
2022-04-11 14:58:39adminsetgithub: 72792
2016-11-04 00:08:13r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg280035

resolution: not a bug
stage: resolved
2016-11-03 21:49:41Brian Smithcreate