Ticket #8708: trac_8708-jhp.v2.patch

File trac_8708-jhp.v2.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 f5a8c6d204e969b861cb5839ece6050671087b0c
    # Parent  9ba7162d80e9be5880c493e1fed479db17862e3f
    #8708: allow sage-doctest to extract docs from both """ and '''.
    
    diff --git a/sage-doctest b/sage-doctest
    a b def check_with_tolerance(expected, actua 
    536537            tmpfiles.append(target_base + ".pyc") # compiled version of it
    537538
    538539    # Prefix/suffix for all doctests replacing the starting/ending """
    539     doc_prefix = 'r""">>> set_random_seed(0L)\n\n>>> change_warning_output(sys.stdout)\n\n'
    540     doc_suffix = '\n>>> sig_on_count()\n0\n"""'
     540    doc_prefix_dq = 'r""">>> set_random_seed(0L)\n\n>>> change_warning_output(sys.stdout)\n\n'
     541    doc_prefix_sq = doc_prefix_dq.replace('"""',"'''")
     542    doc_suffix_dq = '\n>>> sig_on_count()\n0\n"""'
     543    doc_suffix_sq = doc_suffix_dq.replace('"""',"'''")
    541544
    542545    n = 0
     546    # Now extract the docstring by using a regular expression search
     547    # with pattern "('''|\"\"\")(.*?)\\1":
     548    #
     549    #    ('''|\"\"\"):  ''' or """, then
     550    #    (.*?):   any text, then
     551    #    \\1:     the same triple quotes as at the beginning.
     552    #
     553    # Remember the parenthesized groups for later -- remember the
     554    # first one so we can match it at the end, and remember the second
     555    # because it contains the actual code to be tested.
     556    #
     557    # The question mark in "(.*?)" means a non-greedy search, so this
     558    # part is as short as possible; thus it won't match everything
     559    # between the first """ and the last """ in the string, but
     560    # instead between the first and the second.  The last argument,
     561    # re.M | re.S, means to allow matches spanning multiple lines
     562    # (re.M), and to allow the "."  in ".*?" match newlines (re.S).
     563    pattern = re.compile("('''|\"\"\")(.*?)\\1", re.M | re.S)
    543564    while True:
    544         i = F.find('"""')
    545         if i == -1: break
    546         k = F[i+3:].find('"""')
    547         if k == -1: break
    548         j = i+3 + k
     565        m = pattern.search(F)
     566        if not m: # no match
     567            break
     568        # m.groups() will have two entries, one for each set of parentheses:
     569        #   [0] the starting triple quotes,
     570        #   [1] the string between the starting and ending triple quotes.
     571        doc = m.groups()[1]
     572        F = F[m.end():]
     573        if len(doc) == 0:
     574            # Then we're matching a string like """""" or ''''''.
     575            # Skip this match.
     576            continue
    549577        try:
    550             doc = doc_preparse(F[i:j+3])
     578            doc = doc_preparse(doc)
    551579        except SyntaxError:
    552             doc = F[i:j+3]
     580            pass
    553581        if len(doc):
    554             doc = doc_prefix + doc[3:-3] + doc_suffix
    555582            if random_order:
    556583                n_str = pad_zeros(new_index(n),10)
    557584            else:
    558585                n_str = str(n)
    559             s += "def example_%s():" % n_str
     586            name = "example"
     587            s += "def %s_%s():" % (name, n_str)
    560588            n += 1
    561             s += "\t"+ doc + "\n\n"
    562         F = F[j+3:]
     589            if m.groups()[0].find("'") > -1: # single quotes
     590                s += "\t"+ doc_prefix_sq + doc + doc_suffix_sq + "\n\n"
     591            else:
     592                s += "\t"+ doc_prefix_dq + doc + doc_suffix_dq + "\n\n"
    563593
    564594    if n == 0:
    565595        return  ''
    def check_with_tolerance(expected, actua 
    567597
    568598    # Allow for "sage:" instead of the traditional Python ">>>".
    569599    s = s.replace("sage:",">>>").replace('_sage"','')
    570    
     600
    571601    return s
    572602
    573603def pythonify_tex(F):