Skip to content

Commit 1dfe0c4

Browse files
committed
Save format changed to be executable code
Save to file now outputs executable code (i.e. without the >>> and ... and with "# OUT: " prepended to all output lines). I never used this feature much but someone asked for this behaviour. committer: Bob Farrell <robertanthonyfarrell@gmail.com>
1 parent 2425738 commit 1dfe0c4

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ encoding.
1919
Bohdan Vlasyuk sent me a patch that fixes a problem with the above patch from
2020
Mark if sys.__stdout__.encoding didn't exist.
2121

22+
Save to file now outputs executable code (i.e. without the >>> and ... and with
23+
"# OUT: " prepended to all output lines). I never used this feature much but
24+
someone asked for this behaviour.
25+
2226
v0.7.1
2327
======
2428
Added support for a history file, defaults to ~/.pythonhist and 100 lines but

bpython/cli.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,19 @@ def getstdout(self):
688688

689689
return self.stdout_hist + '\n'
690690

691+
def formatforfile(self, s):
692+
"""Format the stdout buffer to something suitable for writing to disk,
693+
i.e. without >>> and ... at input lines and with "# OUT: " prepended to
694+
output lines."""
695+
696+
def process():
697+
for line in s.split('\n'):
698+
if line.startswith('>>>') or line.startswith('...'):
699+
yield line[4:]
700+
elif line.rstrip():
701+
yield "# OUT: %s" % (line,)
702+
return "\n".join(process())
703+
691704
def write2file(self):
692705
"""Prompt for a filename and write the current contents of the stdout
693706
buffer to disk."""
@@ -697,7 +710,7 @@ def write2file(self):
697710
if fn.startswith('~'):
698711
fn = os.path.expanduser(fn)
699712

700-
s = self.getstdout()
713+
s = self.formatforfile(self.getstdout())
701714

702715
try:
703716
f = open(fn, 'w')

0 commit comments

Comments
 (0)