From f205bde7c8a32fda52a3db5cbe80b948830980ea Mon Sep 17 00:00:00 2001 From: David Ormrod Morley Date: Fri, 6 Dec 2024 10:42:40 +0100 Subject: [PATCH] Make warnings on accessing results of failed jobs less chatty SO107 SCMSUITE-8725 --- core/results.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/core/results.py b/core/results.py index ccb48425..3e2409cb 100644 --- a/core/results.py +++ b/core/results.py @@ -8,7 +8,7 @@ import threading from os.path import join as opj from subprocess import PIPE -from typing import List, Dict +from typing import List, Dict, Optional from scm.plams.core.errors import FileError, ResultsError from scm.plams.core.functions import config, log @@ -50,6 +50,19 @@ def _privileged_access(): return False +# def _is_called_from_job_method(method: List[str], prev_method: Optional[str]): +# """Analyze contents of the current stack to find out if current function was called by a given method from the same |Job| instance. +# """ +# from scm.plams.core.basejob import Job +# +# for frame in inspect.getouterframes(inspect.currentframe()): +# cal, arg = _caller_name_and_arg(frame[0]) +# prev_cal, prev_arg = _caller_name_and_arg(frame[0].f_back) +# if cal in method and (prev_calprev_cal == "_finalize" and arg == prev_arg and isinstance(arg, Job): +# return True +# return False + + def _restrict(func): """Decorator that wraps methods of |Results| instances. @@ -80,10 +93,21 @@ def guardian(self, *args, **kwargs): raise ResultsError("Using Results associated with deleted job") elif self.job.status in [JobStatus.CRASHED, JobStatus.FAILED]: - if func.__name__ == "wait": # waiting for crashed of failed job should not trigger any warnings/exceptions + # waiting for crashed of failed job should not trigger any warnings/exceptions + # and neither should checking the status from this job with 'ok', 'check' or 'get_errormsg' + suppress_errors = func.__name__ == "wait" + if not suppress_errors: + for frame in inspect.getouterframes(inspect.currentframe()): + cal, arg = _caller_name_and_arg(frame[0]) + if arg == self.job and cal in ["ok", "check", "get_errormsg"]: + suppress_errors = True + break + + if suppress_errors: cal, arg = _caller_name_and_arg(inspect.currentframe()) if isinstance(arg, Results): return func(self, *args, **kwargs) + if config.ignore_failure: log("WARNING: Trying to obtain results of crashed or failed job {}".format(self.job.name), 3) try: