diff --git a/CHANGELOG b/CHANGELOG index 05a7816..1d38b2d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -19,6 +19,10 @@ encoding. Bohdan Vlasyuk sent me a patch that fixes a problem with the above patch from Mark if sys.__stdout__.encoding didn't exist. +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. + v0.7.1 ====== Added support for a history file, defaults to ~/.pythonhist and 100 lines but diff --git a/bpython/cli.py b/bpython/cli.py index 81975c5..72020b7 100644 --- a/bpython/cli.py +++ b/bpython/cli.py @@ -688,6 +688,19 @@ class Repl(object): return self.stdout_hist + '\n' + def formatforfile(self, s): + """Format the stdout buffer to something suitable for writing to disk, + i.e. without >>> and ... at input lines and with "# OUT: " prepended to + output lines.""" + + def process(): + for line in s.split('\n'): + if line.startswith('>>>') or line.startswith('...'): + yield line[4:] + elif line.rstrip(): + yield "# OUT: %s" % (line,) + return "\n".join(process()) + def write2file(self): """Prompt for a filename and write the current contents of the stdout buffer to disk.""" @@ -697,7 +710,7 @@ class Repl(object): if fn.startswith('~'): fn = os.path.expanduser(fn) - s = self.getstdout() + s = self.formatforfile(self.getstdout()) try: f = open(fn, 'w')