Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving non-ascii history #1377

Merged
merged 3 commits into from Feb 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions IPython/core/history.py
Expand Up @@ -15,6 +15,7 @@
# Stdlib imports
import atexit
import datetime
from io import open as io_open
import os
import re
try:
Expand Down Expand Up @@ -808,7 +809,7 @@ def _format_lineno(session, line):
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
Expand Down Expand Up @@ -851,10 +852,10 @@ def _format_lineno(session, line):
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)
Expand Down
9 changes: 8 additions & 1 deletion IPython/core/tests/test_history.py
Expand Up @@ -7,6 +7,7 @@

# stdlib
import os
import shutil
import sys
import tempfile
import unittest
Expand All @@ -31,7 +32,7 @@ def test_history():
hist_file = os.path.join(tmpdir, 'history.sqlite')
try:
ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
hist = ['a=1', 'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)

Expand All @@ -51,6 +52,12 @@ def test_history():
# Check whether specifying a range beyond the end of the current
# session results in an error (gh-804)
ip.magic('%hist 2-500')

# Check that we can write non-ascii characters to a file
ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))

# New session
ip.history_manager.reset()
Expand Down