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

Method _parse_example

Lib/doctest.py:716–758  ·  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

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

Callers 1

parseMethod · 0.95

Calls 7

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

Tested by

no test coverage detected