From 07edbb40bd27b807519094df11b04d680c29900b Mon Sep 17 00:00:00 2001 From: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:33:55 -0600 Subject: [PATCH 1/2] unwrap module-level classes used in class body annotations (#875) A class defined at module level is stored in the global symbol table as an EvalLocalVar. ast_name only unwrapped that when the global table was the current symbol table, so inside another class's body a name like Foo came back as the wrapper. Most uses still worked through its __getattr__, but operators didn't, so an annotation such as "z: Foo | None = None" raised TypeError. Unwrap a defined EvalLocalVar when a name is found in the global symbol table after the local ones. A class that's still being defined stays wrapped, so a self-reference like "nxt: Node = None" inside Node keeps working as before. Names declared global inside a function are looked up and assigned as before. Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> --- custom_components/pyscript/eval.py | 8 ++++- tests/test_unit_eval.py | 57 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index e27f4306..75d4b690 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -1539,7 +1539,13 @@ async def ast_name(self, arg): if arg.id in self.global_sym_table: if self.curr_func and arg.id in self.curr_func.local_names: raise UnboundLocalError(f"local variable '{arg.id}' referenced before assignment") - return self.global_sym_table[arg.id] + # + # a class defined at module level is stored as an EvalLocalVar, so unwrap + # 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 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] != "_": diff --git a/tests/test_unit_eval.py b/tests/test_unit_eval.py index 549b725f..ab2f11b6 100644 --- a/tests/test_unit_eval.py +++ b/tests/test_unit_eval.py @@ -302,6 +302,63 @@ class Annotated: """, [["ok"], {"a": int, "b": int}, 3, True], ], + [ + """ +class Foo: + pass + +class Bar: + z: Foo | None = None + w: list[Foo] = [] +[Bar.__annotations__["z"] == (Foo | None), Bar.__annotations__["w"] == list[Foo], Bar.z] +""", + [True, True, None], + ], + [ + """ +from dataclasses import dataclass + +class Foo: + pass + +@dataclass +class Bar: + name: str + z: Foo | None = None + +foo = Foo() +obj1 = Bar("abc") +obj2 = Bar("xyz", foo) +[obj1.name, obj1.z, obj2.name, obj2.z is foo] +""", + ["abc", None, "xyz", True], + ], + [ + """ +class Node: + nxt: Node = None +[Node.nxt, list(Node.__annotations__)] +""", + [None, ["nxt"]], + ], + [ + """ +class Foo: + value = 1 + +def read(): + return Foo.value + +def keep(): + global Foo + Foo = Foo + +keep() +Foo = type("Replacement", (), {"value": 2}) +[Foo.value, read()] +""", + [2, 2], + ], ["Foo = [type('Foo', (), {'x': 100})]; Foo[0].x = 10; Foo[0].x", 10], ["Foo = [type('Foo', (), {'x': [100, 101]})]; Foo[0].x[1] = 10; Foo[0].x", [100, 10]], ["Foo = [type('Foo', (), {'x': [0, [[100, 101]]]})]; Foo[0].x[1][0][1] = 10; Foo[0].x[1]", [[100, 10]]], From cab9d143a1f7781bb773839cb10bdcf63f79691c Mon Sep 17 00:00:00 2001 From: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:59:20 +0000 Subject: [PATCH 2/2] update module-level names in place when assigned or deleted through global (#875) A module-level class is stored in the global symbol table as an EvalLocalVar, and functions defined at module level hold on to that wrapper. Used through a global declaration, the class still came back wrapped, so inside a function with "global Foo", "Foo | None" raised TypeError, "Foo is type(Foo())" was False and "Foo.x = 3" set x on the wrapper. Assigning or deleting through global replaced or removed the table entry, so functions defined earlier kept seeing the old class after "global Foo; Foo = Other" or "global Foo; del Foo". As suggested in review, assignment through global now calls .set() on an existing EvalLocalVar instead of replacing it, del through global calls set_undefined() on it (and raises NameError if it is already undefined, like the module-level branch), and a load through global unwraps it when it is defined. An undefined one is left wrapped, as the lookup that falls through to the global symbol table already does, so a class defined through global can still refer to itself in its annotations. Updating a wrapper in place is only safe if no other binding shares it, and functions defined earlier should also see a class defined again or imported under the same name. So: - Assignment, import and from-import now bind names through one helper, assign_name(). It stores the value held by a defined EvalLocalVar rather than the wrapper, so "from mod import Foo", "from mod import *" and "Foo = globals()['Original']" no longer share another binding's wrapper. Otherwise "global Foo; Foo = Other" or "del Foo" in the importer would change mod's own Foo, for every script importing mod. A plain module-level "Foo = Other" or "del Foo" after "from mod import Foo" already did this before. - Reading an attribute of a module, as in "mod.Foo", now also gives the value held by mod's EvalLocalVar, so "Foo = mod.Foo" and "globals()['Foo'] = mod.Foo" store the class itself, and "mod.Foo | None", "mod.Foo is mod.modread()" and isinstance(obj, mod.Foo) work. For a name mod has deleted it raises AttributeError, as in Python. "mod.Foo = Other" updates mod's EvalLocalVar in place, so mod's own functions see the new value, as in Python. - Like assignment, import and from-import now update an existing EvalLocalVar for the name, so functions defined earlier see the imported value, and a name declared global in a function is bound in the global table instead of locally. - Names bound by import or from-import inside a function are now local to it, as in Python, so a nested function can use them; for example "import mod as m" inside a function used to be invisible to a nested function. In a function with a nested function, reading any local name before it is bound, or after it is deleted, now raises UnboundLocalError instead of NameError, as in Python. Without a nested function, reading an imported name before the import raises UnboundLocalError when a module-level name of that name exists, instead of finding it. - In a class body, assignment, import, del, and class and def statements bind in the class namespace, even inside a function that declares the name global, and reading a name finds the class's own binding before the global one, as in Python. Assignment and import there replace an EvalLocalVar left by a nested class instead of updating it, so a method doesn't pick up the new value as an enclosing variable. A global declaration in the class body itself applies to the class body only, as in Python: the name is bound and read in the global table there, but no longer becomes global in the enclosing function once the class statement completes. In a class body at module level it no longer raises SyntaxError. - "from mod import Foo" of a name mod has deleted raises ImportError with Python's message, and "from mod import *" skips it, since del now leaves an undefined EvalLocalVar in mod. - A class statement always made a new EvalLocalVar, so functions defined earlier never saw a class defined again under the same name, and inside the new class body the name was undefined. It now reuses an existing wrapper and sets the new class once the class statement completes. Until then the name still holds the old class, also if the class body raises, as in Python. A first definition works as before. This changes how imports and aliases behave: "from mod import Foo" and "Foo = mod.Foo" now take a snapshot of mod's Foo, as in Python, and no longer follow later changes to Foo inside mod; "mod.Foo" itself does. Known limits: - getattr(mod, "Foo"), mod.__dict__ and vars(mod) still hand out mod's EvalLocalVar, and so does "mod.Foo" inside a lambda or @pyscript_compile function; after mod deletes Foo, hasattr(mod, "Foo") and dir(mod) still report it - a lambda or @pyscript_compile function gets a module-level class as its EvalLocalVar: reading an attribute works and follows a later rebind, but calling it or "|" raises TypeError, and "is", "==", isinstance() and issubclass() against the class give False - a function defined with @pyscript_compile in a class body that declares its name global is still bound in the class namespace, not the global table - in a function without a nested function, reading an imported name before the import gives the builtin of that name if there is one, and raises NameError if there is neither a module-level name nor a builtin, instead of UnboundLocalError - a dotted import such as "import os.path" inside a function is not local to it, so a nested function can't use it - a method reads a class nested in its class body (class C: class Foo: ...) as if it were an enclosing variable, instead of the module-level name - "global Zed; del Zed" on a name that was never defined doesn't raise NameError - after "global Foo; del Foo", a bare read of Foo through global gives back the undefined wrapper instead of raising NameError. Reading an attribute, calling it or using it in isinstance() still raises NameError, but "|" raises TypeError, and "is", "==", bool(), str() and setting an attribute don't raise at all Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> --- custom_components/pyscript/eval.py | 132 ++++- tests/test_unit_eval.py | 825 +++++++++++++++++++++++++++++ 2 files changed, 934 insertions(+), 23 deletions(-) diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index 75d4b690..fa405418 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/tests/test_unit_eval.py b/tests/test_unit_eval.py index ab2f11b6..267504d5 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()