MCPcopy Index your code
hub / github.com/RustPython/RustPython / DocTestParser

Class DocTestParser

Lib/doctest.py:609–837  ·  view source on GitHub ↗

A class used to parse strings containing doctest examples.

Source from the content-addressed store, hash-verified

607######################################################################
608
609class DocTestParser:
610 """
611 A class used to parse strings containing doctest examples.
612 """
613 # This regular expression is used to find doctest examples in a
614 # string. It defines three groups: `source` is the source code
615 # (including leading indentation and prompts); `indent` is the
616 # indentation of the first (PS1) line of the source code; and
617 # `want` is the expected output (including leading indentation).
618 _EXAMPLE_RE = re.compile(r'''
619 # Source consists of a PS1 line followed by zero or more PS2 lines.
620 (?P<source>
621 (?:^(?P<indent> [ ]*) >>> .*) # PS1 line
622 (?:\n [ ]* \.\.\. .*)*) # PS2 lines
623 \n?
624 # Want consists of any non-blank lines that do not start with PS1.
625 (?P<want> (?:(?![ ]*$) # Not a blank line
626 (?![ ]*>>>) # Not a line starting with PS1
627 .+$\n? # But any other line
628 )*)
629 ''&#x27;, re.MULTILINE | re.VERBOSE)
630
631 # A regular expression for handling `want` strings that contain
632 # expected exceptions. It divides `want` into three pieces:
633 # - the traceback header line (`hdr`)
634 # - the traceback stack (`stack`)
635 # - the exception message (`msg`), as generated by
636 # traceback.format_exception_only()
637 # `msg` may have multiple lines. We assume/require that the
638 # exception message is the first non-indented line starting with a word
639 # character following the traceback header line.
640 _EXCEPTION_RE = re.compile(r"""
641 # Grab the traceback header. Different versions of Python have
642 # said different things on the first traceback line.
643 ^(?P<hdr> Traceback\ \(
644 (?: most\ recent\ call\ last
645 | innermost\ last
646 ) \) :
647 )
648 \s* $ # toss trailing whitespace on the header.
649 (?P<stack> .*?) # don't blink: absorb stuff until...
650 ^ (?P<msg> \w+ .*) # a line *starts* with alphanum.
651 """, re.VERBOSE | re.MULTILINE | re.DOTALL)
652
653 # A callable returning a true value iff its argument is a blank line
654 # or contains a single comment.
655 _IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$').match
656
657 def parse(self, string, name='<string>'):
658 """
659 Divide the given string into examples and intervening text,
660 and return them as a list of alternating Examples and strings.
661 Line numbers for the Examples are 0-based. The optional
662 argument `name` is a name identifying this string, and is only
663 used for error messages.
664 """
665 string = string.expandtabs()
666 # If all lines begin with the same indentation, then strip it.

Callers 4

__init__Method · 0.85
testfileFunction · 0.85
DocFileTestFunction · 0.85
script_from_examplesFunction · 0.85

Calls 1

compileMethod · 0.45

Tested by 4

__init__Method · 0.68
testfileFunction · 0.68
DocFileTestFunction · 0.68
script_from_examplesFunction · 0.68