Skip to content

Commit cd01afd

Browse files
thomasballingersebastinas
authored andcommitted
Restore original save behavior for cli and urwid
1 parent d632b51 commit cd01afd

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,8 @@ def push(self, line, insert_into_history=True):
11641164
11651165
If the interpreter successfully runs the code, clear the buffer
11661166
"""
1167+
# Note that push() overrides its parent without calling it, unlike
1168+
# urwid and cli which implement custom behavior and call repl.Repl.push
11671169
if self.paste_mode:
11681170
self.saved_indent = 0
11691171
else:

bpython/repl.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -832,15 +832,31 @@ def get_session_formatted_for_file(self):
832832
i.e. without >>> and ... at input lines and with "# OUT: " prepended to
833833
output lines and "### " prepended to current line"""
834834

835-
def process():
836-
for line, lineType in self.all_logical_lines:
837-
if lineType == LineTypeTranslator.INPUT:
838-
yield line
839-
elif line.rstrip():
840-
yield "# OUT: %s" % line
841-
yield "### %s" % self.current_line
842-
843-
return "\n".join(process())
835+
if hasattr(self, 'all_logical_lines'):
836+
# Curtsies
837+
838+
def process():
839+
for line, lineType in self.all_logical_lines:
840+
if lineType == LineTypeTranslator.INPUT:
841+
yield line
842+
elif line.rstrip():
843+
yield "# OUT: %s" % line
844+
yield "### %s" % self.current_line
845+
846+
return "\n".join(process())
847+
848+
else: # cli and Urwid
849+
session_output = self.getstdout()
850+
851+
def process():
852+
for line in session_output.split("\n"):
853+
if line.startswith(self.ps1):
854+
yield line[len(self.ps1) :]
855+
elif line.startswith(self.ps2):
856+
yield line[len(self.ps2) :]
857+
elif line.rstrip():
858+
yield "# OUT: %s" % (line,)
859+
return '\n'.join(process())
844860

845861
def write2file(self):
846862
"""Prompt for a filename and write the current contents of the stdout
@@ -952,6 +968,7 @@ def do_pastebin(self, s):
952968
def push(self, s, insert_into_history=True):
953969
"""Push a line of code onto the buffer so it can process it all
954970
at once when a code block ends"""
971+
# This push method is used by cli and urwid, but not curtsies
955972
s = s.rstrip("\n")
956973
self.buffer.append(s)
957974

0 commit comments

Comments
 (0)