Ticket #1276: trac_1276_scripts.patch

File trac_1276_scripts.patch, 3.5 kB (added by gfurnish, 8 months ago)

rebased & fixed scripts repo patch for this.

  • a/sage-doctest

    old new  
    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: 
     
    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__], 
     
    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 
     
    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]) 
     
    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 
     
    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: