diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index 75d4b69..fa40541 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -13,7 +13,7 @@ import sys import time import traceback -from types import TracebackType +from types import ModuleType, TracebackType from typing import TYPE_CHECKING, Any import weakref @@ -279,6 +279,11 @@ def __repr__(self): return f"EvalLocalVar @{hex(id(self))} = {self.value if self.defined else 'undefined'}" +def unwrap_defined(value): + """Return the value held by a defined EvalLocalVar, otherwise value unchanged.""" + return value.get() if isinstance(value, EvalLocalVar) and value.is_defined() else value + + class EvalName: """Identifier that hasn't yet been resolved.""" @@ -301,11 +306,15 @@ def __init__(self, obj, attr): def setattr(self, value): """Set the attribute value.""" - setattr(self.obj, self.attr, value) + # update a pyscript module's EvalLocalVar in place, so its own functions see the change + if isinstance(self.obj, ModuleType) and isinstance(vars(self.obj).get(self.attr), EvalLocalVar): + vars(self.obj)[self.attr].set(unwrap_defined(value)) + else: + setattr(self.obj, self.attr, value) def getattr(self): """Get the attribute value.""" - return getattr(self.obj, self.attr) + return unwrap_defined(getattr(self.obj, self.attr)) class EvalFunc: @@ -926,6 +935,8 @@ def __init__(self, name: str, global_ctx: "GlobalContext", logger_name: str | No self.set_logger_name(logger_name if logger_name is not None else self.name) self.config_entry = Function.hass.data.get(DOMAIN, {}).get(CONFIG_ENTRY, {}) self.dec_eval_depth = 0 + self.class_sym_table: SymTable | None = None + self.class_global_names: set[str] = set() async def ast_not_implemented(self, arg, *args): """Raise NotImplementedError exception for unimplemented AST types.""" @@ -966,7 +977,7 @@ async def ast_import(self, arg): mod = await Function.hass.async_add_executor_job(importlib.import_module, imp.name) else: mod = sys.modules[imp.name] - self.sym_table[imp.name if imp.asname is None else imp.asname] = mod + self.assign_name(imp.name if imp.asname is None else imp.asname, mod) async def ast_importfrom(self, arg): """Execute from X import Y.""" @@ -976,7 +987,7 @@ async def ast_importfrom(self, arg): mod = await self.global_ctx.module_import(imp.name, arg.level) if not mod: raise ModuleNotFoundError(f"module '{imp.name}' not found") - self.sym_table[imp.name if imp.asname is None else imp.asname] = mod + self.assign_name(imp.name if imp.asname is None else imp.asname, mod) return if arg.module == "stubs" or arg.module.startswith("stubs."): for imp in arg.names: @@ -1000,10 +1011,17 @@ async def ast_importfrom(self, arg): for imp in arg.names: if imp.name == "*": for name, value in mod.__dict__.items(): - if name[0] != "_": - self.sym_table[name] = value + # skip a module-level EvalLocalVar whose name was deleted + if name[0] != "_" and not (isinstance(value, EvalLocalVar) and not value.is_defined()): + self.assign_name(name, value) else: - self.sym_table[imp.name if imp.asname is None else imp.asname] = getattr(mod, imp.name) + value = getattr(mod, imp.name) + if isinstance(value, EvalLocalVar) and not value.is_defined(): + raise ImportError( + f"cannot import name '{imp.name}' from '{mod.__name__}' (unknown location)" + ) + # assign_name takes the value out of the module's EvalLocalVar + self.assign_name(imp.name if imp.asname is None else imp.asname, value) async def ast_if(self, arg): """Execute if statement.""" @@ -1067,23 +1085,32 @@ async def ast_classdef(self, arg): keywords = {kw.arg: await self.aeval(kw.value) for kw in arg.keywords} metaclass = keywords.pop("metaclass", type(bases[0]) if bases else type) - if self.curr_func and arg.name in self.curr_func.global_names: + if self.binds_global(arg.name): sym_table_assign = self.global_sym_table else: sym_table_assign = self.sym_table - sym_table_assign[arg.name] = EvalLocalVar(arg.name) + # + # reuse an existing EvalLocalVar, so functions defined earlier see the new class; + # it keeps the old class until the new one is set below, as in Python + # + if not (arg.name in sym_table_assign and isinstance(sym_table_assign[arg.name], EvalLocalVar)): + sym_table_assign[arg.name] = EvalLocalVar(arg.name) if hasattr(metaclass, "__prepare__"): sym_table = metaclass.__prepare__(arg.name, tuple(bases), **keywords) else: sym_table = {} self.sym_table_stack.append(self.sym_table) self.sym_table = sym_table + prev_class_sym_table, self.class_sym_table = self.class_sym_table, sym_table + prev_class_global_names, self.class_global_names = self.class_global_names, set() 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.class_sym_table = prev_class_sym_table + self.class_global_names = prev_class_global_names self.sym_table = self.sym_table_stack.pop() decorators = [await self.aeval(dec) for dec in arg.decorator_list] @@ -1191,7 +1218,7 @@ async def executor_wrap(*args, **kwargs): else: func_var = func - if self.curr_func and name in self.curr_func.global_names: + if self.binds_global(name): sym_table = self.global_sym_table else: sym_table = self.sym_table @@ -1346,6 +1373,10 @@ async def ast_return(self, arg): async def ast_global(self, arg): """Execute global statement.""" + if self.sym_table is self.class_sym_table: + # a class body's global declarations apply to the class body only + self.class_global_names.update(arg.names) + return if not self.curr_func: raise SyntaxError("global statement outside function") for var_name in arg.names: @@ -1412,13 +1443,37 @@ async def recurse_assign(self, lhs, val): raise NameError( f"invalid name '{var_name}' (should be 'domain.entity' or 'domain.entity.attr')" ) - if self.curr_func and var_name in self.curr_func.global_names: - self.global_sym_table[var_name] = val - return - if var_name in self.sym_table and isinstance(self.sym_table[var_name], EvalLocalVar): - self.sym_table[var_name].set(val) + self.assign_name(var_name, val) + + def binds_global(self, var_name): + """Return whether var_name is bound in the global symbol table by a global declaration.""" + if self.sym_table is self.class_sym_table: + # a class body binds in its own namespace, whatever an enclosing function declares + return var_name in self.class_global_names + return bool(self.curr_func and var_name in self.curr_func.global_names) + + def assign_name(self, var_name, val): + """Bind a plain variable name to val, updating an existing EvalLocalVar in place.""" + # + # store the value rather than another binding's EvalLocalVar (eg, from mod.Foo), + # so a later update of this name doesn't change that other binding + # + val = unwrap_defined(val) + if self.binds_global(var_name): + # update a module-level EvalLocalVar in place, so functions defined earlier see it + if isinstance(self.global_sym_table.get(var_name), EvalLocalVar): + self.global_sym_table[var_name].set(val) else: - self.sym_table[var_name] = val + self.global_sym_table[var_name] = val + return + if self.sym_table is self.class_sym_table: + # a class body binds plainly in the class namespace (see binds_global) + self.sym_table[var_name] = val + return + if var_name in self.sym_table and isinstance(self.sym_table[var_name], EvalLocalVar): + self.sym_table[var_name].set(val) + else: + self.sym_table[var_name] = val async def ast_assign(self, arg): """Execute assignment statement.""" @@ -1456,8 +1511,14 @@ async def ast_delete(self, arg): var = await self.aeval(arg1.value) del var[await self.aeval(arg1.slice)] elif isinstance(arg1, ast.Name): - if self.curr_func and arg1.id in self.curr_func.global_names: - if arg1.id in self.global_sym_table: + if self.binds_global(arg1.id): + # as with assignment, mark a module-level EvalLocalVar undefined in place + if isinstance(self.global_sym_table.get(arg1.id), EvalLocalVar): + if self.global_sym_table[arg1.id].is_defined(): + self.global_sym_table[arg1.id].set_undefined() + else: + raise NameError(f"name '{arg1.id}' is not defined") + elif arg1.id in self.global_sym_table: del self.global_sym_table[arg1.id] elif arg1.id in self.sym_table: if isinstance(self.sym_table[arg1.id], EvalLocalVar): @@ -1515,6 +1576,12 @@ async def ast_attribute(self, arg): val = await self.aeval(arg.value) if isinstance(arg.ctx, ast.Store): return EvalAttrSet(val, arg.attr) + if isinstance(val, ModuleType): + # a pyscript module keeps module-level classes in EvalLocalVars, so unwrap them + value = getattr(val, arg.attr) + if isinstance(value, EvalLocalVar) and not value.is_defined(): + raise AttributeError(f"module '{val.__name__}' has no attribute '{arg.attr}'") + return unwrap_defined(value) return getattr(val, arg.attr) async def ast_name(self, arg): @@ -1523,15 +1590,28 @@ async def ast_name(self, arg): # # check other scopes if required by global declarations # - if self.curr_func and arg.id in self.curr_func.global_names: + in_class = self.sym_table is self.class_sym_table + if (in_class and arg.id in self.class_global_names) or ( + self.curr_func and arg.id in self.curr_func.global_names + ): + if in_class and arg.id not in self.class_global_names and arg.id in self.sym_table: + # a class body finds its own names first, unless it declares them global + val = self.sym_table[arg.id] + return val.get() if isinstance(val, EvalLocalVar) else val if arg.id in self.global_sym_table: - return self.global_sym_table[arg.id] + return unwrap_defined(self.global_sym_table[arg.id]) raise NameError(f"global name '{arg.id}' is not defined") # # now check in our current symbol table, and then some other places # if arg.id in self.sym_table: if isinstance(self.sym_table[arg.id], EvalLocalVar): + if ( + self.curr_func + and arg.id in self.curr_func.local_names + and not self.sym_table[arg.id].is_defined() + ): + raise UnboundLocalError(f"local variable '{arg.id}' referenced before assignment") return self.sym_table[arg.id].get() return self.sym_table[arg.id] if arg.id in self.local_sym_table: @@ -1544,8 +1624,7 @@ async def ast_name(self, arg): # it here too (eg, when it's used inside another class's body); while the # class body is still running it isn't defined yet, so leave it wrapped # - val = self.global_sym_table[arg.id] - return val.get() if isinstance(val, EvalLocalVar) and val.is_defined() else val + return unwrap_defined(self.global_sym_table[arg.id]) if arg.id in BUILTIN_AST_FUNCS_FACTORY: return BUILTIN_AST_FUNCS_FACTORY[arg.id](self) if hasattr(builtins, arg.id) and arg.id not in BUILTIN_EXCLUDE and arg.id[0] != "_": @@ -2025,6 +2104,13 @@ async def get_names_set(self, arg, names, nonlocal_names, global_names, local_na target_vars, _ = await self.loopvar_scope_save(arg.generators) for name in target_vars: local_names.add(name) + elif cls_name in {"Import", "ImportFrom"}: + # imported names are local too, so nested functions can share their binding + for alias in arg.names: + name = alias.name if alias.asname is None else alias.asname + if name != "*" and "." not in name: + local_names.add(name) + names.add(name) elif cls_name == "Try": for handler in arg.handlers: if handler.name is not None: diff --git a/docs/plans/global-path-875.md b/docs/plans/global-path-875.md new file mode 100644 index 0000000..46d171f --- /dev/null +++ b/docs/plans/global-path-875.md @@ -0,0 +1,74 @@ +# Plan: global-path-875 + +## What Sean asked for +Act on the maintainer's review of upstream PR custom-components/pyscript#902 (the #875 fix): they approved it, and asked whether we want to also fix the `global` path in the same PR or leave it for a follow-up. + +## What we found +- The maintainer's three suggested changes fix every `global` case in their comment, but they bring back a problem we removed from #875 earlier: after `from mod import Foo`, a function doing `global Foo; Foo = Other` (or `del Foo`) changes `mod`'s own `Foo`, for every script that imports `mod`. This happens because `from mod import Foo` copies the module's internal wrapper object, so both scripts share one box. +- The same sharing already causes a bug on master today, even without `global`: after `from mod import Foo`, a plain top-level `Foo = Other` or `del Foo` in the importing script changes `mod`'s `Foo`. +- The maintainer's changes also make one case they mention worse: after `global Foo; del Foo`, defining `Foo` again raises `NameError` in functions defined earlier. +- Two small extra changes fix both: `from mod import ...` takes the value out of the wrapper (as real Python does), and a class definition reuses the existing wrapper instead of making a new one. With them, all 202 existing tests pass and about 40 test cases behave like real Python. That covers the maintainer's cases, cross-module imports, closures, and `del` followed by a redefinition. + +## Decision: add it to #902 +Sean chose to add the `global` fix to #902 itself, as the maintainer offered, rather than send a separate PR. The new commits go on top of the existing #902 commit, so the maintainer reviews only what's new. + +## What we'll change +1. **Reply on #902 (Sean posts it, after the new commits are pushed).** A short comment for Sean to post from his account. It thanks the maintainer, says the `global` path is now included, and explains why it has two changes beyond their sketch: the cross-module catch, and redefinition after `del`. It also points out the change to import behaviour. We'll also give Sean an updated description for #902 (the current one ends with an empty "Known limits" heading). *Check:* text reviewed for tone and accuracy before Sean posts it. +2. **`custom_components/pyscript/eval.py`, five small edits:** + - assigning through `global` updates the existing wrapper instead of replacing it; + - deleting through `global` marks the wrapper undefined instead of removing it; + - reading through `global` returns the class, not the wrapper; + - `from mod import X` and `from mod import *` copy the value, not the wrapper; + - a class definition reuses an existing wrapper for its name, and keeps the old class visible until the new one is finished (as Python does). + *Check:* full test suite. +3. **Tests in `tests/test_unit_eval.py`** (or a nearby test file if module imports need it). They cover each case in the maintainer's comment (`|`, `is`, setting an attribute, rebinding, `del`, `del` then redefine), closures, and the cross-module cases (`from mod import Foo` / `import *`, then a rebind or `del` in the importer; a second script still sees the original). Each test is checked against what real Python gives. *Check:* the new tests fail on the current branch and pass with step 2. +4. **All project checks** on Python 3.14, as CI runs them: `pytest`, `ruff check`, `ruff format --check`, `pylint` and `mypy` on `custom_components/pyscript/*.py tests/*.py`, plus codespell from pre-commit. +5. **Update #902.** After the final approval here, push only the code and test commits (no plan or notes) onto the #902 branch (`fix-class-annotation-union-875` on the fork). This updates upstream #902 directly. The existing #902 commit is kept as it is, with no history rewrite or force-push. The commit message credits the maintainer's suggestion, names the import behaviour change, and lists the known limits. + +## Contribution rules that apply +- `docs/contributing.rst` points to `tests/README.md`: a dev setup from `tests/requirements_test.txt`, pre-commit hooks, and `pytest`. PRs come from a fork. +- Pre-commit runs ruff (check and format, line length 109), codespell, check-json, yamllint, pylint and mypy. CI (`validate.yml`, Python 3.14) runs hassfest, ruff, pytest with coverage, pylint and mypy. +- No PR template exists. We follow the maintainer's observed habits: small focused PRs, a clear description, and tests for every fix. +- Commits signed off (`git commit -s`), authored as Sean, written as solo work. + +## Who's on it +Engineer (code and tests), QA (tests and plan check), Security Officer (leaks and risky code), Auditor (independent review of the logic), and a plain-English check of the report. No second security review: the change doesn't touch credentials, shell commands, the network, setup scripts, CI or dependencies. + +## Risks +- **Import behaviour change.** Today `from mod import Foo` keeps following later changes to `Foo` inside `mod`, because both share the wrapper. After this change it takes a snapshot, as Python does. An app that relied on the old behaviour would stop seeing those updates. Code using `mod.Foo` is not affected. This will be stated plainly in the upstream PR. +- **Redefining a class.** The wrapper is reused, so while the new class body runs, the name still means the old class, and if the body fails the old class stays. Real Python does the same. A first-time definition works as it does today. Tests will cover both. +- **The maintainer may prefer their smaller version.** The reply will show the failing cross-module case, so they can judge. +- **#902's approval goes stale.** New commits mean the maintainer reviews again before merging. #902 is wider than it was, which they invited. + +## Out of scope (stated as known limits in the upstream PR) +- `import mod` then `mod.Foo | None` or `mod.Foo = Other` (attribute access on a module still hands out the wrapper). +- A lambda or `@pyscript_compile` function reading a module-level class that is later rebound. +- `global Zed; del Zed` on a name that was never defined doesn't raise `NameError`. +- No change to the existing #902 commit. + +--- + +## Record (2026-11-20) + +**Status:** built and checked, with no open problems. Not yet pushed to #902. + +**What shipped on this branch:** one commit, `update module-level names in place when assigned or deleted through global (#875)`. It changes `eval.py` (+109/−23) and adds tests (+825). It applies cleanly on its own onto #902's commit, and all 204 tests, ruff, pylint, mypy and codespell pass on Python 3.14. + +**Decisions:** +- Add the `global` fix to #902 instead of a separate PR (Sean, on this PR). +- A read through `global` leaves an undefined wrapper wrapped, as in the maintainer's sketch, so a class defined through `global` can still name itself in its type hints. Rejected: raising NameError there, which broke code that works today. +- Unwrap on every binding (assignment, imports) and on module attribute reads, so no binding shares another module's wrapper. Rejected: an "owner" field on the wrapper (bigger change, and it left existing leaks). +- Class bodies bind in the class namespace, even inside a function that declares the name `global`. +- Imports inside a function are local to it, as in Python. + +**Review rounds:** +- Round 1: the audit found two must-fix regressions: aliases through `mod.Foo` still shared the wrapper, and imports inside a function broke closures. Both fixed. +- Round 2: the audit found class-body import and alias problems and a wrong exception type. All fixed. +- Round 3 (extra round, Sean commented "fix"): the audit found that `global x` written inside a class body was ignored, and that a nested class failed with a custom `__prepare__` mapping. Both are fixed. The final audit found no new problems; it asked for one more known limit in the commit message (a `@pyscript_compile` function defined in a class body under `global`, which upstream already has), and that was added. + +**Rejected or deferred findings:** +- `global Zed; del Zed` on a never-defined name doesn't raise. Unchanged from upstream, listed as a known limit. +- `hasattr(mod, "Foo")` after mod deletes `Foo` returns True. Added to the `getattr(mod, ...)` known limit. +- Star import inside a function is accepted. Unchanged from upstream. +- A class body that raises doesn't restore pyscript's scope stack. Unchanged from upstream; worth its own small PR. +- Plan check and plain-English check were skipped (the reviewer's monthly quota was used up). diff --git a/tests/test_unit_eval.py b/tests/test_unit_eval.py index ab2f11b..267504d 100644 --- a/tests/test_unit_eval.py +++ b/tests/test_unit_eval.py @@ -4,6 +4,7 @@ import asyncio import contextlib from dataclasses import dataclass +import sys from types import FunctionType, ModuleType import pytest @@ -2505,3 +2506,827 @@ async def test_with_exit_does_not_swallow_cancel(hass): await Function.waiter_sync() await Function.waiter_stop() await Function.reaper_stop() + + +GLOBAL_MOD_NAME = "mod_global" + +GLOBAL_MOD_SOURCE = """ +class Foo: + x = 1 + +def modread(): + return Foo + +def drop(): + global Foo + del Foo +""" + +GLOBAL_BASE_SOURCE = """ +class Foo: + x = 1 + +class Other: + x = 9 + +def read(): + return Foo + +def try_call(func): + try: + func() + return "no error" + except NameError: + return "NameError" +""" + +# +# each entry is a list of scripts and the value of "result" in the last one, which +# is what CPython gives; the scripts share one fresh copy of the GLOBAL_MOD_SOURCE module +# +globalNameTests = [ + [ + [ + GLOBAL_BASE_SOURCE + + """ +def f(): + global Foo + return [Foo | None, Foo is type(Foo())] +union, same = f() +result = [union == (Foo | None), same] +""" + ], + [True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def f(): + global Foo + Foo.x = 3 + return [Foo.x, read().x] +result = [f(), Foo.x] +""" + ], + [[3, 3], 3], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def rebind(): + global Foo + Foo = Other +rebind() +result = [read() is Other, Foo is Other] +""" + ], + [True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def drop(): + global Foo + del Foo +def read_global_attr(): + global Foo + return Foo.x +drop() +result = [try_call(read), try_call(read_global_attr), try_call(drop)] +""" + ], + ["NameError", "NameError", "NameError"], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def drop(): + global Foo + del Foo +drop() +class Foo: + x = 5 +result = [read().x, read() is Foo] +""" + ], + [5, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def drop(): + global Foo + del Foo +def make(): + global Foo + class Foo: + x = 7 +drop() +make() +result = [read().x, read() is Foo, Foo.x] +""" + ], + [7, True, 7], + ], + [ + [ + """ +def make(): + global Node + class Node: + nxt: Node = None +make() +result = [Node.nxt, list(Node.__annotations__)] +""" + ], + [None, ["nxt"]], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +class Foo: + x = Foo.x + 4 +result = [read().x, read() is Foo] +""" + ], + [5, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +del Foo +class Foo: + x = 5 +result = [read().x, read() is Foo] +""" + ], + [5, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def outer(): + def inner(): + global Foo + Foo = Other + inner() + return Foo +def outer_union(): + def inner(): + global Foo + return Foo | None + return inner() +result = [outer_union() == (Foo | None), outer() is Other, read() is Other] +""" + ], + [True, True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def outer(): + def inner(): + return Foo + return inner +inner = outer() +def rebind(): + global Foo + Foo = Other +rebind() +result = [inner() is Other, read() is Other] +""" + ], + [True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo is Other, modread().x, modread() is Other] +""" + ], + [True, 1, False], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +same = [Foo is modread(), read() is modread()] +def drop(): + global Foo + del Foo +drop() +result = [same, try_call(read), modread().x] +""" + ], + [[True, True], "NameError", 1], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +Foo = Other +result = [Foo is Other, modread().x, modread() is Other] +""" + ], + [True, 1, False], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +del Foo +result = [try_call(read), modread().x] +""" + ], + ["NameError", 1], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import * +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo is Other, modread().x, modread() is Other] +""" + ], + [True, 1, False], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import * +del Foo +result = [try_call(read), modread().x] +""" + ], + ["NameError", 1], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo +def rebind(): + global Foo + Foo = Other +rebind() +""", + GLOBAL_BASE_SOURCE + + """ +from mod_global import * +def drop(): + global Foo + del Foo +drop() +""", + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo +Foo = Other +""", + """ +from mod_global import Foo, modread +result = [Foo.x, modread().x, Foo is modread()] +""", + ], + [1, 1, True], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo.x, Foo is Other, mod_global.modread().x] +""" + ], + [9, True, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +same = Foo is mod_global.modread() +def read_foo(): + return Foo.x +def drop_alias(): + global Foo + del Foo +drop_alias() +try: + read_foo() + gone = False +except NameError: + gone = True +result = [same, gone, mod_global.modread().x] +""" + ], + [True, True, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +class Foo: + x = 9 +result = [Foo.x, mod_global.modread().x] +""" + ], + [9, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +""", + """ +import mod_global +Foo = mod_global.Foo +same = Foo is mod_global.modread() +def read_foo(): + return Foo.x +def drop_alias(): + global Foo + del Foo +drop_alias() +try: + read_foo() + gone = False +except NameError: + gone = True +""", + """ +import mod_global +Foo = mod_global.Foo +class Foo: + x = 9 +""", + """ +from mod_global import Foo, modread +result = [Foo.x, modread().x, Foo is modread()] +""", + ], + [1, 1, True], + ], + [ + [ + """ +class Original: + x = 1 +Foo = globals()["Original"] +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +result = [Original.x, Foo.x, Foo is Other] +""" + ], + [1, 9, True], + ], + [ + [ + """ +def outer(): + from mod_global import Foo + def inner(): + return Foo.x + return inner() +def outer_as(): + from mod_global import Foo as Local + def inner(): + return Local.x + return inner +def outer_import(): + import mod_global as mod + def inner(): + return mod.modread().x + return inner() +result = [outer(), outer_as()(), outer_import()] +""" + ], + [1, 1, 1], + ], + [ + [ + """ +def outer(): + from mod_global import Foo, modread + def read(): + return Foo + def change(): + nonlocal Foo + Foo = 20 + change() + return [Foo, read(), modread().x] +result = outer() +""" + ], + [20, 20, 1], + ], + [ + [ + """ +from mod_global import modread +def load(): + global Foo, mod_global + from mod_global import Foo + import mod_global +load() +result = [Foo.x, Foo is modread(), mod_global.modread() is Foo] +""" + ], + [1, True, True], + ], + [ + [ + """ +import mod_global +mod_global.drop() +try: + from mod_global import Foo + msg = None +except ImportError as exc: + msg = str(exc) +from mod_global import * +def read_foo(): + return Foo.x +try: + read_foo() + gone = False +except NameError: + gone = True +result = [msg, gone] +""" + ], + ["cannot import name 'Foo' from 'mod_global' (unknown location)", True], + ], + [ + [ + """ +class Foo: + x = 1 +def make(): + global Foo + class C: + from math import pi as Foo + class D: + import math as Foo + class E: + Foo = 3 + return [C, D, E] +C, D, E = make() +result = [isinstance(C.Foo, float), D.Foo.__name__, E.Foo, Foo.x] +""" + ], + [True, "math", 3, 1], + ], + [ + [ + """ +class Foo: + x = 1 +def make(): + global Foo + class C: + Foo = 3 + y = Foo + del Foo + class D: + def Foo(self): + return 2 + class E: + class Foo: + x = 4 + return [C, D, E] +C, D, E = make() +result = [C.y, hasattr(C, "Foo"), D().Foo(), E.Foo.x, Foo.x] +""" + ], + [3, False, 2, 4, 1], + ], + [ + [ + """ +class Foo: + x = 1 +class C: + class Foo: + x = 2 + from math import pi as Foo + def read(self): + return Foo.x +class D: + class Foo: + x = 2 + Foo = 5 + def read(self): + return Foo.x +result = [C().read(), isinstance(C.Foo, float), D().read(), D.Foo] +""" + ], + [1, True, 1, 5], + ], + [ + [ + """ +import mod_global +globals()["Foo"] = mod_global.Foo +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo.x, mod_global.modread().x] +""" + ], + [9, 1], + ], + [ + [ + """ +import mod_global +globals().update({"Foo": mod_global.Foo}) +class Foo: + x = 9 +result = [Foo.x, mod_global.modread().x] +""" + ], + [9, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.modread() +result = [ + (mod_global.Foo | None) == (Foo | None), + mod_global.Foo is Foo, + isinstance(Foo(), mod_global.Foo), + type(mod_global.Foo()) is mod_global.Foo, +] +""" + ], + [True, True, True, True], + ], + [ + [ + """ +import mod_global +from mod_global import Foo as Snap +class Other: + x = 9 +mod_global.Foo = Other +result = [mod_global.Foo is Other, mod_global.modread() is Other, Snap.x] +""" + ], + [True, True, 1], + ], + [ + [ + """ +import math +def f(): + def inner(): + return math.sqrt(4) + try: + before = math.sqrt(4) + except UnboundLocalError: + before = "unbound" + import math + return [before, inner()] +def g(): + def inner(): + return x + x = 1 + del x + try: + x + return "no error" + except UnboundLocalError: + return "unbound" +result = [f(), g()] +""" + ], + [["unbound", 2.0], "unbound"], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +x = 1 +def make(): + class C: + global x, Foo + x = 2 + from mod_global import Foo + return C +C = make() +import mod_global +result = [x, hasattr(C, "x"), hasattr(C, "Foo"), read() is mod_global.Foo] +""" + ], + [2, False, False, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +x = 1 +class C: + global x, Foo + x = 2 + from math import pi as Foo + y = x + 1 +result = [x, hasattr(C, "x"), C.y, hasattr(C, "Foo"), isinstance(read(), float)] +""" + ], + [2, False, 3, False, True], + ], + [ + [ + """ +x = 1 +def f(): + class C: + global x + x = 2 + x = 5 + return x +def g(): + global x + class C: + x = 3 + return C.x +result = [f(), x, g(), x] +""" + ], + [5, 2, 3, 2], + ], + [ + [ + """ +def pyscript_compile(func): + return func +@pyscript_compile +def make_meta(): + class Namespace: + def __init__(self): + self.values = {} + def __getitem__(self, name): + return self.values[name] + def __setitem__(self, name, value): + self.values[name] = value + def __contains__(self, name): + return name in self.values + class Meta(type): + @classmethod + def __prepare__(mcs, name, bases): + return Namespace() + def __new__(mcs, name, bases, namespace): + return type.__new__(mcs, name, bases, dict(namespace.values)) + return Meta +class C(metaclass=make_meta()): + class D: + x = 3 + y = D.x +result = [C.D.x, C.y] +""" + ], + [3, 3], + ], +] + + +def run_global_name_test_python(sources): + """Run the scripts in CPython and return the last one's result.""" + mod = ModuleType(GLOBAL_MOD_NAME) + FunctionType(compile(GLOBAL_MOD_SOURCE, GLOBAL_MOD_NAME, "exec"), mod.__dict__)() + sys.modules[GLOBAL_MOD_NAME] = mod + try: + for source in sources: + namespace = {} + FunctionType(compile(source, "test", "exec"), namespace)() + return namespace["result"] + finally: + del sys.modules[GLOBAL_MOD_NAME] + + +async def run_global_name_test(sources): + """Run the scripts in pyscript and return the last one's result.""" + mod = ModuleType(GLOBAL_MOD_NAME) + module_ctx = GlobalContext(GLOBAL_MOD_NAME, global_sym_table=mod.__dict__, manager=GlobalContextMgr) + module_ctx.module = mod + module_ctx.file_path = f"modules/{GLOBAL_MOD_NAME}.py" + module_ast = AstEval(GLOBAL_MOD_NAME, global_ctx=module_ctx) + Function.install_ast_funcs(module_ast) + module_ast.parse(GLOBAL_MOD_SOURCE) + await module_ast.eval() + GlobalContextMgr.set(f"modules.{GLOBAL_MOD_NAME}", module_ctx) + try: + for source in sources: + global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(source) + await ast.eval() + return global_ctx.global_sym_table["result"] + finally: + GlobalContextMgr.delete(f"modules.{GLOBAL_MOD_NAME}") + + +async def test_global_names_match_python(hass): + """Test module-level names used through global and from-imports, compared with CPython.""" + 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) + DecoratorRegistry.init(hass) + + for sources, expect in globalNameTests: + assert run_global_name_test_python(sources) == expect, sources[-1] + assert await run_global_name_test(sources) == expect, sources[-1] + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop() + + +async def test_class_body_exception_keeps_old_class(hass): + """Test that a class redefinition whose body raises keeps the old class, as in CPython.""" + 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) + + source = ( + GLOBAL_BASE_SOURCE + + """ +class Foo: + x = 5 + raise ValueError("bad class body") +""" + ) + check = "result = [Foo.x, read().x, read() is Foo]" + expect = [1, 1, True] + + namespace = {} + with pytest.raises(ValueError, match="bad class body"): + FunctionType(compile(source, "test", "exec"), namespace)() + FunctionType(compile(check, "test", "exec"), namespace)() + assert namespace["result"] == expect + + global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(source) + with pytest.raises(ValueError, match="bad class body"): + await ast.eval() + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(check) + await ast.eval() + assert global_ctx.global_sym_table["result"] == expect + + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop()