Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,13 +1078,15 @@ async def ast_classdef(self, arg):
sym_table = {}
self.sym_table_stack.append(self.sym_table)
self.sym_table = sym_table
for arg1 in arg.body:
val = await self.aeval(arg1)
if isinstance(val, EvalReturn):
raise SyntaxError(f"{val.name()} statement outside function")
if isinstance(val, EvalStopFlow):
raise SyntaxError(f"{val.name()} statement outside loop")
self.sym_table = self.sym_table_stack.pop()
try:
for arg1 in arg.body:
val = await self.aeval(arg1)
if isinstance(val, EvalReturn):
raise SyntaxError(f"{val.name()} statement outside function")
if isinstance(val, EvalStopFlow):
raise SyntaxError(f"{val.name()} statement outside loop")
finally:
self.sym_table = self.sym_table_stack.pop()

decorators = [await self.aeval(dec) for dec in arg.decorator_list]
sym_table["__init__evalfunc_wrap__"] = None
Expand Down
51 changes: 51 additions & 0 deletions docs/plans/classdef-finally.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Plan: classdef-finally

## What Sean asked for
"start on the class body try/finally fix": the small follow-up found while working on #902.

## What we found
- When a class body raises an error, pyscript never leaves the class's scope (`ast_classdef` in `custom_components/pyscript/eval.py`). It switches into the class's namespace before running the body and switches back only after the body finishes normally.
- Checked on upstream's latest code: with `try: class Foo: raise ValueError ... except ValueError: pass` at the top of a script, a later `x = 5` is stored inside the failed class, not in the script. Real Python stores it in the script.
- Inside a function, the same error leaves pyscript's internal scope stack one entry too long. Upstream issue search found no existing report.
- #902's second commit (not merged yet) changes the same few lines. Whichever PR merges second needs a small, mechanical update.

## What we'll change
1. **`custom_components/pyscript/eval.py`, `ast_classdef`:** wrap the loop that runs the class body in `try`/`finally`, so pyscript always returns to the outer scope, even when the body raises. *Check:* new tests below.
2. **Tests in `tests/test_unit_eval.py`**, each checked against real Python:
- a class body that raises, caught by `try`/`except` at module level, then later assignments land in the script;
- the same inside a function, with the function's later locals and return value correct;
- a nested class whose body raises;
- an error from a class body that isn't caught: it still reaches the caller, and the next script run is unaffected.
*Check:* the new tests fail on upstream's current code and pass with step 1.
3. **All project checks** on Python 3.14, as CI runs them: `pytest`, `ruff check`, `ruff format --check`, `pylint`, `mypy`, and `codespell`.
4. **Upstream PR.** After your final approval, one commit goes on a clean branch cut from upstream `master`, holding only the fix and its tests (no plan or notes). You get a prefilled link to open the PR from your account. The PR text mentions that it touches the same lines as #902.

## Contribution rules that apply
- `docs/contributing.rst` and `tests/README.md`: dev setup from `tests/requirements_test.txt`, pre-commit hooks, `pytest`, PRs from a fork.
- Pre-commit runs ruff (check and format, line length 109), codespell, pylint and mypy. CI (`validate.yml`, Python 3.14) runs hassfest, ruff, pytest with coverage, pylint and mypy.
- No PR template; we follow the maintainer's habits: one small fix per PR, with tests. Commits are signed off.

