Skip to content

JIT (correctness): Different UnboundLocalError for same code with JIT on or off #140104

Description

@devdanzin

Bug report

Bug description:

The following code results in two different UnboundLocalError exceptions depending on whether it's run with the JIT on or off:

def f():

    if 0:
        class Class:
            pass

    if 0:
        instance = Class()

    for _ in range(5000):
        try:
            _ = instance.f()
        except Exception:
            pass
    Class.__bases__ = (object,)

f()

With JIT on, the UnboundLocalError points to 'instance', while with JIT off it points to 'Class'.

Traceback with JIT on:

Traceback (most recent call last):
  File "/home/danzin/divergences/divergence_child_6010_3_1.py", line 17, in <module>
    f()
    ~^^
  File "/home/danzin/divergences/divergence_child_6010_3_1.py", line 12, in f
    _ = instance.f()
        ^^^^^^^^
UnboundLocalError: cannot access local variable 'instance' where it is not associated with a value

Traceback with JIT off:

Traceback (most recent call last):
  File "/home/danzin/divergences/divergence_child_6010_3_1.py", line 17, in <module>
    f()
    ~^^
  File "/home/danzin/divergences/divergence_child_6010_3_1.py", line 15, in f
    Class.__bases__ = (object,)
    ^^^^^
UnboundLocalError: cannot access local variable 'Class' where it is not associated with a value

Output for PYTHON_LLTRACE=4:
divergence_01_lltrace.txt

Output for PYTHON_OPT_DEBUG=4:
divergence_01_opt_debug.txt

Please let me know whether you're able to reproduce this issue.

Found using lafleur.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. Fidget-Spinner commented on Oct 14, 2025

    @Fidget-Spinner
    Member

    It's possible #139109 might end up fixing this inadvertently, so let's not fix this for now maybe? Or we could fix it with the knowledge the code will be ripped out soon.

  2. devdanzin commented on Oct 15, 2025

    @devdanzin
    MemberAuthor

    Here's a simpler reproducer for what seems to be the same issue:

    def f():
        for i in range(500):
            try:
                g(i)
            except Exception:
                pass
    
    for _ in range(5000):
        f()

    This one runs to completion without the JIT and raises an exception with it.

  3. added a commit that references this issue on Oct 27, 2025
  4. efimov-mikhail commented on Oct 27, 2025

    @efimov-mikhail
    Member

    We need a backport for 3.14, don't we?
    Or we ignore backports since JIT is in expiremental stage?

  5. Fidget-Spinner commented on Oct 27, 2025

    @Fidget-Spinner
    Member

    Let's backport to 3.14 but not 3.13.

  6. Fidget-Spinner commented on Oct 27, 2025

    @Fidget-Spinner
    Member

    Backport failed, maybe I should open up the backport to anyone who wants to try please?

  7. efimov-mikhail commented on Oct 27, 2025

    @efimov-mikhail
    Member

    Backport failed, maybe I should open up the backport to anyone who wants to try please?

    I'd like to provide this backport.

  8. added a commit that references this issue on Oct 27, 2025
  9. added a commit that references this issue on Oct 27, 2025
  10. Fidget-Spinner commented on Nov 1, 2025

    @Fidget-Spinner
    Member

    This is still broken on 3.14 sadly.

  11. added a commit that references this issue on Nov 1, 2025
  12. efimov-mikhail commented on Nov 1, 2025

    @efimov-mikhail
    Member

    This is still broken on 3.14 sadly.

    I'm sorry. It seems that I've tested incorrectly.
    It's actually still broken on 3.14.

  13. Fidget-Spinner commented on Nov 1, 2025

    @Fidget-Spinner
    Member

    This is still broken on 3.14 sadly.

    I'm sorry. It seems that I've tested incorrectly. It's actually still broken on 3.14.

    No worries. I didn't realize CI didn't trigger for JIT on the backport either!

  14. added a commit that references this issue on Nov 1, 2025
  15. added a commit that references this issue on Nov 13, 2025
  16. added a commit that references this issue on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions