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