# Buggy code
def __init__(self, store_dir):
    super(LevelDbStore, self).__init__()

    self._store_dir = store_dir
    self._pending_torrents = {}
    self._logger = logging.getLogger(self.__class__.__name__)
    # This is done to work around LevelDB's inability to deal with non-ascii paths on windows.
    try:
        db_path = (
            store_dir.decode("windows-1252") if sys.platform == "win32" else store_dir
        )
        self._db = self._leveldb(db_path)
    except ValueError:
        # This can happen on Windows when the state dir and Tribler installation are on different disks.
        # In this case, hope for the best by using the full path.
        self._db = self._leveldb(store_dir)
    except Exception as exc:
        # We cannot simply catch LevelDBError since that class might not be available on some systems.
        if use_leveldb and isinstance(exc, LevelDBError):
            # The database might be corrupt, start with a fresh one
            self._logger.error("Corrupt LevelDB store detected; recreating database")
            rmtree(self._store_dir)
            os.makedirs(self._store_dir)
            self._db = self._leveldb(os.path.relpath(store_dir, os.getcwdu()))

    self._writeback_lc = self.register_task("flush cache ", LoopingCall(self.flush))
    self._writeback_lc.clock = self._reactor
    self._writeback_lc.start(WRITEBACK_PERIOD)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 99, in on_read_data
RuntimeError: Unhandled Error
Traceback (most recent call last):
File "lib\multiprocessing\process.py", line 114, in run

File "TriblerGUI\core_manager.py", line 63, in start_tribler_core

File "lib\site-packages\twisted\internet\base.py", line 1243, in run

File "lib\site-packages\twisted\internet\base.py", line 1252, in mainLoop

--- <exception caught here> ---
File "lib\site-packages\twisted\internet\base.py", line 851, in runUntilCurrent

File "Tribler\Core\TFTP\handler.py", line 233, in data_came_in

File "Tribler\Core\TFTP\handler.py", line 301, in _handle_new_request

File "Tribler\Core\TFTP\handler.py", line 345, in _load_torrent

File "lib\_abcoll.py", line 382, in get

File "Tribler\Core\leveldbstore.py", line 86, in __getitem__

exceptions.AttributeError: 'LevelDbStore' object has no attribute '_db'

# Fixed code
def __init__(self, store_dir):
    super(LevelDbStore, self).__init__()

    self._store_dir = store_dir
    self._pending_torrents = {}
    self._logger = logging.getLogger(self.__class__.__name__)
    # This is done to work around LevelDB's inability to deal with non-ascii paths on windows.
    try:
        db_path = (
            store_dir.decode("windows-1252") if sys.platform == "win32" else store_dir
        )
        self._db = self._leveldb(db_path)
    except ValueError:
        # This can happen on Windows when the state dir and Tribler installation are on different disks.
        # In this case, hope for the best by using the full path.
        self._db = self._leveldb(store_dir)
    except Exception as exc:
        # We cannot simply catch LevelDBError since that class might not be available on some systems.
        if use_leveldb and isinstance(exc, LevelDBError):
            # The database might be corrupt, start with a fresh one
            self._logger.error("Corrupt LevelDB store detected; recreating database")
            rmtree(self._store_dir)
            os.makedirs(self._store_dir)
            self._db = self._leveldb(os.path.relpath(store_dir, os.getcwdu()))
        else:  # If something else goes wrong, we throw the exception again
            raise

    self._writeback_lc = self.register_task("flush cache ", LoopingCall(self.flush))
    self._writeback_lc.clock = self._reactor
    self._writeback_lc.start(WRITEBACK_PERIOD)