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

Method _parse_example

Lib/doctest.py:722–764  ·  view source on GitHub ↗

Given a regular expression match from `_EXAMPLE_RE` (`m`), return a pair `(source, want)`, where `source` is the matched example's source code (with prompts and indentation stripped); and `want` is the example's expected output (with indentation stripped).

(self, m, name, lineno)

Source from the content-addressed store, hash-verified

720 if isinstance(x, Example)]
721
722 def _parse_example(self, m, name, lineno):
723 """
724 Given a regular expression match from `_EXAMPLE_RE` (`m`),
725 return a pair `(source, want)`, where `source` is the matched
726 example's source code (with prompts and indentation stripped);
727 and `want` is the example's expected output (with indentation
728 stripped).
729
730 `name` is the string's name, and `lineno` is the line number
731 where the example starts; both are used for error messages.
732 """
733 # Get the example's indentation level.
734 indent = len(m.group('indent'))
735
736 # Divide source into lines; check that they're properly
737 # indented; and then strip their indentation & prompts.
738 source_lines = m.group('source').split('\n')
739 self._check_prompt_blank(source_lines, indent, name, lineno)
740 self._check_prefix(source_lines[1:], ' '*indent + '.', name, lineno)
741 source = '\n'.join([sl[indent+4:] for sl in source_lines])
742
743 # Divide want into lines; check that it's properly indented; and
744 # then strip the indentation. Spaces before the last newline should
745 # be preserved, so plain rstrip() isn't good enough.
746 want = m.group('want')
747 want_lines = want.split('\n')
748 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
749 del want_lines[-1] # forget final newline & spaces after it
750 self._check_prefix(want_lines, ' '*indent, name,
751 lineno + len(source_lines))
752 want = '\n'.join([wl[indent:] for wl in want_lines])
753
754 # If `want` contains a traceback message, then extract it.
755 m = self._EXCEPTION_RE.match(want)
756 if m:
757 exc_msg = m.group('msg')
758 else:
759 exc_msg = None
760
761 # Extract options from the source.
762 options = self._find_options(source, name, lineno)
763
764 return source, options, want, exc_msg
765
766 # This regular expression looks for option directives in the
767 # source code of an example. Option directives are comments

Callers 1

parseMethod · 0.95

Calls 8

_check_prompt_blankMethod · 0.95
_check_prefixMethod · 0.95
_find_optionsMethod · 0.95
lenFunction · 0.85
groupMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected