[New-bugs-announce] [issue45707] Variable reassginment triggers incorrect behaviors of locals()

Xinmeng Xia report at bugs.python.org
Wed Nov 3 23:24:22 EDT 2021


New submission from Xinmeng Xia <xiaxm at smail.nju.edu.cn>:

Normally after executing a piece of code in a function, locals() should contain the local variables and these variables can be reassigned next. In the following code, "attr" should be found in locals(). Actually, it can not be found in either locals() or globals() after executing code "attr = 1". This program triggers a keyError. I think something wrong during handling locals().

================================
def foo():
    exec("attr = 1")
    a = locals()['attr']
    attr = 2 
foo()
================================

Reported Error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in foo
KeyError: 'attr'


Expected output:
This test program should work well. The value of a is 1 and the value of attr is 2 after execution. No error is reported.

Python version: python3.10, Ubuntu 16.04

----------
components: Interpreter Core
messages: 405660
nosy: xxm
priority: normal
severity: normal
status: open
title: Variable reassginment triggers incorrect behaviors of locals()
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45707>
_______________________________________


More information about the New-bugs-announce mailing list