diff --git a/lisa/_cli_tools/lisa_load_kmod.py b/lisa/_cli_tools/lisa_load_kmod.py index db914aa1905b896a517ae82d737df0a5ce1979ca..07f2d8a844fb7b5865df14cdcdf2c6e6ae7aa84a 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 907fd24dd7aa630027f2c8f8a8ec4284bafe4fef..80f5ca02ede37c2e40d37611fbc8a45619b36cf0 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 6a0cea140d9dc4b5eb39aaa01a384917fabfeab7..a76944899858a0780ab200e8d3ae2aa4cf6f335d 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):