##// END OF EJS Templates
BUG: use a :memory: history DB for testing. Refactor the initialization of the HistoryManager to support this.
Robert Kern -
Show More
@@ -47,15 +47,22 b' class HistoryManager(Configurable):'
47 47 input_hist_raw = List([""])
48 48 # A list of directories visited during session
49 49 dir_hist = List()
50 def _dir_hist_default(self):
51 try:
52 return [os.getcwd()]
53 except OSError:
54 return []
55
50 56 # A dict of output history, keyed with ints from the shell's
51 57 # execution count. If there are several outputs from one command,
52 58 # only the last one is stored.
53 59 output_hist = Dict()
54 60 # Contains all outputs, in lists of reprs.
55 output_hist_reprs = Instance(defaultdict)
56
61 output_hist_reprs = Instance(defaultdict, args=(list,))
62
57 63 # String holding the path to the history file
58 hist_file = Unicode()
64 hist_file = Unicode(config=True)
65
59 66 # The SQLite database
60 67 db = Instance(sqlite3.Connection)
61 68 # The number of the current session in the history database
@@ -73,47 +80,48 b' class HistoryManager(Configurable):'
73 80 # Variables used to store the three last inputs from the user. On each new
74 81 # history update, we populate the user's namespace with these, shifted as
75 82 # necessary.
76 _i00, _i, _ii, _iii = '','','',''
83 _i00 = Unicode(u'')
84 _i = Unicode(u'')
85 _ii = Unicode(u'')
86 _iii = Unicode(u'')
77 87
78 88 # A set with all forms of the exit command, so that we don't store them in
79 89 # the history (it's annoying to rewind the first entry and land on an exit
80 90 # call).
81 _exit_commands = None
82
83 def __init__(self, shell, config=None):
91 _exit_commands = Instance(set, args=(['Quit', 'quit', 'Exit', 'exit',
92 '%Quit', '%quit', '%Exit', '%exit'],))
93
94 def __init__(self, shell, config=None, **traits):
84 95 """Create a new history manager associated with a shell instance.
85 96 """
86 97 # We need a pointer back to the shell for various tasks.
87 super(HistoryManager, self).__init__(shell=shell, config=config)
98 super(HistoryManager, self).__init__(shell=shell, config=config,
99 **traits)
88 100
89 # list of visited directories
90 try:
91 self.dir_hist = [os.getcwd()]
92 except OSError:
93 self.dir_hist = []
101 if self.hist_file == u'':
102 # No one has set the hist_file, yet.
103 if shell.profile:
104 histfname = 'history-%s' % shell.profile
105 else:
106 histfname = 'history'
107 self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
94 108
95 # Now the history file
96 if shell.profile:
97 histfname = 'history-%s' % shell.profile
98 else:
99 histfname = 'history'
100 self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
101 109 try:
102 110 self.init_db()
103 111 except sqlite3.DatabaseError:
104 newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
105 os.rename(self.hist_file, newpath)
106 print("ERROR! History file wasn't a valid SQLite database.",
107 "It was moved to %s" % newpath, "and a new file created.")
108 self.init_db()
109
112 if os.path.isfile(self.hist_file):
113 # Try to move the file out of the way.
114 newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
115 os.rename(self.hist_file, newpath)
116 print("ERROR! History file wasn't a valid SQLite database.",
117 "It was moved to %s" % newpath, "and a new file created.")
118 self.init_db()
119 else:
120 # The hist_file is probably :memory: or something else.
121 raise
122
110 123 self.new_session()
111
112 self._i00, self._i, self._ii, self._iii = '','','',''
113 self.output_hist_reprs = defaultdict(list)
114 124
115 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
116 '%quit', '%Exit', '%exit'])
117 125
118 126 def init_db(self):
119 127 """Connect to the database, and create tables if necessary."""
@@ -23,18 +23,10 b' def setUp():'
23 23 def test_history():
24 24 ip = get_ipython()
25 25 with TemporaryDirectory() as tmpdir:
26 #tmpdir = '/software/temp'
27 histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite'))
28 # Ensure that we restore the history management that we mess with in
29 # this test doesn't affect the IPython instance used by the test suite
30 # beyond this test.
26 # Make a new :memory: DB.
31 27 hist_manager_ori = ip.history_manager
32 28 try:
33 ip.history_manager = HistoryManager(shell=ip)
34 ip.history_manager.hist_file = histfile
35 ip.history_manager.init_db() # Has to be called after changing file
36 ip.history_manager.reset()
37 print 'test',histfile
29 ip.history_manager = HistoryManager(shell=ip, hist_file=':memory:')
38 30 hist = ['a=1', 'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
39 31 for i, h in enumerate(hist, start=1):
40 32 ip.history_manager.store_inputs(i, h)
@@ -167,6 +167,7 b' def default_config():'
167 167 config.TerminalInteractiveShell.colors = 'NoColor'
168 168 config.TerminalTerminalInteractiveShell.term_title = False,
169 169 config.TerminalInteractiveShell.autocall = 0
170 config.HistoryManager.hist_file = u':memory:'
170 171 return config
171 172
172 173
General Comments 0
You need to be logged in to leave comments. Login now