Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

History memory #312

Merged
merged 1 commit into from

3 participants

@rkern

Use a :memory: DB for history during testing.

@fperez fperez merged commit 62118c7 into ipython:master
@fperez
Owner

Awesome, many thanks! The .core test time went from ~30 secs to 2 secs on my machine, so this really is significant. Great job, as always.

@takluyver takluyver commented on the diff
IPython/core/history.py
@@ -47,15 +47,22 @@ class HistoryManager(Configurable):
input_hist_raw = List([""])
# A list of directories visited during session
dir_hist = List()
+ def _dir_hist_default(self):
@takluyver Owner

Does this method get automatically picked up by traits to set the default?

@rkern
rkern added a note

Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@takluyver
Owner

This looks fine to me, but I've been too slow with reviewing it, and it's already merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 25, 2011
  1. @rkern

    BUG: use a :memory: history DB for testing. Refactor the initializati…

    rkern authored
    …on of the HistoryManager to support this.
This page is out of date. Refresh to see the latest.
View
68 IPython/core/history.py
@@ -47,15 +47,22 @@ class HistoryManager(Configurable):
input_hist_raw = List([""])
# A list of directories visited during session
dir_hist = List()
+ def _dir_hist_default(self):
@takluyver Owner

Does this method get automatically picked up by traits to set the default?

@rkern
rkern added a note

Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ try:
+ return [os.getcwd()]
+ except OSError:
+ return []
+
# A dict of output history, keyed with ints from the shell's
# execution count. If there are several outputs from one command,
# only the last one is stored.
output_hist = Dict()
# Contains all outputs, in lists of reprs.
- output_hist_reprs = Instance(defaultdict)
-
+ output_hist_reprs = Instance(defaultdict, args=(list,))
+
# String holding the path to the history file
- hist_file = Unicode()
+ hist_file = Unicode(config=True)
+
# The SQLite database
db = Instance(sqlite3.Connection)
# The number of the current session in the history database
@@ -73,47 +80,48 @@ class HistoryManager(Configurable):
# Variables used to store the three last inputs from the user. On each new
# history update, we populate the user's namespace with these, shifted as
# necessary.
- _i00, _i, _ii, _iii = '','','',''
+ _i00 = Unicode(u'')
+ _i = Unicode(u'')
+ _ii = Unicode(u'')
+ _iii = Unicode(u'')
# A set with all forms of the exit command, so that we don't store them in
# the history (it's annoying to rewind the first entry and land on an exit
# call).
- _exit_commands = None
-
- def __init__(self, shell, config=None):
+ _exit_commands = Instance(set, args=(['Quit', 'quit', 'Exit', 'exit',
+ '%Quit', '%quit', '%Exit', '%exit'],))
+
+ def __init__(self, shell, config=None, **traits):
"""Create a new history manager associated with a shell instance.
"""
# We need a pointer back to the shell for various tasks.
- super(HistoryManager, self).__init__(shell=shell, config=config)
+ super(HistoryManager, self).__init__(shell=shell, config=config,
+ **traits)
- # list of visited directories
- try:
- self.dir_hist = [os.getcwd()]
- except OSError:
- self.dir_hist = []
+ if self.hist_file == u'':
+ # No one has set the hist_file, yet.
+ if shell.profile:
+ histfname = 'history-%s' % shell.profile
+ else:
+ histfname = 'history'
+ self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
- # Now the history file
- if shell.profile:
- histfname = 'history-%s' % shell.profile
- else:
- histfname = 'history'
- self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
try:
self.init_db()
except sqlite3.DatabaseError:
- newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
- 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.")
- self.init_db()
-
+ if os.path.isfile(self.hist_file):
+ # Try to move the file out of the way.
+ newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
+ 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.")
+ self.init_db()
+ else:
+ # The hist_file is probably :memory: or something else.
+ raise
+
self.new_session()
-
- self._i00, self._i, self._ii, self._iii = '','','',''
- self.output_hist_reprs = defaultdict(list)
- self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
- '%quit', '%Exit', '%exit'])
def init_db(self):
"""Connect to the database, and create tables if necessary."""
View
12 IPython/core/tests/test_history.py
@@ -23,18 +23,10 @@ def setUp():
def test_history():
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
- #tmpdir = '/software/temp'
- histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite'))
- # Ensure that we restore the history management that we mess with in
- # this test doesn't affect the IPython instance used by the test suite
- # beyond this test.
+ # Make a new :memory: DB.
hist_manager_ori = ip.history_manager
try:
- ip.history_manager = HistoryManager(shell=ip)
- ip.history_manager.hist_file = histfile
- ip.history_manager.init_db() # Has to be called after changing file
- ip.history_manager.reset()
- print 'test',histfile
+ ip.history_manager = HistoryManager(shell=ip, hist_file=':memory:')
hist = ['a=1', '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)
View
1  IPython/testing/tools.py
@@ -167,6 +167,7 @@ def default_config():
config.TerminalInteractiveShell.colors = 'NoColor'
config.TerminalTerminalInteractiveShell.term_title = False,
config.TerminalInteractiveShell.autocall = 0
+ config.HistoryManager.hist_file = u':memory:'
return config
Something went wrong with that request. Please try again.