## Who's on it
Engineer, Security Officer and Auditor. Skipped as too small for them: research (done while planning), QA (the Auditor runs the tests), and the plan check (its reviewer's monthly quota is used up). No second security review: nothing security-related is touched.

## Risks
- **Conflict with #902:** both change `ast_classdef`. If #902 merges first, we update this branch (a few lines); if this merges first, #902 gets the same small update.
- **Behaviour change:** only code where a class body raises and the error is caught behaves differently, and it now behaves like Python.

## Out of scope
- A class redefined with a body that raises losing its old value: #902's second commit already fixes that.
- Any other class-body behaviour.

---

## Record (2026-11-20)

**Status:** built and checked, with no open problems. Not yet sent upstream.

**What shipped on this branch:** one commit, `restore the scope when a class body raises` (`5aa2e0b`). It wraps the class-body loop in `ast_classdef` in `try`/`finally` (+9/−7) and adds two tests (+103) compared against CPython 3.14. It applies cleanly to upstream `master` (`8131a78`), and all 204 tests, ruff, pylint, mypy and codespell pass.

**Checks:** the Security Officer reported it clear, and the Auditor approved with no findings. The audit also confirmed that the scope is restored for `return`/`break` in a class body and for task cancellation. Plan check, QA and plain-English check were skipped (job too small; the reviewer's quota was used up).

**Decisions:**
- Cut from upstream `master`, not from #902's branch, so the fix can merge on its own. Whichever of the two merges second needs a small update.
- The class name left bound to an undefined placeholder after a failed body is left alone: #902's second commit handles redefinition, and a first definition already gives NameError, as Python does.
103 changes: 103 additions & 0 deletions tests/test_unit_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2448,3 +2448,106 @@ async def test_with_exit_does_not_swallow_cancel(hass):
await Function.waiter_sync()
await Function.waiter_stop()
await Function.reaper_stop()


CLASS_BODY_RAISES_SOURCE = """
results = {}

try:
class Foo:
a = 1
raise ValueError("boom")
except ValueError:
pass
x = 5
results["module"] = [x, "x" in globals()]

def func():
try:
class Bar:
b = 2
raise ValueError("boom")
except ValueError:
pass
y = 7
z = [y, "y" in locals()]
return z

results["func"] = func()
results["func_global"] = "y" in globals()

class Outer:
c = 3
try:
class Inner:
d = 4
raise ValueError("boom")
except ValueError:
pass
e = 5

results["nested"] = [Outer.c, Outer.e, hasattr(Outer, "d")]
"""


async def test_class_body_raises_matches_python(hass):
"""Test that a class body that raises leaves the scope like Python, compared with CPython itself."""
hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})}
Function.init(hass)
State.init(hass)
State.register_functions()
TrigTime.init(hass)

expected_ns = {}
exec(compile(CLASS_BODY_RAISES_SOURCE, "test", "exec"), expected_ns) # pylint: disable=exec-used
expected = expected_ns["results"]

global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr)
ast = AstEval("test", global_ctx=global_ctx)
Function.install_ast_funcs(ast)
ast.parse(CLASS_BODY_RAISES_SOURCE + "results\n")
assert await ast.eval() == expected
assert "x" in global_ctx.global_sym_table
assert not ast.sym_table_stack

await Function.waiter_sync()
await Function.waiter_stop()
await Function.reaper_stop()


async def test_class_body_error_reaches_caller(hass):
"""Test that an uncaught class body error reaches the caller and later runs are unaffected."""
hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})}
Function.init(hass)
State.init(hass)
State.register_functions()
TrigTime.init(hass)

for source in [
'class Foo:\n raise ValueError("boom")\n',
"class Foo:\n return 1\n",
"class Foo:\n break\n",
]:
with pytest.raises((ValueError, SyntaxError)):
exec(compile(source, "test", "exec"), {}) # pylint: disable=exec-used

global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr)
ast = AstEval("test", global_ctx=global_ctx)
Function.install_ast_funcs(ast)
for source, exc_type in [
('class Foo:\n raise ValueError("boom")\n', ValueError),
("class Foo:\n for i in range(2):\n pass\n raise KeyError(1)\n", KeyError),
]:
ast.parse(source)
with pytest.raises(exc_type):
await ast.eval()
assert ast.sym_table is ast.global_sym_table
assert not ast.sym_table_stack

ast.parse("x = 5\nx\n")
assert await ast.eval() == 5
assert global_ctx.global_sym_table["x"] == 5

await Function.waiter_sync()
await Function.waiter_stop()
await Function.reaper_stop()
Loading