Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 6d4bb9f

Browse files
committed
doctest framework: clear exception
1 parent 71f5ae8 commit 6d4bb9f

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/sage/doctest/forker.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,16 @@ def compiler(example):
673673
raise
674674
except BaseException:
675675
exception = sys.exc_info()
676+
# On Python 2, the exception lives in sys.exc_info() as
677+
# long we are in the same stack frame. To ensure that
678+
# sig_occurred() works correctly, we need to clear the
679+
# exception. This is not an issue on Python 3, where the
680+
# exception is cleared as soon as we are outside of the
681+
# "except" clause.
682+
try:
683+
sys.exc_clear()
684+
except AttributeError:
685+
pass # Python 3
676686
finally:
677687
if self.debugger is not None:
678688
self.debugger.set_continue() # ==== Example Finished ====
@@ -699,8 +709,7 @@ def compiler(example):
699709

700710
# The example raised an exception: check if it was expected.
701711
else:
702-
exc_info = exception
703-
exc_msg = traceback.format_exception_only(*exc_info[:2])[-1]
712+
exc_msg = traceback.format_exception_only(*exception[:2])[-1]
704713

705714
if six.PY3 and example.exc_msg is not None:
706715
# On Python 3 the exception repr often includes the
@@ -709,7 +718,7 @@ def compiler(example):
709718
# normalize Python 3 exceptions to match tests written to
710719
# Python 2
711720
# See https://trac.sagemath.org/ticket/24271
712-
exc_cls = exc_info[0]
721+
exc_cls = exception[0]
713722
exc_name = exc_cls.__name__
714723
if exc_cls.__module__:
715724
exc_fullname = (exc_cls.__module__ + '.' +
@@ -736,7 +745,7 @@ def compiler(example):
736745
break
737746

738747
if not quiet:
739-
got += doctest._exception_traceback(exc_info)
748+
got += doctest._exception_traceback(exception)
740749

741750
# If `example.exc_msg` is None, then we weren't expecting
742751
# an exception.
@@ -770,7 +779,7 @@ def compiler(example):
770779
elif outcome is BOOM:
771780
if not quiet:
772781
self.report_unexpected_exception(out, test, example,
773-
exc_info)
782+
exception)
774783
failures += 1
775784
else:
776785
assert False, ("unknown outcome", outcome)

0 commit comments

Comments
 (0)