diff --git a/testplan/testing/multitest/base.py b/testplan/testing/multitest/base.py index 8f6eb463b..5b0fccba4 100644 --- a/testplan/testing/multitest/base.py +++ b/testplan/testing/multitest/base.py @@ -1158,7 +1158,7 @@ def _run_testcase( ), ) - if self.cfg.collect_code_context: + if getattr(self.cfg, "collect_code_context", False): testcase = collect_code_context(func=testcase) # specially handle skipped testcases diff --git a/testplan/testing/result.py b/testplan/testing/result.py index 6310fdaf2..6cbd1eab5 100644 --- a/testplan/testing/result.py +++ b/testplan/testing/result.py @@ -93,12 +93,14 @@ def __exit__(self, exc_type, exc_value, tb): description=self.description, ) - with MOD_LOCK: - # TODO: see https://github.com/python/cpython/commit/85cf1d514b84dc9a4bcb40e20a12e1d82ff19f20 - caller_frame = inspect.stack()[1] + if getattr(assertion_state, "collect_code_context", False): + with MOD_LOCK: + # TODO: see https://github.com/python/cpython/commit/85cf1d514b84dc9a4bcb40e20a12e1d82ff19f20 + caller_frame = inspect.stack()[1] - exc_assertion.file_path = os.path.abspath(caller_frame[1]) - exc_assertion.line_no = caller_frame[2] + exc_assertion.file_path = os.path.abspath(caller_frame[1]) + exc_assertion.line_no = caller_frame[2] + exc_assertion.code_context = caller_frame.code_context[0].strip() # We cannot use `bind_entry` here as this block will # be run when an exception is raised @@ -1383,10 +1385,13 @@ def __exit__(self, exc_type, exc_value, traceback): return False super().__exit__(exc_type, exc_value, traceback) - with MOD_LOCK: - # TODO: see https://github.com/python/cpython/commit/85cf1d514b84dc9a4bcb40e20a12e1d82ff19f20 - # XXX: do we have concrete ideas about thread-safety here? - caller_frame = inspect.stack()[1] + if getattr(assertion_state, "collect_code_context", False): + with MOD_LOCK: + # TODO: see https://github.com/python/cpython/commit/85cf1d514b84dc9a4bcb40e20a12e1d82ff19f20 + # XXX: do we have concrete ideas about thread-safety here? + caller_frame = inspect.stack()[1] + else: + caller_frame = None assertion = assertions.LogfileMatch( self.timeout, @@ -1395,8 +1400,11 @@ def __exit__(self, exc_type, exc_value, traceback): self.description, self.category, ) - assertion.file_path = os.path.abspath(caller_frame[1]) - assertion.line_no = caller_frame[2] + + if caller_frame: + assertion.file_path = os.path.abspath(caller_frame[1]) + assertion.line_no = caller_frame[2] + # assertion.code_context = caller_frame.code_context[0].strip() stdout_registry.log_entry( entry=assertion, stdout_style=self.result.stdout_style diff --git a/tests/functional/testplan/runnable/interactive/interactive_executable.py b/tests/functional/testplan/runnable/interactive/interactive_executable.py index 034b413e1..5e7bd0634 100644 --- a/tests/functional/testplan/runnable/interactive/interactive_executable.py +++ b/tests/functional/testplan/runnable/interactive/interactive_executable.py @@ -447,7 +447,7 @@ def main(): module_info.original_report for module_info in TEST_SUITE_MODULES ], actual_reports=_get_actual_reports(plan), - ignore=["file_path", "line_no", "machine_time", "utc_time"], + ignore=["file_path", "line_no", "machine_time", "utc_time", "code_context"], ) assert plan.interactive.report.passed is False @@ -491,7 +491,7 @@ def main(): module_info.updated_report for module_info in TEST_SUITE_MODULES ], actual_reports=actual_reports, - ignore=["file_path", "line_no", "machine_time", "utc_time"], + ignore=["file_path", "line_no", "machine_time", "utc_time", "code_context"], ) assert plan.interactive.report.passed is True diff --git a/tests/functional/testplan/runnable/interactive/test_interactive.py b/tests/functional/testplan/runnable/interactive/test_interactive.py index 41d1cc51a..a3ac6fb4d 100644 --- a/tests/functional/testplan/runnable/interactive/test_interactive.py +++ b/tests/functional/testplan/runnable/interactive/test_interactive.py @@ -175,6 +175,7 @@ def test_top_level_tests(): "utc_time", "file_path", "line_no", + "code_context", ], )[0] is True @@ -210,6 +211,7 @@ def test_top_level_tests(): "utc_time", "file_path", "line_no", + "code_context", ], )[0] is True @@ -233,6 +235,7 @@ def test_top_level_tests(): "utc_time", "file_path", "line_no", + "code_context", ], )[0] is True