@@ -277,7 +277,7 @@ def smarter_request_reload(desc):
277277 self .interact = self .status_bar # overwriting what bpython.Repl put there
278278 # interact is called to interact with the status bar,
279279 # 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 '>>> ')
281281 self .current_stdouterr_line = '' # current line of output - stdout and stdin go here
282282 self .display_lines = [] # lines separated whenever logical line
283283 # length goes over what the terminal width
@@ -586,8 +586,10 @@ def send_current_block_to_external_editor(self, filename=None):
586586
587587 def send_session_to_external_editor (self , filename = None ):
588588 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' ))
591593 text = self .send_to_external_editor (for_editor )
592594 lines = text .split ('\n ' )
593595 self .history = [line for line in lines if line [:4 ] != '### ' ]
@@ -627,7 +629,7 @@ def add_normal_character(self, char):
627629 char +
628630 self .current_line [self .cursor_offset :])
629631 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 ):
631633 self .current_line = self .current_line [4 :]
632634 self .cursor_offset = max (0 , self .cursor_offset - 4 )
633635
@@ -673,11 +675,8 @@ def push(self, line, insert_into_history=True):
673675 code_to_run = '\n ' .join (self .buffer )
674676
675677 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
681680 if c :
682681 logger .debug ('finished - buffer cleared' )
683682 self .display_lines .extend (self .display_buffer_lines )
@@ -688,6 +687,21 @@ def push(self, line, insert_into_history=True):
688687 self .coderunner .load_code (code_to_run )
689688 self .run_code_and_maybe_finish ()
690689
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+
691705 def run_code_and_maybe_finish (self , for_code = None ):
692706 r = self .coderunner .run_code (for_code = for_code )
693707 if r :
@@ -1072,7 +1086,7 @@ def reevaluate(self, insert_into_history=False):
10721086 self .display_lines = []
10731087
10741088 if not self .weak_rewind :
1075- self .interp = code . InteractiveInterpreter ()
1089+ self .interp = self . interp . __class__ ()
10761090 self .coderunner .interp = self .interp
10771091
10781092 self .buffer = []
0 commit comments