Skip to content

exec('var = 1') executed on thread fails to define var #156416

Description

@bperun

Bug report

Bug description:

import time
import threading

class TestClass(threading.Thread):
    
    def __init__(self):
        threading.Thread.__init__(self)
        self.abort = threading.Event()
    
    def run(self):
        exec('var = 1')
        print(var)
        while True:
            if self.abort.is_set():
                self.abort.clear()
                break
            time.sleep(0.5)

obj = TestClass()
obj.start()
obj.abort.set()

bug.py

Above script also attached for convenience. Produces output:

NameError: name 'var' is not defined. Did you mean: 'vars'?

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions