From cfbebb72019169fe735cc3dc3b7c8a57aba68fca Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 10 Apr 2022 12:58:01 -0700 Subject: [PATCH] Better support for helper functions in pexpect When a pexpect test fails, it reports the "failing line." Prior to this commit, it did so by walking up the Python call stack, looking for the first frame which is not in the pexpect_helper module, and so presumably in the test itself. However sometimes the test wants to define a helper function; then if the test fails the helper function is reported as the failing line, not the callsite of the helper. Fix this by skipping functions which have the `callsite_skip` attribute set. Nothing to relnote here. --- build_tools/pexpect_helper.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/build_tools/pexpect_helper.py b/build_tools/pexpect_helper.py index 4eda5be3c..fdd6a2192 100644 --- a/build_tools/pexpect_helper.py +++ b/build_tools/pexpect_helper.py @@ -48,8 +48,13 @@ def get_callsite(): """Return a triple (filename, line_number, line_text) of the call site location.""" callstack = inspect.getouterframes(inspect.currentframe()) for f in callstack: - if inspect.getmodule(f.frame) is not Message.MODULE: - return (os.path.basename(f.filename), f.lineno, f.code_context) + # Skip call sites from this file. + if inspect.getmodule(f.frame) is Message.MODULE: + continue + # Skip functions which have a truthy callsite_skip attribute. + if getattr(f.function, "callsite_skip", False): + continue + return (os.path.basename(f.filename), f.lineno, f.code_context) return ("Unknown", -1, "")