# 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
|
|
32 | 32 | tolerance_pattern = re.compile(r'\b((?:abs(?:olute)?)|(?:rel(?:ative)?))? *?tol(?:erance)?\b( +[0-9.e+-]+)?') |
33 | 33 | backslash_replacer = re.compile(r"""(\s*)sage:(.*)\\\ * |
34 | 34 | \ *(((\.){4}:)|((\.){3}))?\ *""") |
| 35 | ansi_escape_sequence = re.compile(r'(\x1b[@-Z\\-~]|\x1b\[.*?[@-~]|\x9b.*?[@-~])') |
35 | 36 | |
36 | 37 | def parse_optional_tags(string): |
37 | 38 | """ |
… |
… |
|
572 | 573 | sage: OC.check_output(ex.want, 'x + 0.8935153492877', optflag) |
573 | 574 | False |
574 | 575 | """ |
| 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 | |
575 | 606 | def check_output(self, want, got, optionflags): |
576 | 607 | """ |
577 | 608 | Checks to see if the output matches the desired output. |
… |
… |
|
667 | 698 | sage: print "1.000009" # abs tol 1e-5 |
668 | 699 | 1.0 |
669 | 700 | """ |
| 701 | got = self.human_readable_escape_sequences(got) |
670 | 702 | if isinstance(want, MarkedOutput): |
671 | 703 | if want.random: |
672 | 704 | return True |
… |
… |
|
810 | 842 | Tolerance exceeded: infinity > 1e-01 |
811 | 843 | |
812 | 844 | """ |
| 845 | got = self.human_readable_escape_sequences(got) |
813 | 846 | want = example.want |
814 | 847 | diff = doctest.OutputChecker.output_difference(self, example, got, optionflags) |
815 | 848 | if isinstance(want, MarkedOutput) and (want.tol or want.abs_tol or want.rel_tol): |