diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index 76abac3..5d46f73 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -316,6 +316,16 @@ def getattr(self): """Get the attribute value.""" return unwrap_defined(getattr(self.obj, self.attr)) + def delattr(self): + """Delete the attribute.""" + # as with setattr, mark a pyscript module's EvalLocalVar undefined in place + if isinstance(self.obj, ModuleType) and isinstance(vars(self.obj).get(self.attr), EvalLocalVar): + if not vars(self.obj)[self.attr].is_defined(): + raise AttributeError(f"module '{self.obj.__name__}' has no attribute '{self.attr}'") + vars(self.obj)[self.attr].set_undefined() + else: + delattr(self.obj, self.attr) + class EvalFunc: """Class for a callable pyscript function.""" @@ -840,21 +850,38 @@ async def check_for_closure(self, arg): class EvalFuncVar: """Class for a callable pyscript function.""" + # keep our own fields out of __dict__, so functools.wraps() can't copy them onto a wrapper + __slots__ = ("__dict__", "__weakref__", "ast_ctx", "func") + def __init__(self, func: EvalFunc) -> None: """Initialize instance with given EvalFunc function.""" self.func = func self.ast_ctx: AstEval | None = None + @property + def __name__(self): + """Return the function name, like a Python function.""" + return self.func.get_name() + + @__name__.setter + def __name__(self, name): + """Set the function name, eg by functools.wraps().""" + self.func.set_name(name) + + @property + def __doc__(self): + """Return the function doc string, like a Python function.""" + return self.func.get_doc_string() + + @__doc__.setter + def __doc__(self, doc_string): + """Set the function doc string, eg by functools.wraps().""" + self.func.doc_string = doc_string + def get_func(self) -> EvalFunc: """Return the EvalFunc function.""" return self.func - def remove_func(self): - """Remove and return the EvalFunc function.""" - func = self.func - self.func = None - return func - async def call(self, ast_ctx, *args, **kwargs): """Call the EvalFunc function.""" return await self.func.call(ast_ctx, *args, **kwargs) @@ -897,6 +924,8 @@ async def __call__(self, *args, **kwargs): class EvalFuncVarClassInst(EvalFuncVar): """Class for a callable pyscript class instance function.""" + __slots__ = ("class_inst_weak",) + def __init__(self, func: EvalFunc, ast_ctx: "AstEval", class_inst_weak: weakref.ReferenceType) -> None: """Initialize instance with given EvalFunc function.""" super().__init__(func) @@ -1191,17 +1220,30 @@ async def executor_wrap(*args, **kwargs): dec_trig, dec_other, dec_dm = await func.eval_decorators(self) self.dec_eval_depth += 1 for dec_func in dec_other: - func = await self.call_func(dec_func, None, func) + if isinstance(func, EvalFunc): + # pass decorators an EvalFuncVar, like a Python function object; functools.wraps() + # then only updates its __dict__, not the fields of the wrapper's EvalFunc + func = EvalFuncVar(func) + func.set_ast_ctx(self) + func_arg = func + func = await self.call_func(dec_func, None, func_arg) if isinstance(func, EvalFuncVar): # set the function name back to its original instead of the decorator function we just called func.set_name(name) - func = func.remove_func() - dec_trig += func.decorators - dec_dm += func.dm_decorators + if func is not func_arg: + dec_trig += func.get_func().decorators + dec_dm += func.get_func().dm_decorators elif isinstance(func, EvalFunc): func.set_name(name) self.dec_eval_depth -= 1 if isinstance(func, EvalFunc): + func = EvalFuncVar(func) + if isinstance(func, EvalFuncVar): + # keep the EvalFuncVar a decorator returned, since the decorator may hold + # references to it and may have set attributes like __wrapped__ on it + func_var = func + func_var.set_ast_ctx(self) + func = func_var.get_func() func.decorators = dec_trig func.dm_decorators = dec_dm if self.dec_eval_depth == 0: @@ -1210,14 +1252,9 @@ async def executor_wrap(*args, **kwargs): await func.trigger_init(self.global_ctx, name) except Exception as e: self.log_exception(e) - func_var = EvalFuncVar(func) - func_var.set_ast_ctx(self) if len(dec_dm) > 0: await self.get_global_ctx().create_decorator_manager(dec_dm, self, func_var) - else: - func_var = EvalFuncVar(func) - func_var.set_ast_ctx(self) else: func_var = func @@ -1534,10 +1571,12 @@ async def ast_delete(self, arg): else: raise NameError(f"name '{arg1.id}' is not defined") elif isinstance(arg1, ast.Attribute): - var_name = await self.ast_attribute_collapse(arg1, check_undef=False) - if not isinstance(var_name, str): - raise NameError("state name should be 'domain.entity' or 'domain.entity.attr'") - State.delete(var_name) + # as with assignment, a dotted name is a state only if its first part isn't defined + var_name = await self.ast_attribute_collapse(arg1) + if var_name is not None: + State.delete(var_name) + else: + EvalAttrSet(await self.aeval(arg1.value), arg1.attr).delattr() else: raise NotImplementedError(f"unknown target type {arg1} in del") diff --git a/custom_components/pyscript/global_ctx.py b/custom_components/pyscript/global_ctx.py index 0eb5c47..0d4ac5b 100644 --- a/custom_components/pyscript/global_ctx.py +++ b/custom_components/pyscript/global_ctx.py @@ -178,6 +178,9 @@ def find_first_file(file_paths: list[list[str]]) -> list[str] | None: if path.endswith("/__init__"): path = os.path.dirname(path) ctx_name = self.name + if os.path.basename(self.file_path or "") != "__init__.py" and "." in ctx_name: + # a plain module is relative to its parent package, so drop its own name + ctx_name = ctx_name[0 : ctx_name.rfind(".")] for _ in range(import_level - 1): path = os.path.dirname(path) idx = ctx_name.rfind(".") @@ -185,7 +188,7 @@ def find_first_file(file_paths: list[list[str]]) -> list[str] | None: raise ImportError("attempted relative import above parent package") ctx_name = ctx_name[0:idx] ctx_name += f".{module_name}" - module_info = [ctx_name, f"{path}/{module_path}.py", path] + module_info = [ctx_name, f"{path}/{module_path}.py", os.path.dirname(f"{path}/{module_path}.py")] path += f"/{module_path}" file_paths.append([ctx_name, f"{path}/__init__.py", path]) file_paths.append(module_info) diff --git a/docs/plans/issues-batch-2.md b/docs/plans/issues-batch-2.md new file mode 100644 index 0000000..606de3c --- /dev/null +++ b/docs/plans/issues-batch-2.md @@ -0,0 +1,95 @@ +# Plan: issues-batch-2 + +## What Sean asked for +"look at issues in custom-components/pyscript find me 5 of the most important ones and start fixing them" + +## What we found +We went through all 138 open issues and checked each likely bug on upstream's latest `master` (`309a3ff`). +- Most are feature requests, how-to questions, Home Assistant or install problems, or already fixed. About 25 are already fixed on `master` (for example #512, #538, #761, #792, #819 and the blocking-call warnings #608, #612, #619 and #620). Others are covered by open upstream PRs (#899, #896, #907, #908, #869). +- **Only two open issues are real, still-broken bugs that a small fix can solve.** We reproduced both: + - **#808:** a decorator written with `functools.wraps` loses its own body. Its `try`/`except` never runs, so the error it should catch gets through. + - **#726:** in an app with subfolders, a correct relative import (`from .a import foo` in `subfolder/b.py`) fails. A reload can also unload part of the app. +- We also found one bug while checking that has no issue yet: `del obj.attr` on a normal object fails with `NameError`, because pyscript treats it as deleting a Home Assistant state. +- Two issues ask for clearer docs, and the maintainer agreed in the threads: logging for apps and scripts (#821, #520, #265), and `watch=` firing only when a value changes (#864). + +So the five are two bug fixes, one small bug fix without an issue, and two docs fixes. Generator expressions (#564) are **left out** (see "Out of scope"). We can add them if you want them. + +## What we'll change +Each item becomes its own small upstream PR, cut from upstream `master`, with its tests. + +1. **#808: decorators that use `functools.wraps`** (`custom_components/pyscript/eval.py`, `tests/test_unit_eval.py`). + - Before calling a user decorator, pyscript will hand it a wrapped copy of the function. That way `wraps` can't overwrite pyscript's own fields on the decorator's inner function. + - If the decorator returns the function unchanged, it is unwrapped again. + - *Check:* new tests cover a `wraps` decorator that catches an error, one that passes arguments through, stacked decorators, one that returns the function unchanged, and that `__wrapped__` is set. Each test fails on today's code and passes with the fix, and each result matches real Python. +2. **#726: relative imports inside an app's subfolders** (`custom_components/pyscript/global_ctx.py`, `tests/test_apps_modules.py`). + - A module imported as `subfolder.b` will resolve its own relative imports from `subfolder/`, as Python does. Today it uses the app's top folder. + - This is a one-line change. + - *Check:* new tests cover the issue's exact example, and confirm that a reload unloads nothing it shouldn't. The PR text will say plainly that scripts using today's workaround (`from .subfolder.a` inside `subfolder/b.py`) need to switch to the normal form. +3. **`del obj.attr` on normal objects** (`custom_components/pyscript/eval.py`, `tests/test_unit_eval.py`). + - Only names that refer to Home Assistant states (`domain.entity`, `domain.entity.attr`) will be deleted as states. + - Attributes of normal objects, including `del self.x` in a method, will be deleted like in Python. + - *Check:* tests cover objects, `self` in methods, a missing attribute (`AttributeError`, as in Python), and the existing state-deletion tests still pass. +4. **Docs: logging** (`docs/reference.rst`, maybe `docs/tutorial.rst`), for #821, #520 and #265. + - The docs will give the logger names for apps (`custom_components.pyscript.apps.`), scripts and modules. + - They will add one full `logger:` example: quiet by default, one app at info, one script at debug. + - *Check:* the docs build cleanly with the project's Sphinx setup, and the logger names match the code. +5. **Docs: `@state_trigger` and repeated values** (`docs/reference.rst`), for #864. + - The docs will say that `watch=` and trigger expressions fire only when a value changes, so a sensor that repeats the same value doesn't fire them. + - They will show the workaround from the issue (`watch=["sensor.x.last_updated"]`). + - *Check:* the docs build cleanly, and the behaviour matches the code (`trigger.py`). +6. **All project checks** on Python 3.14, as CI runs them: `pytest`, `ruff check`, `ruff format --check`, `pylint`, `mypy`, and the pre-commit hooks (including codespell). +7. **Upstream PRs.** After your final approval, each fix goes on its own clean branch from upstream `master`, with a prefilled link for you to open the PR. Each PR description uses "Closes #N" (or no issue for item 3), then what was broken, why, what changed, and testing. + +## Contribution rules that apply +- The project has `docs/contributing.rst` and `tests/README.md`, and no CONTRIBUTING.md or PR template. Their rules: + - Set up the dev environment from `tests/requirements_test.txt`. + - Run the pre-commit hooks: ruff check and format, codespell, check-json, yamllint, pylint and mypy. + - Run `pytest`, and send PRs from a fork. +- CI (`validate.yml`, Python 3.14) runs hassfest, ruff, pytest with coverage, pylint and mypy. +- The maintainer's habits: one small fix per PR, with its tests and with a docs change when behaviour changes. Titles name the fix and the issue number. Commits are signed off. + +## Who's on it +Three Engineers on separate files, working in parallel: +- one Engineer for items 1 and 3 (`eval.py`, separate parts); +- one for item 2 (`global_ctx.py`); +- one for items 4 and 5 (docs). + +After that, QA, the Security Officer and the Auditor review the work. Research is done (above). There's no second security review, because no security-related code is touched. + +## Risks +- **#808** changes how every user decorator gets its function. The whole test suite passes with a first version of the fix, and the Auditor will look closely at this item. +- **#726** changes behaviour for anyone using today's workaround. This is stated in the PR text. +- **Overlap with open upstream PRs:** #907 and #909 also touch `eval.py`. Whichever merges later may need a small update; we'd do it. + +## Out of scope +- **#564, generator expressions** (for example `sum(x for x in y)`). A 4-line version works, but it builds the whole list first. So `any(...)` doesn't stop early, and an endless generator would freeze Home Assistant. The maintainer called a full version "quite hard". This is a feature that needs his buy-in, not a bug fix. +- Issues that need the maintainer's design: #891, #877 and #885, #831, #788 (stubs for `service.call`), and #855 (Jupyter listen address). +- Commenting on the issues that are already fixed on `master`. This stays on the roadmap, if you want it. + +--- + +## Record (2026-11-21) + +**Status:** built and checked, with no open problems. Not yet sent upstream. + +**What shipped on this branch:** five commits, each one meant to become its own upstream PR. Each one applies on its own to upstream `master` (`309a3ff`) and passes the tests there. +- `181956e` keep a decorator's wrapper body when it uses functools.wraps (#808): `eval.py` +42/−15, 6 new test cases. +- `75137bc` delete object attributes in del instead of treating them as states: `eval.py` +16/−4, 5 new test cases. +- `d16e990` document the logger names for scripts, apps and modules (#821, #520, #265): docs only. +- `cf83267` document that state triggers only fire when a value changes (#864): docs only. +- `c53ef6c` fix relative imports inside an app's subfolders (#726): `global_ctx.py` +4/−1, 1 new test. + +**Checks:** all 207 tests, ruff, pylint, mypy, codespell and a strict Sphinx docs build pass on Python 3.14. hassfest only runs on GitHub. The new tests fail on upstream's code without the fixes, and every new case matches real Python. + +**Review rounds:** +- QA: passed. +- Security: clear. +- Audit: + - **Must-fix, fixed:** the #726 package check compared the end of the file path with `/__init__.py`. On Windows that would treat an app's `__init__.py` as a plain module. It now uses `os.path.basename`. The fix was checked by rerunning the tests, ruff, mypy and pylint. + - **Should-fix, not changed:** after `del mod.Foo` (a class defined in another pyscript module), `hasattr(mod, "Foo")` stays True. The class is kept as an empty placeholder so that the module's own functions see it as deleted, and pyscript already does the same for a module-level `del Foo`. This is a rare case and is a known limit. + +**Decisions:** +- The #808 fix keeps the function object a decorator returns, and doesn't unwrap it as the first version did. A decorator that stores the function (a registry) keeps a working one, and attributes like `__wrapped__` survive. `remove_func()` had no other users, so it was removed. +- #726 needed a second small change beyond the first idea. A plain module's relative imports now start from its package, not from itself, which removes the phantom contexts. Scripts that use the old workaround (`from .subfolder.a` inside `subfolder/b.py`) must switch to `from .a`. The PR text says so. +- Unchanged: relative imports from a plain file inside a `modules/` package still aren't supported, as before. +- The plan check and the plain-English check were skipped: their reviewer's monthly quota is used up. diff --git a/docs/reference.rst b/docs/reference.rst index d649c9b..35054a8 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -475,6 +475,16 @@ Optional arguments are: rendering those other variables as only conditions in the trigger expression that won't cause a trigger themselves, since the expression won't be evaluated when they change. + Note that "change" means the value actually changes: a state update that repeats the same value + (e.g., a sensor with ``force_update: true`` that reports the same reading again) doesn't evaluate + the expression or cause a trigger, even if the variable is in ``watch`` or is a plain state variable + name argument. To trigger on every update, watch the ``last_updated`` attribute, which changes + each time, for example: + + .. code:: python + + @state_trigger("True", watch=["sensor.my_sensor.last_updated"]) + Here's a summary of the trigger behavior with these parameter settings: =================== ==================== ================= ======================== @@ -1356,6 +1366,24 @@ where ``FUNCNAME`` is the name of the top-level Python function (e.g., the one c or service), defined in the script file ``SCRIPTNAME.py``. See the `Global Context <#global-context>`__ section for the logging paths for other cases. +The path starts with the global context name, so it depends on where the file lives. Replace +``file.SCRIPTNAME`` in the path above with: + +=========================================== ===================================================== +Location Logging path +=========================================== ===================================================== +``pyscript/SCRIPTNAME.py`` ``custom_components.pyscript.file.SCRIPTNAME`` +``pyscript/scripts/SCRIPTNAME.py`` ``custom_components.pyscript.scripts.SCRIPTNAME`` +``pyscript/apps/APP.py`` or ``APP/`` ``custom_components.pyscript.apps.APP`` +``pyscript/modules/MODULE.py`` ``custom_components.pyscript.modules.MODULE`` +Jupyter session ``custom_components.pyscript.jupyter_NNN`` +=========================================== ===================================================== + +An app is not under ``file``, so ``custom_components.pyscript.file`` settings don't apply to it; use +``custom_components.pyscript.apps`` instead. Pyscript's own internal messages (for example, a +trigger that is ignored because it is not active) are logged under ``custom_components.pyscript`` +itself (for example, ``custom_components.pyscript.trigger``), not under the script or app. + That allows you to set the log level for each Python top-level script or function separately if necessary. That setting also applies to any other Python functions that the top-level Python function calls. For example, these settings: @@ -1372,13 +1400,30 @@ will log all messages at ``info`` or higher (i.e., ``log.info()``, ``log.warning ``log.error()``), and inside ``my_function`` defined in the script file ``my_script.py`` (and any other functions it calls) will log all messages at ``debug`` or higher. +Here is a fuller example: quiet by default, pyscript's own messages and the app ``my_app`` at ``info``, +and the script ``my_script.py`` at ``debug``: + +.. code:: yaml + + logger: + default: warning + logs: + custom_components.pyscript: info + custom_components.pyscript.apps.my_app: info + custom_components.pyscript.file.my_script: debug + +Setting ``custom_components.pyscript`` to ``debug`` also shows the internal trigger messages (such as +``trigger ... got ... trigger, but not active``), which can be very noisy. + Note that debug logging also includes pyscript's own internal messages, which can show the full arguments passed to your functions, such as webhook payloads, service call data and event data. These can contain tokens or other sensitive information, so review debug logs before sharing them (e.g., in a GitHub issue), and turn debug logging off when you no longer need it. -Note that in Jupyter, all the ``log`` functions will display output in your session, independent of -the ``logger`` configuration settings. +Note that in Jupyter, the ``log`` functions display their output in your session, but only for messages +that pass the ``logger`` configuration settings for your session's path +(``custom_components.pyscript.jupyter_NNN``, which falls under ``custom_components.pyscript``). If you +see no output, enable that path (for example, ``custom_components.pyscript: info``). Changing the log level via the main configuration yaml file will typically require a restart of HASS. To avoid that, you can set the log level for any specific logging path by calling the ``logger.set_level`` diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 177442c..093c915 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -63,9 +63,9 @@ hey, this is just an example.) .. note:: - You'll need to look at the log messages to see the output (unless you are using Jupyter, in which - case all log messages will be displayed, independent of the log setting). The log message won't - be visible unless the ``Logger`` is enabled at least for level ``info``, for example: + You'll need to look at the log messages to see the output (in Jupyter, they are displayed in your + session). The log message won't be visible, including in Jupyter, unless the ``Logger`` is enabled + at least for level ``info``, for example: .. code:: yaml diff --git a/tests/test_apps_modules.py b/tests/test_apps_modules.py index 2ccf524..7114f41 100644 --- a/tests/test_apps_modules.py +++ b/tests/test_apps_modules.py @@ -2,6 +2,7 @@ from ast import literal_eval import asyncio +from contextlib import ExitStack import re from unittest.mock import patch @@ -9,6 +10,7 @@ import pytest from custom_components.pyscript.const import DOMAIN, FOLDER +from custom_components.pyscript.global_ctx import GlobalContextMgr from homeassistant.const import EVENT_STATE_CHANGED from homeassistant.setup import async_setup_component @@ -213,3 +215,117 @@ async def state_changed(event): assert "ModuleNotFoundError: import of no_such_package not allowed" in caplog.text assert "SyntaxError: expected ':'" in caplog.text assert 'modules/bad_module.py", line 2' in caplog.text + + +@pytest.mark.asyncio +async def test_app_subfolder_relative_imports(hass, caplog): + """Test relative imports between modules in an app's subfolders, from #726.""" + + conf_dir = hass.config.path(FOLDER) + + file_contents = { + f"{conf_dir}/apps/myapp/__init__.py": """ +from .subfolder.a import foo +from .subfolder.b import bar, baz +from .subfolder.deep.c import qux + +@service(supports_response="only") +def myapp_func(): + return {"done": [foo(), bar(), baz(), qux()]} +""", + f"{conf_dir}/apps/myapp/subfolder/a.py": """ +def foo(): + return "foo" +""", + # + # a plain module in a subfolder importing its sibling + # + f"{conf_dir}/apps/myapp/subfolder/b.py": """ +from .a import foo + +def bar(): + return "bar" + foo() + +def baz(): + return __name__ +""", + # + # a deeper subfolder importing from the parent subfolder + # + f"{conf_dir}/apps/myapp/subfolder/deep/c.py": """ +from ..a import foo +from .. import b + +def qux(): + return foo() + b.bar() +""", + } + + mock_open = MockOpen() + for key, value in file_contents.items(): + mock_open[key].read_data = value + + def isfile_side_effect(arg): + return arg in file_contents + + def glob_side_effect(path, recursive=None, root_dir=None, dir_fd=None, include_hidden=False): + result = [] + path_re = path.replace("*", "[^/]*").replace(".", "\\.") + path_re = path_re.replace("[^/]*[^/]*/", ".*") + for this_path in file_contents: + if re.match(path_re, this_path): + result.append(this_path) + return result + + conf = {"apps": {"myapp": {}}} + + def file_patches(mtime): + stack = ExitStack() + for this_patch in ( + patch("custom_components.pyscript.os.path.isdir", return_value=True), + patch("custom_components.pyscript.glob.iglob", side_effect=glob_side_effect), + patch("custom_components.pyscript.global_ctx.open", mock_open), + patch("custom_components.pyscript.open", mock_open), + patch("homeassistant.config.load_yaml_config_file", return_value={"pyscript": conf}), + patch("custom_components.pyscript.watchdog_start", return_value=None), + patch("custom_components.pyscript.os.path.getmtime", return_value=mtime), + patch("custom_components.pyscript.global_ctx.os.path.getmtime", return_value=mtime), + patch("custom_components.pyscript.global_ctx.os_path_isfile", side_effect=isfile_side_effect), + ): + stack.enter_context(this_patch) + return stack + + with file_patches(1000): + assert await async_setup_component(hass, "pyscript", {DOMAIN: conf}) + + await hass.async_start() + await hass.async_block_till_done() + + expected = {"done": ["foo", "barfoo", "myapp.subfolder.b", "foobarfoo"]} + + def ctx_names(): + return sorted(name for name, _ in GlobalContextMgr.items()) + + assert hass.services.has_service("pyscript", "myapp_func") + ret = await hass.services.async_call("pyscript", "myapp_func", {}, return_response=True, blocking=True) + assert ret == expected + + # + # no phantom contexts like apps.myapp.subfolder.b.a, so a reload keeps everything + # + names = ctx_names() + assert names == [ + "apps.myapp", + "apps.myapp.subfolder.a", + "apps.myapp.subfolder.b", + "apps.myapp.subfolder.deep.c", + ] + + with file_patches(2000): + await hass.services.async_call("pyscript", "reload", {}, blocking=True) + await hass.async_block_till_done() + + assert ctx_names() == names + assert hass.services.has_service("pyscript", "myapp_func") + ret = await hass.services.async_call("pyscript", "myapp_func", {}, return_response=True, blocking=True) + assert ret == expected diff --git a/tests/test_unit_eval.py b/tests/test_unit_eval.py index c9d1441..8bb1749 100644 --- a/tests/test_unit_eval.py +++ b/tests/test_unit_eval.py @@ -398,6 +398,74 @@ def keep(): "Foo = [type('Foo', (), {'x': [0, [[100, 101, 102, 103]]]})]; Foo[0].x[1][0][1:2] = [11, 12]; Foo[0].x[1]", [[100, 11, 12, 102, 103]], ], + [ + """ +class A: + pass + +a = A() +a.x = 1 +a.y = 2 +del a.x +[hasattr(a, "x"), a.y, vars(a)] +""", + [False, 2, {"y": 2}], + ], + [ + """ +class Cache: + def __init__(self): + self.value = 5 + + def clear(self): + del self.value + + def has_value(self): + return hasattr(self, "value") + +cache = Cache() +before = cache.has_value() +cache.clear() +[before, cache.has_value()] +""", + [True, False], + ], + [ + """ +class A: + pass + +a = A() +results = [] +try: + del a.missing + results.append("no error") +except AttributeError as exc: + results.append("AttributeError") +a.items = [A()] +a.items[0].z = 3 +del a.items[0].z +results.append(hasattr(a.items[0], "z")) +results +""", + ["AttributeError", False], + ], + [ + """ +import types + +class A: + x = 1 + y = 2 + +mod = types.ModuleType("mod") +mod.value = 10 +del A.x +del mod.value +[hasattr(A, "x"), A.y, hasattr(mod, "value")] +""", + [False, 2, False], + ], [ "pyscript.var1 = 1; pyscript.var2 = 2; set(state.names('pyscript'))", {"pyscript.var1", "pyscript.var2"}, @@ -1217,6 +1285,152 @@ def func(incr): ], [ """ +from functools import wraps + +errors = [] + +def catch_all(func): + @wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as exc: + errors.append(str(exc)) + return "caught" + return wrapper + +@catch_all +def fails(value): + raise ValueError(f"bad value {value}") + +[fails(1), fails(value=2), errors] +""", + ["caught", "caught", ["bad value 1", "bad value 2"]], + ], + [ + """ +from functools import wraps + +calls = [] + +def trace(func): + @wraps(func) + def wrapper(*args, **kwargs): + calls.append([args, kwargs]) + return func(*args, **kwargs) * 10 + return wrapper + +@trace +def add(a, b=2, *, c=3): + return a + b + c + +[add(1), add(1, 5, c=7), add(a=4, b=0, c=0), calls] +""", + [60, 130, 40, [[(1,), {}], [(1, 5), {"c": 7}], [(), {"a": 4, "b": 0, "c": 0}]]], + ], + [ + """ +from functools import wraps + +calls = [] + +def trace(func): + @wraps(func) + def wrapper(*args, **kwargs): + calls.append(f"trace {func.__name__}") + return func(*args, **kwargs) + return wrapper + +def double(func): + def double_wrapper(*args, **kwargs): + calls.append("double") + return 2 * func(*args, **kwargs) + return double_wrapper + +@double +@trace +def inc1(x): + return x + 1 + +@trace +@trace +def inc2(x): + return x + 2 + +[inc1(1), inc2(1), calls] +""", + [4, 3, ["double", "trace inc1", "trace inc2", "trace inc2"]], + ], + [ + """ +registry = [] + +def register(func): + registry.append(func) + return func + +@register +@register +def inc(x): + return x + 1 + +[inc(1), registry[0](2), registry[0] is inc, registry[1] is inc] +""", + [2, 3, True, True], + ], + [ + """ +from functools import wraps + +def trace(func): + @wraps(func) + def wrapper(*args, **kwargs): + return func(*args, **kwargs) + return wrapper + +@trace +def inc(x): + '''Add one to x.''' + return x + 1 + +[inc.__name__, inc.__doc__, inc.__wrapped__(1), inc.__wrapped__.__name__, "__wrapped__" in vars(inc)] +""", + ["inc", "Add one to x.", 2, "inc", True], + ], + [ + """ +from functools import wraps + +errors = [] + +def catch_all(func): + @wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as exc: + errors.append(str(exc)) + return -1 + return wrapper + +class Counter: + def __init__(self): + self.count = 0 + + @catch_all + def incr(self, step): + if step < 0: + raise ValueError(f"bad step {step}") + self.count += step + return self.count + +counter = Counter() +[counter.incr(2), counter.incr(-1), counter.incr(3), errors, Counter.incr.__name__] +""", + [2, -1, 5, ["bad step -1"], "incr"], + ], + [ + """ def f1(value): i = 1 def f2(value): @@ -3154,6 +3368,31 @@ class Other: [ [ """ +import mod_global +del mod_global.Foo +try: + mod_global.modread() + called = "no error" +except NameError: + called = "NameError" +try: + mod_global.Foo + read = "no error" +except AttributeError: + read = "AttributeError" +try: + del mod_global.Foo + again = "no error" +except AttributeError: + again = "AttributeError" +result = [called, read, again] +""" + ], + ["NameError", "AttributeError", "AttributeError"], + ], + [ + [ + """ import math def f(): def inner():