Ticket #14290: trac_14290_ansi_escapes_indication.patch

File trac_14290_ansi_escapes_indication.patch, 2.6 KB (added by vbraun, 9 years ago)

Updated patch

  • sage/doctest/parsing.py

    # HG changeset patch
    # User Volker Braun <vbraun@stp.dias.ie>
    # Date 1364338386 -3600
    # Node ID 86968d241737de4bea2b68ffc5ae0c0f7c31a656
    # Parent  809286ab516ede21c45f34f3696da756329a2c77
    Mention in got<->want diff that there are ansi escape sequences
    
    diff --git a/sage/doctest/parsing.py b/sage/doctest/parsing.py
    a b  
    3232tolerance_pattern = re.compile(r'\b((?:abs(?:olute)?)|(?:rel(?:ative)?))? *?tol(?:erance)?\b( +[0-9.e+-]+)?')
    3333backslash_replacer = re.compile(r"""(\s*)sage:(.*)\\\ *
    3434\ *(((\.){4}:)|((\.){3}))?\ *""")
     35ansi_escape_sequence = re.compile(r'(\x1b[@-Z\\-~]|\x1b\[.*?[@-~]|\x9b.*?[@-~])')
    3536
    3637def parse_optional_tags(string):
    3738    """
     
    572573        sage: OC.check_output(ex.want, 'x + 0.8935153492877', optflag)
    573574        False
    574575    """
     576    def human_readable_escape_sequences(self, string):
     577        """
     578        Make ANSI escape sequences human readable.
     579   
     580        EXAMPLES::
     581
     582            sage: print 'This ist \x1b[1mbold\x1b[0m text'
     583            This ist <CSI-1m>bold<CSI-0m> text
     584
     585        TESTS::
     586       
     587            sage: from sage.doctest.parsing import SageOutputChecker
     588            sage: OC = SageOutputChecker()
     589            sage: teststr = '-'.join([
     590            ...       'bold\x1b[1m',
     591            ...       'newlinemode\x9b20h',
     592            ...       'red\x1b[31m',
     593            ...       'oscmd\x1ba'])
     594            sage: OC.human_readable_escape_sequences(teststr)
     595            'bold<CSI-1m>-newlinemode<CSI-20h>-red<CSI-31m>-oscmd<ESC-a>'
     596        """
     597        def human_readable(match):
     598            ansi_escape = match.group(1)
     599            assert len(ansi_escape) >= 2
     600            if len(ansi_escape) == 2:
     601                return '<ESC-'+ansi_escape[1]+'>'
     602            else:
     603                return '<CSI-'+ansi_escape.lstrip('\x1b[\x9b')+'>'
     604        return ansi_escape_sequence.subn(human_readable, string)[0]
     605
    575606    def check_output(self, want, got, optionflags):
    576607        """
    577608        Checks to see if the output matches the desired output.
     
    667698            sage: print "1.000009"   # abs tol 1e-5
    668699            1.0
    669700        """
     701        got = self.human_readable_escape_sequences(got)
    670702        if isinstance(want, MarkedOutput):
    671703            if want.random:
    672704                return True
     
    810842            Tolerance exceeded: infinity > 1e-01
    811843
    812844        """
     845        got = self.human_readable_escape_sequences(got)
    813846        want = example.want
    814847        diff = doctest.OutputChecker.output_difference(self, example, got, optionflags)
    815848        if isinstance(want, MarkedOutput) and (want.tol or want.abs_tol or want.rel_tol):