Skip to content
This repository

allow setting HistoryManager.hist_file with config #940

Merged
merged 2 commits into from over 2 years ago

2 participants

Min RK Fernando Perez
Min RK
Owner

fixes trait init in HistoryAccessor, which prevented setting hist_file via config from having any effect.

Also remove dependency on having a shell object in HistoryAccessor.__init__

Associated test included.

as described in #882

Min RK allow setting HistoryManager.hist_file with config
fixes improper init in HistoryAccessor, preventing setting `hist_file` via config from having any effect.

Also remove dependency on having a shell object in `HistoryAccessor.__init__`

Associated test included.
af429a3
IPython/core/tests/test_history.py
@@ -122,3 +125,10 @@ def test_timestamp_type():
122 125
     ip = get_ipython()
123 126
     info = ip.history_manager.get_session_info()
124 127
     nt.assert_true(isinstance(info[1], datetime))
  128
+
  129
+def test_hist_file_config():
  130
+    cfg = Config()
  131
+    cfg.HistoryManager.hist_file = tempfile.mktemp()
1
Fernando Perez Owner
fperez added a note

mktemp is deprecated. For this particular test, I suggest instead creating the temp file with NamedTemporaryFile:

tfile = tempfile.NamedTemporaryFile(delete=False)
cfg.HistoryManager.hist_file = tfile.name
...

try:
  nt.assert...
finally:
  os.unlink(tfile.name)

That's the right pattern for these guys. In Pyhton 3 they've been turned into context managers, which is really what you want.

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

Other than the above fixes to the test, the code looks good to me. I'll merge it once this small stuff is fixed; thanks!

Min RK
Owner

suggested changes made and pushed

Fernando Perez fperez merged commit 602e3c3 into from
Fernando Perez fperez closed this
Fernando Perez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Oct 28, 2011
Min RK allow setting HistoryManager.hist_file with config
fixes improper init in HistoryAccessor, preventing setting `hist_file` via config from having any effect.

Also remove dependency on having a shell object in `HistoryAccessor.__init__`

Associated test included.
af429a3
Oct 30, 2011
Min RK use NamedTemporaryFile in hist_file_config test 088742b
This page is out of date. Refresh to see the latest.
36  IPython/core/history.py
@@ -65,8 +65,23 @@ class HistoryAccessor(Configurable):
65 65
     
66 66
     This is intended for use by standalone history tools. IPython shells use
67 67
     HistoryManager, below, which is a subclass of this."""
  68
+
68 69
     # String holding the path to the history file
69  
-    hist_file = Unicode(config=True)
  70
+    hist_file = Unicode(config=True,
  71
+        help="""Path to file to use for SQLite history database.
  72
+        
  73
+        By default, IPython will put the history database in the IPython profile
  74
+        directory.  If you would rather share one history among profiles,
  75
+        you ca set this value in each, so that they are consistent.
  76
+        
  77
+        Due to an issue with fcntl, SQLite is known to misbehave on some NFS mounts.
  78
+        If you see IPython hanging, try setting this to something on a local disk,
  79
+        e.g::
  80
+        
  81
+            ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
  82
+        
  83
+        """)
  84
+
70 85
 
71 86
     # The SQLite database
72 87
     if sqlite3:
@@ -74,7 +89,7 @@ class HistoryAccessor(Configurable):
74 89
     else:
75 90
         db = Instance(DummyDB)
76 91
     
77  
-    def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **traits):
  92
+    def __init__(self, profile='default', hist_file=u'', config=None, **traits):
78 93
         """Create a new history accessor.
79 94
         
80 95
         Parameters
@@ -84,15 +99,17 @@ def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **
84 99
         hist_file : str
85 100
           Path to an SQLite history database stored by IPython. If specified,
86 101
           hist_file overrides profile.
87  
-        shell :
88  
-          InteractiveShell object, for use by HistoryManager subclass
89 102
         config :
90 103
           Config object. hist_file can also be set through this.
91 104
         """
92 105
         # We need a pointer back to the shell for various tasks.
93  
-        super(HistoryAccessor, self).__init__(shell=shell, config=config,
94  
-                                              hist_file=hist_file, **traits)
95  
-
  106
+        super(HistoryAccessor, self).__init__(config=config, **traits)
  107
+        # defer setting hist_file from kwarg until after init,
  108
+        # otherwise the default kwarg value would clobber any value
  109
+        # set by config
  110
+        if hist_file:
  111
+            self.hist_file = hist_file
  112
+        
96 113
         if self.hist_file == u'':
97 114
             # No one has set the hist_file, yet.
98 115
             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, **
106 123
             self.init_db()
107 124
         except sqlite3.DatabaseError:
108 125
             if os.path.isfile(self.hist_file):
109  
-                # Try to move the file out of the way.
110  
-                newpath = os.path.join(self.shell.profile_dir.location, "hist-corrupt.sqlite")
  126
+                # Try to move the file out of the way
  127
+                base,ext = os.path.splitext(self.hist_file)
  128
+                newpath = base + '-corrupt' + ext
111 129
                 os.rename(self.hist_file, newpath)
112 130
                 print("ERROR! History file wasn't a valid SQLite database.",
113 131
                 "It was moved to %s" % newpath, "and a new file created.")
14  IPython/core/tests/test_history.py
@@ -8,12 +8,15 @@
8 8
 # stdlib
9 9
 import os
10 10
 import sys
  11
+import tempfile
11 12
 import unittest
12 13
 from datetime import datetime
  14
+
13 15
 # third party
14 16
 import nose.tools as nt
15 17
 
16 18
 # our own packages
  19
+from IPython.config.loader import Config
17 20
 from IPython.utils.tempdir import TemporaryDirectory
18 21
 from IPython.core.history import HistoryManager, extract_hist_ranges
19 22
 from IPython.utils import py3compat
@@ -122,3 +125,14 @@ def test_timestamp_type():
122 125
     ip = get_ipython()
123 126
     info = ip.history_manager.get_session_info()
124 127
     nt.assert_true(isinstance(info[1], datetime))
  128
+
  129
+def test_hist_file_config():
  130
+    cfg = Config()
  131
+    tfile = tempfile.NamedTemporaryFile(delete=False)
  132
+    cfg.HistoryManager.hist_file = tfile.name
  133
+    try:
  134
+        hm = HistoryManager(shell=get_ipython(), config=cfg)
  135
+        nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
  136
+    finally:
  137
+        os.remove(tfile.name)
  138
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.