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