Ticket #14557: 14557_doctest_atexit.patch

File 14557_doctest_atexit.patch, 2.8 KB (added by jdemeyer, 9 years ago)
  • sage/doctest/forker.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1368173925 -7200
    # Node ID 57ba56933fdad72d290f72c2188263af089caa31
    # Parent  75a1095ab5b4f6bcb7c8467bb37256d4b1841ae3
    Add support for atexit functions to the doctesting framework
    
    diff --git a/sage/doctest/forker.py b/sage/doctest/forker.py
    a b  
    20432043            sage: ntests >= 200 or ntests
    20442044            True
    20452045        """
     2046        # Store all existing atexit() functions and clear them,
     2047        # so we know that all newly registered functions come from
     2048        # doctests.
     2049        import atexit
     2050        saved_exithandlers = atexit._exithandlers
     2051        atexit._exithandlers = []
     2052
    20462053        result = None
    20472054        try:
    20482055            file = self.source.path
     
    20782085            results.optionals = extras['optionals']
    20792086            # We subtract 1 to remove the sig_on_count() tests
    20802087            result = (sum([max(0,len(test.examples) - 1) for test in doctests]), results)
     2088
     2089            # multiprocessing.Process instances don't run exit
     2090            # functions, so we run the functions added by doctests
     2091            # now manually and restore the old exit functions.
     2092            atexit._run_exitfuncs()
     2093            atexit._exithandlers = saved_exithandlers
    20812094        except BaseException:
    20822095            exc_info = sys.exc_info()
    20832096            tb = "".join(traceback.format_exception(*exc_info))
  • sage/doctest/test.py

    diff --git a/sage/doctest/test.py b/sage/doctest/test.py
    a b  
    341341    ...
    342342    ValueError: invalid optional tag 'bad-option'
    343343    1
     344
     345Test ``atexit`` support in the doctesting framework::
     346
     347    sage: F = tmp_filename()
     348    sage: os.path.isfile(F)
     349    True
     350    sage: from copy import deepcopy
     351    sage: kwds2 = deepcopy(kwds)
     352    sage: kwds2['env']['DOCTEST_DELETE_FILE'] = F
     353    sage: subprocess.call(["sage", "-t", "atexit.rst"], **kwds2)  # long time
     354    Running doctests...
     355    Doctesting 1 file.
     356    sage -t atexit.rst
     357        [3 tests, ... s]
     358    ----------------------------------------------------------------------
     359    All tests passed!
     360    ----------------------------------------------------------------------
     361    ...
     362    0
     363    sage: os.path.isfile(F)  # long time
     364    False
     365    sage: try:
     366    ....:     os.unlink(F)
     367    ....: except OSError:
     368    ....:     pass
    344369"""
  • new file sage/doctest/tests/atexit.rst

    diff --git a/sage/doctest/tests/atexit.rst b/sage/doctest/tests/atexit.rst
    new file mode 100644
    - +  
     1Register an atexit function to remove the file given by the
     2``DOCTEST_DELETE_FILE`` environment variable::
     3
     4    sage: import atexit
     5    sage: fn = os.environ['DOCTEST_DELETE_FILE']
     6    sage: atexit.register(os.unlink, fn)
     7    <built-in function unlink>