@@ -277,7 +277,7 @@ def smarter_request_reload(desc):
277
277
self .interact = self .status_bar # overwriting what bpython.Repl put there
278
278
# interact is called to interact with the status bar,
279
279
# so we're just using the same object
280
- self ._current_line = '' # line currently being edited, without '>>> '
280
+ self ._current_line = '' # line currently being edited, without ps1 (usually '>>> ')
281
281
self .current_stdouterr_line = '' # current line of output - stdout and stdin go here
282
282
self .display_lines = [] # lines separated whenever logical line
283
283
# length goes over what the terminal width
@@ -586,8 +586,10 @@ def send_current_block_to_external_editor(self, filename=None):
586
586
587
587
def send_session_to_external_editor (self , filename = None ):
588
588
for_editor = '### current bpython session - file will be reevaluated, ### lines will not be run\n ' .encode ('utf8' )
589
- for_editor += ('\n ' .join (line [4 :] if line [:4 ] in ('... ' , '>>> ' ) else '### ' + line
590
- for line in self .getstdout ().split ('\n ' )).encode ('utf8' ))
589
+ for_editor += ('\n ' .join (line [len (self .ps1 ):] if line .startswith (self .ps1 ) else
590
+ (line [len (self .ps2 ):] if line .startswith (self .ps2 ) else
591
+ '### ' + line )
592
+ for line in self .getstdout ().split ('\n ' )).encode ('utf8' ))
591
593
text = self .send_to_external_editor (for_editor )
592
594
lines = text .split ('\n ' )
593
595
self .history = [line for line in lines if line [:4 ] != '### ' ]
@@ -627,7 +629,7 @@ def add_normal_character(self, char):
627
629
char +
628
630
self .current_line [self .cursor_offset :])
629
631
self .cursor_offset += 1
630
- if self .config .cli_trim_prompts and self .current_line .startswith (">>> " ):
632
+ if self .config .cli_trim_prompts and self .current_line .startswith (self . ps1 ):
631
633
self .current_line = self .current_line [4 :]
632
634
self .cursor_offset = max (0 , self .cursor_offset - 4 )
633
635
@@ -673,11 +675,8 @@ def push(self, line, insert_into_history=True):
673
675
code_to_run = '\n ' .join (self .buffer )
674
676
675
677
logger .debug ('running %r in interpreter' , self .buffer )
676
- try :
677
- c = bool (code .compile_command ('\n ' .join (self .buffer )))
678
- self .saved_predicted_parse_error = False
679
- except (ValueError , SyntaxError , OverflowError ):
680
- c = self .saved_predicted_parse_error = True
678
+ c , code_will_parse = self .buffer_finished_will_parse ()
679
+ self .saved_predicted_parse_error = not code_will_parse
681
680
if c :
682
681
logger .debug ('finished - buffer cleared' )
683
682
self .display_lines .extend (self .display_buffer_lines )
@@ -688,6 +687,21 @@ def push(self, line, insert_into_history=True):
688
687
self .coderunner .load_code (code_to_run )
689
688
self .run_code_and_maybe_finish ()
690
689
690
+ def buffer_finished_will_parse (self ):
691
+ """Returns a tuple of whether the buffer could be complete and whether it will parse
692
+
693
+ True, True means code block is finished and no predicted parse error
694
+ True, False means code block is finished because a parse error is predicted
695
+ False, True means code block is unfinished
696
+ False, False isn't possible - an predicted error makes code block done"""
697
+ try :
698
+ finished = bool (code .compile_command ('\n ' .join (self .buffer )))
699
+ code_will_parse = True
700
+ except (ValueError , SyntaxError , OverflowError ):
701
+ finished = True
702
+ code_will_parse = False
703
+ return finished , code_will_parse
704
+
691
705
def run_code_and_maybe_finish (self , for_code = None ):
692
706
r = self .coderunner .run_code (for_code = for_code )
693
707
if r :
@@ -1072,7 +1086,7 @@ def reevaluate(self, insert_into_history=False):
1072
1086
self .display_lines = []
1073
1087
1074
1088
if not self .weak_rewind :
1075
- self .interp = code . InteractiveInterpreter ()
1089
+ self .interp = self . interp . __class__ ()
1076
1090
self .coderunner .interp = self .interp
1077
1091
1078
1092
self .buffer = []
0 commit comments