# Buggy code
def calculateCategory(self, torrent_dict, display_name):
    # torrent_dict is the  dict of
    # a torrent file
    # return value: list of category the torrent belongs to

    files_list = []
    try:
        # the multi-files mode
        for ifiles in torrent_dict["info"]["files"]:
            files_list.append(
                (ifiles["path"][-1], ifiles["length"] / float(self.__size_change))
            )
    except KeyError:
        # single mode
        files_list.append(
            (
                torrent_dict["info"]["name"],
                torrent_dict["info"]["length"] / float(self.__size_change),
            )
        )

    tracker = torrent_dict.get("announce")
    if not tracker:
        tracker = torrent_dict.get("announce-list", [[""]])[0][0]

    comment = torrent_dict.get("comment")
    return self.calculateCategoryNonDict(files_list, display_name, tracker, comment)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 130, in on_read_data
RuntimeError: Unhandled Error
Traceback (most recent call last):
File "run_tribler.py", line 84, in <module>

File "run_tribler.py", line 76, 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 878, in runUntilCurrent

File "Tribler\dispersy\util.py", line 150, in wrapper

File "Tribler\Core\TFTP\handler.py", line 194, in _process_callbacks

File "Tribler\Core\TFTP\handler.py", line 263, in <lambda>

File "Tribler\pyipv8\ipv8\util.py", line 14, in helper

File "Tribler\pyipv8\ipv8\util.py", line 26, in blockingCallFromThread

File "Tribler\Core\RemoteTorrentHandler.py", line 600, in _on_download_successful

File "Tribler\pyipv8\ipv8\util.py", line 14, in helper

File "Tribler\pyipv8\ipv8\util.py", line 26, in blockingCallFromThread

File "Tribler\Core\RemoteTorrentHandler.py", line 167, in save_torrent

File "Tribler\Core\CacheDB\SqliteCacheDBHandler.py", line 313, in addExternalTorrent

File "Tribler\Core\CacheDB\SqliteCacheDBHandler.py", line 416, in _addTorrentToDB

File "Tribler\Core\CacheDB\SqliteCacheDBHandler.py", line 397, in _get_database_dict

File "Tribler\Core\Category\Category.py", line 70, in calculateCategory

exceptions.IndexError: list index out of range

# Fixed code
def calculateCategory(self, torrent_dict, display_name):
    """
    Calculate the category for a given torrent_dict of a torrent file.
    :return a list of categories this torrent belongs to.
    """
    files_list = []
    try:
        # the multi-files mode
        for ifiles in torrent_dict["info"]["files"]:
            files_list.append(
                (ifiles["path"][-1], ifiles["length"] / float(self.__size_change))
            )
    except KeyError:
        # single mode
        files_list.append(
            (
                torrent_dict["info"]["name"],
                torrent_dict["info"]["length"] / float(self.__size_change),
            )
        )

    tracker = torrent_dict.get("announce")
    if not tracker:
        announce_list = torrent_dict.get("announce-list", [[""]])
        if announce_list and announce_list[0]:
            tracker = announce_list[0][0]

    comment = torrent_dict.get("comment")
    return self.calculateCategoryNonDict(files_list, display_name, tracker, comment)