# Buggy code
def addExternalTorrentNoDef(
    self, infohash, name, files, trackers, timestamp, extra_info={}
):
    if not self.hasTorrent(infohash):
        metainfo = {"info": {}, "encoding": "utf_8"}
        metainfo["info"]["name"] = name.encode("utf_8")
        metainfo["info"]["piece length"] = -1
        metainfo["info"]["pieces"] = ""

        if len(files) > 1:
            files_as_dict = []
            for filename, file_length in files:
                filename = filename.encode("utf_8")
                files_as_dict.append({"path": [filename], "length": file_length})
            metainfo["info"]["files"] = files_as_dict

        elif len(files) == 1:
            metainfo["info"]["length"] = files[0][1]
        else:
            return

        if len(trackers) > 0:
            metainfo["announce"] = trackers[0]
            metainfo["announce-list"] = [list(trackers)]
        else:
            metainfo["nodes"] = []

        metainfo["creation date"] = timestamp

        try:
            torrentdef = TorrentDef.load_from_dict(metainfo)
            torrentdef.infohash = infohash

            torrent_id = self._addTorrentToDB(torrentdef, extra_info)
            if self._rtorrent_handler:
                self._rtorrent_handler.notify_possible_torrent_infohash(infohash)

            insert_files = [
                (torrent_id, unicode(path), length) for path, length in files
            ]
            sql_insert_files = "INSERT OR IGNORE INTO TorrentFiles (torrent_id, path, length) VALUES (?,?,?)"
            self._db.executemany(sql_insert_files, insert_files)
        except:
            self._logger.error(
                "Could not create a TorrentDef instance %r %r %r %r %r %r",
                infohash,
                timestamp,
                name,
                files,
                trackers,
                extra_info,
            )
            print_exc()


# Traceback (exceptions.AttributeError)
Exception in thread Twisted (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 754, in run
File "/home/quinten/Documents/tribler/Tribler/Test/twisted_thread.py", line 53, in <lambda>
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1194, in run
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1208, in mainLoop
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'msg'

# Fixed code
def addExternalTorrentNoDef(
    self, infohash, name, files, trackers, timestamp, extra_info={}
):
    if not self.hasTorrent(infohash):
        metainfo = {"info": {}, "encoding": "utf_8"}
        metainfo["info"]["name"] = name.encode("utf_8")
        metainfo["info"]["piece length"] = -1
        metainfo["info"]["pieces"] = ""

        if len(files) > 1:
            files_as_dict = []
            for filename, file_length in files:
                filename = filename.encode("utf_8")
                files_as_dict.append({"path": [filename], "length": file_length})
            metainfo["info"]["files"] = files_as_dict

        elif len(files) == 1:
            metainfo["info"]["length"] = files[0][1]
        else:
            return

        if len(trackers) > 0:
            metainfo["announce"] = trackers[0]
            metainfo["announce-list"] = [list(trackers)]
        else:
            metainfo["nodes"] = []

        metainfo["creation date"] = timestamp

        try:
            torrentdef = TorrentDef.load_from_dict(metainfo)
            torrentdef.infohash = infohash

            torrent_id = self._addTorrentToDB(torrentdef, extra_info)
            if self._rtorrent_handler:
                self._rtorrent_handler.notify_possible_torrent_infohash(infohash)

            insert_files = [
                (torrent_id, unicode(path), length) for path, length in files
            ]
            sql_insert_files = "INSERT OR IGNORE INTO TorrentFiles (torrent_id, path, length) VALUES (?,?,?)"
            self._db.executemany(sql_insert_files, insert_files)
        except:
            self._logger.exception(
                "Could not create a TorrentDef instance %r %r %r %r %r %r",
                infohash,
                timestamp,
                name,
                files,
                trackers,
                extra_info,
            )