Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #940 from minrk/hist_file_config

Allow setting HistoryManager.hist_file with config.  This fixes trait init in HistoryAccessor, which prevented setting hist_file via config from having any effect.
  • Loading branch information...
commit 602e3c30a1bf42e870537631c0b0e7f0d79788eb 2 parents f76ea91 + 088742b
@fperez fperez authored
Showing with 41 additions and 9 deletions.
  1. +27 −9 IPython/core/history.py
  2. +14 −0 IPython/core/tests/test_history.py
View
36 IPython/core/history.py
@@ -65,8 +65,23 @@ class HistoryAccessor(Configurable):
This is intended for use by standalone history tools. IPython shells use
HistoryManager, below, which is a subclass of this."""
+
# String holding the path to the history file
- hist_file = Unicode(config=True)
+ hist_file = Unicode(config=True,
+ help="""Path to file to use for SQLite history database.
+
+ By default, IPython will put the history database in the IPython profile
+ directory. If you would rather share one history among profiles,
+ you ca set this value in each, so that they are consistent.
+
+ Due to an issue with fcntl, SQLite is known to misbehave on some NFS mounts.
+ If you see IPython hanging, try setting this to something on a local disk,
+ e.g::
+
+ ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
+
+ """)
+
# The SQLite database
if sqlite3:
@@ -74,7 +89,7 @@ class HistoryAccessor(Configurable):
else:
db = Instance(DummyDB)
- def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **traits):
+ def __init__(self, profile='default', hist_file=u'', config=None, **traits):
"""Create a new history accessor.
Parameters
@@ -84,15 +99,17 @@ def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **
hist_file : str
Path to an SQLite history database stored by IPython. If specified,
hist_file overrides profile.
- shell :
- InteractiveShell object, for use by HistoryManager subclass
config :
Config object. hist_file can also be set through this.
"""
# We need a pointer back to the shell for various tasks.
- super(HistoryAccessor, self).__init__(shell=shell, config=config,
- hist_file=hist_file, **traits)
-
+ super(HistoryAccessor, self).__init__(config=config, **traits)
+ # defer setting hist_file from kwarg until after init,
+ # otherwise the default kwarg value would clobber any value
+ # set by config
+ if hist_file:
+ self.hist_file = hist_file
+
if self.hist_file == u'':
# No one has set the hist_file, yet.
self.hist_file = self._get_hist_file_name(profile)
@@ -106,8 +123,9 @@ def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **
self.init_db()
except sqlite3.DatabaseError:
if os.path.isfile(self.hist_file):
- # Try to move the file out of the way.
- newpath = os.path.join(self.shell.profile_dir.location, "hist-corrupt.sqlite")
+ # Try to move the file out of the way
+ base,ext = os.path.splitext(self.hist_file)
+ newpath = base + '-corrupt' + ext
os.rename(self.hist_file, newpath)
print("ERROR! History file wasn't a valid SQLite database.",
"It was moved to %s" % newpath, "and a new file created.")
View
14 IPython/core/tests/test_history.py
@@ -8,12 +8,15 @@
# stdlib
import os
import sys
+import tempfile
import unittest
from datetime import datetime
+
# third party
import nose.tools as nt
# our own packages
+from IPython.config.loader import Config
from IPython.utils.tempdir import TemporaryDirectory
from IPython.core.history import HistoryManager, extract_hist_ranges
from IPython.utils import py3compat
@@ -122,3 +125,14 @@ def test_timestamp_type():
ip = get_ipython()
info = ip.history_manager.get_session_info()
nt.assert_true(isinstance(info[1], datetime))
+
+def test_hist_file_config():
+ cfg = Config()
+ tfile = tempfile.NamedTemporaryFile(delete=False)
+ cfg.HistoryManager.hist_file = tfile.name
+ try:
+ hm = HistoryManager(shell=get_ipython(), config=cfg)
+ nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
+ finally:
+ os.remove(tfile.name)
+
Please sign in to comment.
Something went wrong with that request. Please try again.