Skip to content

Incorrect output due to Tier 2 _LOAD_CONST_INLINE #117051

Open
@gvanrossum

Description

@gvanrossum

Bug report

I've constructed a sneaky way to break the remove_globals() optimization, by executing a code object in two different globals dicts. (Credit goes to @brandtbucher who started the conversation about this topic, though this particular example is mine.)

Take this code:

src = """
def f():
    global x
    def inner(i):
        a = 1
        for _ in range(100):
            a = x + i
        return a
    return inner

func = f()
"""

co = compile(src, __file__, "exec")

ns1 = {"x": 1000}
eval(co, ns1)
func1 = ns1["func"]
for i in range(1000):
    x = func1(i)
print(x)

ns2 = {"x": 2000}
eval(co, ns2)
func2 = ns2["func"]
for i in range(1000):
    x = func2(i)
print(x)

This should print

1999
2999

(try it with Python 3.12, or Python 3.13 without JIT or -Xuops).

However with current main (EDIT: when using JIT or -Xuops) it prints

1999
1999

:-(

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions