##// END OF EJS Templates
Use AST parser to find python multilines with function handling
Skipper Seabold -
Show More
@@ -455,14 +455,16 b' class EmbeddedSphinxShell(object):'
455 455 output = []
456 456 savefig = False # keep up with this to clear figure
457 457 multiline = False # to handle line continuation
458 multiline_start = None
458 459 fmtin = self.promptin
459 460
461 ct = 0
462
460 463 for lineno, line in enumerate(content):
461 464
462 465 line_stripped = line.strip()
463
464 466 if not len(line):
465 output.append(line) # preserve empty lines in output
467 output.append(line)
466 468 continue
467 469
468 470 # handle decorators
@@ -477,50 +479,43 b' class EmbeddedSphinxShell(object):'
477 479 output.extend([line])
478 480 continue
479 481
480 # deal with multilines
481 if not multiline: # not currently on a multiline
482
483 if line_stripped.endswith('\\'): # now we are
482 # deal with lines checking for multiline
483 continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2))
484 if not multiline:
485 modified = u"%s %s" % (fmtin % ct, line_stripped)
486 output.append(modified)
487 ct += 1
488 try:
489 ast.parse(line_stripped)
490 output.append(u'')
491 except Exception: # on a multiline
484 492 multiline = True
485 cont_len = len(str(lineno)) + 2
486 line_to_process = line.strip('\\')
487 output.extend([u"%s %s" % (fmtin%lineno,line)])
488 continue
489 else: # no we're still not
490 line_to_process = line.strip('\\')
491 else: # we are currently on a multiline
492 line_to_process += line.strip('\\')
493 if line_stripped.endswith('\\'): # and we still are
494 continuation = '.' * cont_len
495 output.extend([(u' %s: '+line_stripped) % continuation])
496 continue
497 # else go ahead and run this multiline then carry on
498
499 # get output of line
500 self.process_input_line(unicode(line_to_process.strip()),
501 store_history=False)
502 out_line = self.cout.getvalue()
503 self.clear_cout()
504
505 # clear current figure if plotted
506 if savefig:
493 multiline_start = lineno
494 if line_stripped.startswith('def '):
495 is_function = True
496 else: # still on a multiline
497 modified = u'%s %s' % (continuation, line)
498 output.append(modified)
499 try:
500 mod = ast.parse(
501 '\n'.join(content[multiline_start:lineno+1]))
502 if isinstance(mod.body[0], ast.FunctionDef):
503 # check to see if we have the whole function
504 for element in mod.body[0].body:
505 if isinstance(element, ast.Return):
506 multiline = False
507 else:
508 output.append(u'')
509 multiline = False
510 except Exception:
511 pass
512
513 if savefig: # clear figure if plotted
507 514 self.ensure_pyplot()
508 515 self.process_input_line('plt.clf()', store_history=False)
509 516 self.clear_cout()
510 517 savefig = False
511 518
512 # line numbers don't actually matter, they're replaced later
513 if not multiline:
514 in_line = u"%s %s" % (fmtin%lineno,line)
515
516 output.extend([in_line])
517 else:
518 output.extend([(u' %s: '+line_stripped) % continuation])
519 multiline = False
520 if len(out_line):
521 output.extend([out_line])
522 output.extend([u''])
523
524 519 return output
525 520
526 521 class IpythonDirective(Directive):
General Comments 0
You need to be logged in to leave comments. Login now