| 582 | |
| 583 | |
| 584 | def _init_checker_class() -> Type["doctest.OutputChecker"]: |
| 585 | import doctest |
| 586 | import re |
| 587 | |
| 588 | class LiteralsOutputChecker(doctest.OutputChecker): |
| 589 | # Based on doctest_nose_plugin.py from the nltk project |
| 590 | # (https://github.com/nltk/nltk) and on the "numtest" doctest extension |
| 591 | # by Sebastien Boisgerault (https://github.com/boisgera/numtest). |
| 592 | |
| 593 | _unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE) |
| 594 | _bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE) |
| 595 | _number_re = re.compile( |
| 596 | r""" |
| 597 | (?P<number> |
| 598 | (?P<mantissa> |
| 599 | (?P<integer1> [+-]?\d*)\.(?P<fraction>\d+) |
| 600 | | |
| 601 | (?P<integer2> [+-]?\d+)\. |
| 602 | ) |
| 603 | (?: |
| 604 | [Ee] |
| 605 | (?P<exponent1> [+-]?\d+) |
| 606 | )? |
| 607 | | |
| 608 | (?P<integer3> [+-]?\d+) |
| 609 | (?: |
| 610 | [Ee] |
| 611 | (?P<exponent2> [+-]?\d+) |
| 612 | ) |
| 613 | ) |
| 614 | """, |
| 615 | re.VERBOSE, |
| 616 | ) |
| 617 | |
| 618 | def check_output(self, want: str, got: str, optionflags: int) -> bool: |
| 619 | if super().check_output(want, got, optionflags): |
| 620 | return True |
| 621 | |
| 622 | allow_unicode = optionflags & _get_allow_unicode_flag() |
| 623 | allow_bytes = optionflags & _get_allow_bytes_flag() |
| 624 | allow_number = optionflags & _get_number_flag() |
| 625 | |
| 626 | if not allow_unicode and not allow_bytes and not allow_number: |
| 627 | return False |
| 628 | |
| 629 | def remove_prefixes(regex: Pattern[str], txt: str) -> str: |
| 630 | return re.sub(regex, r"\1\2", txt) |
| 631 | |
| 632 | if allow_unicode: |
| 633 | want = remove_prefixes(self._unicode_literal_re, want) |
| 634 | got = remove_prefixes(self._unicode_literal_re, got) |
| 635 | |
| 636 | if allow_bytes: |
| 637 | want = remove_prefixes(self._bytes_literal_re, want) |
| 638 | got = remove_prefixes(self._bytes_literal_re, got) |
| 639 | |
| 640 | if allow_number: |
| 641 | got = self._remove_unwanted_precision(want, got) |