From 6cb48101c3bdf5d46dbe8471c7b4c91db6090091 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Tue, 7 May 2024 11:19:24 +0100 Subject: [PATCH] lisa.target: Make Target a context manager FEATURE Make Target a context manager on its own rather than requiring .closing() --- lisa/_cli_tools/lisa_load_kmod.py | 2 +- lisa/_cli_tools/lisa_platinfo_extract.py | 2 +- lisa/target.py | 26 +++++++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lisa/_cli_tools/lisa_load_kmod.py b/lisa/_cli_tools/lisa_load_kmod.py index db914aa19..07f2d8a84 100755 --- a/lisa/_cli_tools/lisa_load_kmod.py +++ b/lisa/_cli_tools/lisa_load_kmod.py @@ -42,7 +42,7 @@ def main(): params=params, ) - with target.closing() as target: + with target as target: return _main(args, target) diff --git a/lisa/_cli_tools/lisa_platinfo_extract.py b/lisa/_cli_tools/lisa_platinfo_extract.py index 907fd24dd..80f5ca02e 100755 --- a/lisa/_cli_tools/lisa_platinfo_extract.py +++ b/lisa/_cli_tools/lisa_platinfo_extract.py @@ -30,7 +30,7 @@ def main(): } args, target = Target.from_custom_cli(params=params) - with target.closing() as target: + with target as target: return _main(args, target) def _main(args, target): diff --git a/lisa/target.py b/lisa/target.py index 6a0cea140..a76944899 100644 --- a/lisa/target.py +++ b/lisa/target.py @@ -42,7 +42,7 @@ from devlib.exception import TargetStableError from devlib.utils.misc import which from devlib.platform.gem5 import Gem5SimulationPlatform -from lisa.utils import Loggable, HideExekallID, resolve_dotted_name, get_subclasses, import_all_submodules, LISA_HOME, RESULT_DIR, LATEST_LINK, setup_logging, ArtifactPath, nullcontext, ExekallTaggable, memoized, destroyablecontextmanager, ContextManagerExit, update_params_from +from lisa.utils import Loggable, HideExekallID, resolve_dotted_name, get_subclasses, import_all_submodules, LISA_HOME, RESULT_DIR, LATEST_LINK, setup_logging, ArtifactPath, nullcontext, ExekallTaggable, memoized, destroyablecontextmanager, ContextManagerExit, update_params_from, deprecate from lisa._assets import ASSETS_PATH from lisa.conf import SimpleMultiSrcConf, KeyDesc, LevelKeyDesc, TopLevelKeyDesc, Configurable, DelegatedLevelKeyDesc, ConfigKeyError from lisa._kmod import _KernelBuildEnv, DynamicKmod, _KernelBuildEnvConf @@ -1305,11 +1305,16 @@ class Target(Loggable, HideExekallID, ExekallTaggable, Configurable): return wrapper return wrapper_param - + @deprecate(deprecated_in='3.1.0', removed_in='4.0') def closing(self): """ - Returns a context manager that will disconnect the target automatically. + Returns a context manager that will disconnect the target automatically.. + + .. warning:: This method is deprecated and not useful anymore as the + :class:`lisa.target.Target` object itself can be used as a context + manager. """ + return self # Do not use contextlib.contextmanager() as it would force the __exit__ # path to run, since destroying a suspended generator raises # GeneratorExit at the yield point. @@ -1322,6 +1327,21 @@ class Target(Loggable, HideExekallID, ExekallTaggable, Configurable): return _ClosingCM() + def __enter__(self): + try: + self.target.__enter__() + # Older versions of devlib don't support __enter__() + except AttributeError: + pass + return self + + def __exit__(self, *args, **kwargs): + try: + self.target.__exit__(*args, **kwargs) + # Older versions of devlib don't support __enter__() + except AttributeError: + self.target.disconnect() + class Gem5SimulationPlatformWrapper(Gem5SimulationPlatform): def __init__(self, system, simulator, **kwargs): -- GitLab