Skip to content

Unwrap EvalLocalVar in nested @pyscript_compile/@pyscript_executor - #947

Open
EnesKartal wants to merge 1 commit into
custom-components:masterfrom
EnesKartal:fix/pyscript-compile-default-local
Open

EnesKartal wants to merge 1 commit into
custom-components:masterfrom
EnesKartal:fix/pyscript-compile-default-local

Conversation

@EnesKartal

Copy link
Copy Markdown

Fixes #945.

Nested @pyscript_compile\ and @pyscript_executor\ functions were compiled via \exec()\ with \self.sym_table\ directly as \locals, causing default arguments (and other decorators) referencing enclosing locals to receive \EvalLocalVar\ cells instead of their values. This wraps \self.sym_table\ in a read-through _LocalSymTable\ view when in a local scope so \exec()\ unwraps \EvalLocalVar\ entries (and raises \NameError\ for undefined locals) without overwriting the enclosing function's cell.

Verified with \pytest\ (307 passed),
uff check,
uff format --check, \mypy, and \pylint.

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @EnesKartal, nice fix.

One small request: when the local isn't assigned yet, could _LocalSymTable.__getitem__ raise UnboundLocalError(f"local variable '{key}' referenced before assignment") instead of the NameError? That's what CPython raises, and it matches pyscript's message for reading an unassigned local elsewhere. The new exception test would then expect UnboundLocalError with that message. Happy to approve after that.

@craigbarratt

Copy link
Copy Markdown
Member

Small update to my request: #950 just switched pyscript to CPython 3.14's wording, so after rebasing on master please raise UnboundLocalError(unbound_local_msg(key)) (from eval.py), which gives cannot access local variable 'x' where it is not associated with a value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default argument naming a local gets EvalLocalVar in a nested @pyscript_compile/@pyscript_executor function

2 participants