Skip to content

eval() cannot see local variables in a method (or those passed in as function parameters) #10769

@ericzundel

Description

@ericzundel

CircuitPython version and board name

Adafruit CircuitPython 10.0.1 on 2025-10-09; Adafruit Feather nRF52840 Express with nRF52840

Code/REPL

def eval_test(foo):
    global loops
    bar=1
    eval("print('Hello')")
    eval('print(f"loops is {loops}")')

    # passing a dict for the locals parameter works:
    eval('print(f"foo is {foo} bar is {bar}")', 
         {}, {"foo":foo, "bar": bar}) 

    # Fails with: NameError: name 'bar' isn't defined
    #eval('print(f"bar is {bar}")') 
    
    # Fails with: NameError: name 'foo' isn't defined
    #eval('print(f"foo is {foo}")') 

loops=1
eval_test(9)

Behavior

When referencing a name in the local scope, you get: NameError: name 'foo' isn't defined

But you can create and pass a local scope and it works.

Description

No response

Additional information

I looked around and didn't see that this was a known issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions