From e9de8c72ff94f2e9b3c15a5b8bd5b0965ba24272 2012-04-06 21:34:57 From: Thomas Kluyver Date: 2012-04-06 21:34:57 Subject: [PATCH] Fix for %hist saving non-ascii history to file. Closes gh-1375 --- diff --git a/IPython/core/history.py b/IPython/core/history.py index 9a4a249..896cc97 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -15,6 +15,7 @@ from __future__ import print_function # Stdlib imports import atexit import datetime +from io import open as io_open import os import re try: @@ -808,7 +809,7 @@ def magic_history(self, parameter_s = ''): print('Aborting.') return print("Overwriting file.") - outfile = open(outfname,'w') + outfile = io_open(outfname, 'w', encoding='utf-8') close_at_end = True print_nums = 'n' in opts @@ -851,10 +852,10 @@ def magic_history(self, parameter_s = ''): multiline = "\n" in inline line_sep = '\n' if multiline else ' ' if print_nums: - print('%s:%s' % (_format_lineno(session, lineno).rjust(width), - line_sep), file=outfile, end='') + print(u'%s:%s' % (_format_lineno(session, lineno).rjust(width), + line_sep), file=outfile, end=u'') if pyprompts: - print(">>> ", end="", file=outfile) + print(u">>> ", end=u"", file=outfile) if multiline: inline = "\n... ".join(inline.splitlines()) + "\n..." print(inline, file=outfile)