MCPcopy Index your code
hub / github.com/python/cpython / parse

Method parse

Lib/doctest.py:651–688  ·  view source on GitHub ↗

Divide the given string into examples and intervening text, and return them as a list of alternating Examples and strings. Line numbers for the Examples are 0-based. The optional argument `name` is a name identifying this string, and is only used for error m

(self, string, name='<string>')

Source from the content-addressed store, hash-verified

649 _IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$').match
650
651 def parse(self, string, name='<string>'):
652 """
653 Divide the given string into examples and intervening text,
654 and return them as a list of alternating Examples and strings.
655 Line numbers for the Examples are 0-based. The optional
656 argument `name` is a name identifying this string, and is only
657 used for error messages.
658 """
659 string = string.expandtabs()
660 # If all lines begin with the same indentation, then strip it.
661 min_indent = self._min_indent(string)
662 if min_indent > 0:
663 string = '\n'.join([l[min_indent:] for l in string.split('\n')])
664
665 output = []
666 charno, lineno = 0, 0
667 # Find all doctest examples in the string:
668 for m in self._EXAMPLE_RE.finditer(string):
669 # Add the pre-example text to `output`.
670 output.append(string[charno:m.start()])
671 # Update lineno (lines before this example)
672 lineno += string.count('\n', charno, m.start())
673 # Extract info from the regexp match.
674 (source, options, want, exc_msg) = \
675 self._parse_example(m, name, lineno)
676 # Create an Example, and add it to the list.
677 if not self._IS_BLANK_OR_COMMENT(source):
678 output.append( Example(source, want, exc_msg,
679 lineno=lineno,
680 indent=min_indent+len(m.group('indent')),
681 options=options) )
682 # Update lineno (lines inside this example)
683 lineno += string.count('\n', m.start(), m.end())
684 # Update charno.
685 charno = m.end()
686 # Add any remaining post-example text to `output`.
687 output.append(string[charno:])
688 return output
689
690 def get_doctest(self, string, globs, name, filename, lineno):
691 """

Callers 2

get_examplesMethod · 0.95
script_from_examplesFunction · 0.45

Calls 11

_min_indentMethod · 0.95
_parse_exampleMethod · 0.95
ExampleClass · 0.70
expandtabsMethod · 0.45
joinMethod · 0.45
splitMethod · 0.45
appendMethod · 0.45
startMethod · 0.45
countMethod · 0.45
groupMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected