aosabook/500lines

Python interpreter frame scope creation bug?

timmwagener opened this issue · 0 comments

As i am following the (rewarding) book i have come across a part in the Python Interpreter chapter for which i am having trouble following the logic. Specifically it is about make_frame() in the Virtualmachine class in this line.

Considering the call site in Function.__call__ vm.make_frame(...) seems to reset the local scope for the frame to the globals. Wouldn't this cause problems when functions modify their local scope? Is this an optimization or a bug? (I'd assume the later one as the logic in the main byterun repo changed here)

# Function
def __call__(self, *args, **kwargs):
        callargs = inspect.getcallargs(self._func, *args, **kwargs)
        frame = self._vm.make_frame(
            self.func_code, callargs, self.func_globals, {}
        )

# VirtualMachine
def make_frame(self, code, callargs={}, global_names=None, local_names=None):
        if global_names is not None and local_names is not None:
            local_names = global_names
...

Pinging the authors @akaptur @nedbat , if you guys find the time, a short clarification would be appreciated.