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>')
| 390 | return block |
| 391 | |
| 392 | def parse(self, string, name='<string>'): |
| 393 | """ |
| 394 | Divide the given string into examples and intervening text, |
| 395 | and return them as a list of alternating Examples and strings. |
| 396 | Line numbers for the Examples are 0-based. The optional |
| 397 | argument `name` is a name identifying this string, and is only |
| 398 | used for error messages. |
| 399 | """ |
| 400 | |
| 401 | #print 'Parse string:\n',string # dbg |
| 402 | |
| 403 | string = string.expandtabs() |
| 404 | # If all lines begin with the same indentation, then strip it. |
| 405 | min_indent = self._min_indent(string) |
| 406 | if min_indent > 0: |
| 407 | string = '\n'.join([l[min_indent:] for l in string.split('\n')]) |
| 408 | |
| 409 | output = [] |
| 410 | charno, lineno = 0, 0 |
| 411 | |
| 412 | # We make 'all random' tests by adding the '# random' mark to every |
| 413 | # block of output in the test. |
| 414 | if self._RANDOM_TEST.search(string): |
| 415 | random_marker = '\n# random' |
| 416 | else: |
| 417 | random_marker = '' |
| 418 | |
| 419 | # Whether to convert the input from ipython to python syntax |
| 420 | ip2py = False |
| 421 | # Find all doctest examples in the string. First, try them as Python |
| 422 | # examples, then as IPython ones |
| 423 | terms = list(self._EXAMPLE_RE_PY.finditer(string)) |
| 424 | if terms: |
| 425 | # Normal Python example |
| 426 | #print '-'*70 # dbg |
| 427 | #print 'PyExample, Source:\n',string # dbg |
| 428 | #print '-'*70 # dbg |
| 429 | Example = doctest.Example |
| 430 | else: |
| 431 | # It's an ipython example. Note that IPExamples are run |
| 432 | # in-process, so their syntax must be turned into valid python. |
| 433 | # IPExternalExamples are run out-of-process (via pexpect) so they |
| 434 | # don't need any filtering (a real ipython will be executing them). |
| 435 | terms = list(self._EXAMPLE_RE_IP.finditer(string)) |
| 436 | if self._EXTERNAL_IP.search(string): |
| 437 | #print '-'*70 # dbg |
| 438 | #print 'IPExternalExample, Source:\n',string # dbg |
| 439 | #print '-'*70 # dbg |
| 440 | Example = IPExternalExample |
| 441 | else: |
| 442 | #print '-'*70 # dbg |
| 443 | #print 'IPExample, Source:\n',string # dbg |
| 444 | #print '-'*70 # dbg |
| 445 | Example = IPExample |
| 446 | ip2py = True |
| 447 | |
| 448 | for m in terms: |
| 449 | # Add the pre-example text to `output`. |
nothing calls this directly
no test coverage detected