Ticket #1276: trac_1276_scripts.patch

File trac_1276_scripts.patch, 3.5 KB (added by gfurnish, 15 years ago)

rebased & fixed scripts repo patch for this.

  • sage-doctest

    # HG changeset patch
    # User Gary Furnish <gfurnish@gfurnish.net>
    # Date 1211668843 21600
    # Node ID 4db48de805ef3999f3e07fdcffb17ad92770c572
    # Parent  d8687692111eba0b3c8439914c82fefe5b42301f
    trac #1276
    
    diff -r d8687692111e -r 4db48de805ef sage-doctest
    a b optional = False 
    1212optional = False
    1313long_time = False
    1414verbose = False
     15time_runs = False
    1516
    1617argv = sys.argv
    1718
    1819import sage.misc.preparser
     20from sage.misc.misc import pad_zeros
    1921
    2022######################################################
    2123# This code is copied from sage.misc.misc for speed:
    os.environ["PYTHONPATH"]=SAGE_ROOT + "/l 
    4244
    4345PYTHON = SAGE_ROOT + "/local/bin/python"
    4446
    45 def test_code():
    46     return """
     47def test_code(file_name):
     48    if not time_runs:
     49        return """
    4750if __name__ ==  '__main__':
    4851    import doctest, sys
    4952    s = doctest.testmod(sys.modules[__name__],
    if __name__ == '__main__': 
    5356                   globs=globals())
    5457    quit_sage(verbose=False)
    5558"""%verbose
     59    else:
     60        #TODO: before computing dochash, only keep actual input (and possibly
     61        # output), stripping all comments from the docstring
     62        return """
     63from sage.misc.timing import DocTestTiming
     64timings = {}
     65
     66def time_run(self, test, *p, **k):
     67    import time, re
     68    from sage.misc.misc import cputime
     69    t = cputime()
     70    starttime = time.time()
     71    self.old_run(test, *p, **k)
     72    cput = cputime(t)
     73    wallt = time.time()-starttime
     74    p = test.name.find("example_")
     75    if p != -1:
     76        num = test.name[p:]
     77        r = re.compile(r"###line (\d+):_sage_")
     78        try:
     79            linenum = r.search(test.docstring).group(1)
     80            dochash = hash(r.sub("###line N:_sage_", test.docstring))
     81            t = DocTestTiming("%s", linenum, num, dochash, cput, wallt)
     82            timings[dochash] = t
     83            if %s:
     84                t.show()
     85        except:
     86            pass
     87
     88if __name__ ==  '__main__':
     89    import doctest, sys
     90    from sage.misc.db import save,load
     91    doctest.DocTestRunner.old_run = doctest.DocTestRunner.run
     92    doctest.DocTestRunner.run = time_run
     93    try:
     94        timings = load("tmp/.doctest/timings.sobj")
     95    except AssertionError:
     96        timings = {}
     97    s = doctest.testmod(sys.modules[__name__],
     98                   optionflags=doctest.NORMALIZE_WHITESPACE
     99                              |doctest.ELLIPSIS,
     100                   verbose=%s,
     101                   globs=globals())
     102    quit_sage(verbose=False)
     103    save(timings, "tmp/.doctest/timings.sobj")
     104"""%(file_name,verbose,verbose)
    56105
    57106ALARM = """
    58107import signal
    def extract_doc(file_name, module): 
    212261##             name = 'example'
    213262        k = F[i+3].find('"""')
    214263        j = i+3 + F[i+3:].find('"""')
    215         s += "def %s_%s():"%(name,n)
     264        s += "def %s_%s():"%(name,pad_zeros(n))
    216265        n += 1
    217266        try:
    218267            doc = doc_preparse(F[i:j+3])
    def extract_doc(file_name, module): 
    229278    #    s += test_code()
    230279    #    s += '    __doctest_cleanup()\n'
    231280    #else:
    232     s += test_code()
     281    s += test_code(file_name)
    233282    s += '    sys.exit(s[0])'
    234283
    235284
    if __name__ == '__main__': 
    423472        massif     = has_opt('massif')
    424473        cachegrind = has_opt('cachegrind')
    425474        omega      = has_opt('omega')
     475        time_runs  = has_opt('time')
    426476        if long_time:
    427477            TIMEOUT = TIMEOUT_LONG
    428478        if gdb or memcheck or massif or cachegrind or omega: