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.
This commit is contained in:
ridiculousfish 2022-04-10 12:58:01 -07:00
parent cb027bfdc0
commit cfbebb7201

View File

@ -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, "")