Ticket #8708: trac_8708-jhp.patch

File trac_8708-jhp.patch, 3.5 KB (added by John Palmieri, 11 years ago)

apply to scripts repo

  • sage-doctest

    # HG changeset patch
    # User J. H. Palmieri <palmieri@math.washington.edu>
    # Date 1313447303 25200
    # Node ID d5f6bad01dc850cd2df1554fc221029049c2d2ed
    # Parent  9bfb3d088a9f591eb4d23aa12ec788ac9217b2b7
    #8708: allow sage-doctest to extract docs from both """ and '''.
    
    diff --git a/sage-doctest b/sage-doctest
    a b def change_warning_output(file): 
    452452            tmpfiles.append(os.path.join(SAGE_TESTDIR, name + '.pyc'))
    453453
    454454    # Prefix/suffix for all doctests replacing the starting/ending """
    455     doc_prefix = 'r""">>> set_random_seed(0L)\n\n>>> change_warning_output(sys.stdout)\n\n'
    456     doc_suffix = '\n>>> sig_on_count()\n0\n"""'
     455    doc_prefix_dq = 'r""">>> set_random_seed(0L)\n\n>>> change_warning_output(sys.stdout)\n\n'
     456    doc_prefix_sq = doc_prefix_dq.replace('"""',"'''")
     457    doc_suffix_dq = '\n>>> sig_on_count()\n0\n"""'
     458    doc_suffix_sq = doc_suffix_dq.replace('"""',"'''")
    457459
    458460    n = 0
     461    # Now extract the docstring by using a regular expression search
     462    # with pattern "('''|\"\"\")(.*?)\\1":
     463    #
     464    #    ('''|\"\"\"):  ''' or """, then
     465    #    (.*?):   any text, then
     466    #    \\1:     the same triple quotes as at the beginning.
     467    #
     468    # Remember the parenthesized groups for later -- remember the
     469    # first one so we can match it at the end, and remember the second
     470    # because it contains the actual code to be tested.
     471    #
     472    # The question mark in "(.*?)" means a non-greedy search, so this
     473    # part is as short as possible; thus it won't match everything
     474    # between the first """ and the last """ in the string, but
     475    # instead between the first and the second.  The last argument,
     476    # re.M | re.S, means to allow matches spanning multiple lines
     477    # (re.M), and to allow the "."  in ".*?" match newlines (re.S).
     478    pattern = re.compile("('''|\"\"\")(.*?)\\1", re.M | re.S)
    459479    while True:
    460         i = F.find('"""')
    461         if i == -1: break
    462         name = "example"       
    463         k = F[i+3:].find('"""')
    464         if k == -1: break
    465         j = i+3 + k
     480        m = pattern.search(F)
     481        if not m: # no match
     482            break
     483        # m.groups() will have two entries, one for each set of parentheses:
     484        #   [0] the starting triple quotes,
     485        #   [1] the string between the starting and ending triple quotes.
     486        doc = m.groups()[1]
     487        F = F[m.end():]
     488        if len(doc) == 0:
     489            # Then we're matching a string like """""" or ''''''.
     490            # Skip this match.
     491            continue
    466492        try:
    467             doc = doc_preparse(F[i:j+3])
     493            doc = doc_preparse(doc)
    468494        except SyntaxError:
    469             doc = F[i:j+3]
     495            pass
    470496        if len(doc):
    471             doc = doc_prefix + doc[3:-3] + doc_suffix
    472497            if random_order:
    473498                n_str = pad_zeros(new_index(n),10)
    474499            else:
    475500                n_str = str(n)
     501            name = "example"
    476502            s += "def %s_%s():"%(name,n_str)
    477503            n += 1
    478             s += "\t"+ doc + "\n\n"
    479         F = F[j+3:]
     504            if m.groups()[0].find("'") > -1: # single quotes
     505                s += "\t"+ doc_prefix_sq + doc + doc_suffix_sq + "\n\n"
     506            else:
     507                s += "\t"+ doc_prefix_dq + doc + doc_suffix_dq + "\n\n"
    480508
    481509    if n == 0:
    482510        return  ''
    def change_warning_output(file): 
    484512
    485513    # Allow for "sage:" instead of the traditional Python ">>>".
    486514    s = s.replace("sage:",">>>").replace('_sage"','')
    487    
     515
    488516    return s
    489517
    490518def pythonify_tex(F):