MCPcopy Create free account
hub / github.com/ipython/ipython / _parse_example

Method _parse_example

IPython/testing/plugin/ipdoctest.py:479–542  ·  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

477 return output
478
479 def _parse_example(self, m, name, lineno,ip2py=False):
480 """
481 Given a regular expression match from `_EXAMPLE_RE` (`m`),
482 return a pair `(source, want)`, where `source` is the matched
483 example's source code (with prompts and indentation stripped);
484 and `want` is the example's expected output (with indentation
485 stripped).
486
487 `name` is the string's name, and `lineno` is the line number
488 where the example starts; both are used for error messages.
489
490 Optional:
491 `ip2py`: if true, filter the input via IPython to convert the syntax
492 into valid python.
493 """
494
495 # Get the example's indentation level.
496 indent = len(m.group('indent'))
497
498 # Divide source into lines; check that they're properly
499 # indented; and then strip their indentation & prompts.
500 source_lines = m.group('source').split('\n')
501
502 # We're using variable-length input prompts
503 ps1 = m.group('ps1')
504 ps2 = m.group('ps2')
505 ps1_len = len(ps1)
506
507 self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len)
508 if ps2:
509 self._check_prefix(source_lines[1:], ' '*indent + ps2, name, lineno)
510
511 source = '\n'.join([sl[indent+ps1_len+1:] for sl in source_lines])
512
513 if ip2py:
514 # Convert source input from IPython into valid Python syntax
515 source = self.ip2py(source)
516
517 # Divide want into lines; check that it's properly indented; and
518 # then strip the indentation. Spaces before the last newline should
519 # be preserved, so plain rstrip() isn't good enough.
520 want = m.group('want')
521 want_lines = want.split('\n')
522 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
523 del want_lines[-1] # forget final newline & spaces after it
524 self._check_prefix(want_lines, ' '*indent, name,
525 lineno + len(source_lines))
526
527 # Remove ipython output prompt that might be present in the first line
528 want_lines[0] = re.sub(r'Out\[\d+\]: \s*?\n?','',want_lines[0])
529
530 want = '\n'.join([wl[indent:] for wl in want_lines])
531
532 # If `want` contains a traceback message, then extract it.
533 m = self._EXCEPTION_RE.match(want)
534 if m:
535 exc_msg = m.group('msg')
536 else:

Callers 1

parseMethod · 0.95

Calls 3

_check_prompt_blankMethod · 0.95
ip2pyMethod · 0.95
groupMethod · 0.80

Tested by

no test coverage detected