# Buggy code
def __init__(self, profile="default", hist_file="", config=None, **traits):
    """Create a new history accessor.

    Parameters
    ----------
    profile : str
      The name of the profile from which to open history.
    hist_file : str
      Path to an SQLite history database stored by IPython. If specified,
      hist_file overrides profile.
    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__(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 == "":
        # No one has set the hist_file, yet.
        self.hist_file = self._get_hist_file_name(profile)

    if sqlite3 is None and self.enabled:
        warn("IPython History requires SQLite, your history will not be saved\n")
        self.enabled = False

    if sqlite3 is not None:
        DatabaseError = sqlite3.DatabaseError
    else:
        DatabaseError = Exception

    try:
        self.init_db()
    except DatabaseError:
        if os.path.isfile(self.hist_file):
            # 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.",
            )
            self.init_db()
        else:
            # The hist_file is probably :memory: or something else.
            raise


# Traceback (DatabaseError)
(master)dreamweaver[nbconvert]> ip
Traceback (most recent call last):
File "/home/fperez/usr/bin/ipython", line 7, in <module>
launch_new_instance()
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 388, in launch_new_instance
app.initialize()
File "<string>", line 2, in initialize
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
return method(app, *args, **kwargs)
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 324, in initialize
self.init_shell()
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 340, in init_shell
ipython_dir=self.ipython_dir)
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/config/configurable.py", line 318, in instance
inst = cls(*args, **kwargs)
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/frontend/terminal/interactiveshell.py", line 360, in __init__
user_module=user_module, custom_exceptions=custom_exceptions
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 481, in __init__
self.init_readline()
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 1868, in init_readline
self.refill_readline_hist()
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 1880, in refill_readline_hist
include_latest=True):
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/core/history.py", line 252, in get_tail
(n,), raw=raw, output=output)
File "/home/fperez/usr/lib/python2.7/site-packages/IPython/core/history.py", line 197, in _run_sql
(toget, sqlfrom) + sql, params)
DatabaseError: database disk image is malformed

# Fixed code
def __init__(self, profile="default", hist_file="", config=None, **traits):
    """Create a new history accessor.

    Parameters
    ----------
    profile : str
      The name of the profile from which to open history.
    hist_file : str
      Path to an SQLite history database stored by IPython. If specified,
      hist_file overrides profile.
    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__(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 == "":
        # No one has set the hist_file, yet.
        self.hist_file = self._get_hist_file_name(profile)

    if sqlite3 is None and self.enabled:
        warn("IPython History requires SQLite, your history will not be saved\n")
        self.enabled = False

    self.init_db()