Skip to content

Eval of code objects does not always fill passed locals dictionary #132148

Open
@lejar

Description

@lejar

Bug report

Bug description:

When passing a code object to the eval (doc) builtin, I am observing that the locals dictionary is not always filled with the variables created inside of the code. For example:

import textwrap

def foo_func():
    a = 1
    b = 2

foo_str = compile(
    textwrap.dedent("""\
        a = 1
        b = 2
        """
    ),
    '<file>',
    'exec',
)


glb = globals()
loc = {}

eval(foo_func.__code__, glb, loc)
print('foo_func', loc)

eval(foo_str, glb, loc)
print('foo_str', loc)

produces the output:

foo_func {}
foo_str {'a': 1, 'b': 2}

I would expect the output to be the same for both calls.

CPython versions tested on:

3.13

Operating systems tested on:

Linux, macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions