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

Method _parse_example

IPython/testing/plugin/ipdoctest.py:203–266  ·  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,ip2py=False)

Source from the content-addressed store, hash-verified

201 return output
202
203 def _parse_example(self, m, name, lineno,ip2py=False):
204 """
205 Given a regular expression match from `_EXAMPLE_RE` (`m`),
206 return a pair `(source, want)`, where `source` is the matched
207 example's source code (with prompts and indentation stripped);
208 and `want` is the example's expected output (with indentation
209 stripped).
210
211 `name` is the string's name, and `lineno` is the line number
212 where the example starts; both are used for error messages.
213
214 Optional:
215 `ip2py`: if true, filter the input via IPython to convert the syntax
216 into valid python.
217 """
218
219 # Get the example's indentation level.
220 indent = len(m.group('indent'))
221
222 # Divide source into lines; check that they're properly
223 # indented; and then strip their indentation & prompts.
224 source_lines = m.group('source').split('\n')
225
226 # We're using variable-length input prompts
227 ps1 = m.group('ps1')
228 ps2 = m.group('ps2')
229 ps1_len = len(ps1)
230
231 self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len)
232 if ps2:
233 self._check_prefix(source_lines[1:], ' '*indent + ps2, name, lineno)
234
235 source = '\n'.join([sl[indent+ps1_len+1:] for sl in source_lines])
236
237 if ip2py:
238 # Convert source input from IPython into valid Python syntax
239 source = self.ip2py(source)
240
241 # Divide want into lines; check that it's properly indented; and
242 # then strip the indentation. Spaces before the last newline should
243 # be preserved, so plain rstrip() isn't good enough.
244 want = m.group('want')
245 want_lines = want.split('\n')
246 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
247 del want_lines[-1] # forget final newline & spaces after it
248 self._check_prefix(want_lines, ' '*indent, name,
249 lineno + len(source_lines))
250
251 # Remove ipython output prompt that might be present in the first line
252 want_lines[0] = re.sub(r'Out\[\d+\]: \s*?\n?','',want_lines[0])
253
254 want = '\n'.join([wl[indent:] for wl in want_lines])
255
256 # If `want` contains a traceback message, then extract it.
257 m = self._EXCEPTION_RE.match(want)
258 if m:
259 exc_msg = m.group('msg')
260 else:

Callers 1

parseMethod · 0.95

Calls 4

_check_prompt_blankMethod · 0.95
ip2pyMethod · 0.95
groupMethod · 0.80
matchMethod · 0.80

Tested by

no test coverage detected