code: def catch_corrupt_db(f, self, *a, **kw):
      """A decorator which wraps HistoryAccessor method calls to catch errors from
      a corrupt SQLite database, move the old database out of the way, and create
      a new one.
  
      We avoid clobbering larger databases because this may be triggered due to filesystem issues,
      not just a corrupt file.
      """
      try:
          return f(self, *a, **kw)
      except (DatabaseError, OperationalError) as e:
          self._corrupt_db_counter += 1
          self.log.error(
              "Failed to open SQLite history %s (%s).",
              self.hist_file,
              e,
          )
          if self.hist_file != ":memory:":
              if self._corrupt_db_counter > self._corrupt_db_limit:
                  self.hist_file = ":memory:"
                  self.log.error(
                      "Failed to load history too many times, history will not be saved."
                  )
              elif os.path.isfile(self.hist_file):
                  # move the file out of the way
                  base, ext = os.path.splitext(self.hist_file)
                  size = os.stat(self.hist_file).st_size
                  if size >= _SAVE_DB_SIZE:
                      # if there's significant content, avoid clobbering
                      now = (
                          datetime.datetime.now()
                          .isoformat()
                          .replace(":", ".")
                      )
                      newpath = base + "-corrupt-" + now + ext
                      # don't clobber previous corrupt backups
                      for i in range(100):
                          if not os.path.isfile(newpath):
                              break
                          else:
                              newpath = (
                                  base
                                  + "-corrupt-"
                                  + now
                                  + ("-%i" % i)
                                  + ext
                              )
                  else:
                      # not much content, possibly empty; don't worry about clobbering
                      # maybe we should just delete it?
                      newpath = base + "-corrupt" + ext
                  os.rename(self.hist_file, newpath)
                  self.log.error(
                      "History file was moved to %s and a new file created.",
                      newpath,
                  )
              self.init_db()
              return []
          else:
              # Failed with :memory:, something serious is wrong
              raise
  
imports: from __future__ import print_function
  import atexit
  import datetime
  import os
  import re
  import threading
  from traitlets.config.configurable import LoggingConfigurable
  from decorator import decorator
  from IPython.utils.decorators import undoc
  from IPython.paths import locate_profile
  from IPython.utils import py3compat
  from traitlets import Any, Bool, Dict, Instance, Integer, List, Unicode, TraitError, default, observe
  from warnings import warn
label_desc: Use image node_id 2 for calling a global function with example usage: catch_corrupt_db(f, ) and returns: f, list