# Buggy code
def _get_torrent_hash(result):
    """
    Gets the torrent hash from either the magnet or torrent file content
    params: :result: an instance of the searchResult class
    """
    if result.url.startswith("magnet"):
        result.hash = re.findall(r"urn:btih:([\w]{32,40})", result.url)[0]
        if len(result.hash) == 32:
            result.hash = b16encode(b32decode(result.hash)).lower()
    else:
        if not result.content:
            logger.exception("Torrent without content")
            raise Exception("Torrent without content")

        try:
            torrent_bdecode: Union[Iterable, Dict] = bencodepy.decode(result.content)
        except (bencodepy.DecodingError, Exception) as error:
            logger.exception("Unable to bdecode torrent")
            logger.info("Error is: {0}".format(error))
            logger.info("Torrent bencoded data: {0!r}".format(result.content))
            raise

        try:
            info = torrent_bdecode[b"info"]
        except Exception:
            logger.exception("Unable to find info field in torrent")
            logger.info("Torrent bencoded data: {0!r}".format(result.content))
            raise

        try:
            result.hash = sha1(bencodepy.encode(info)).hexdigest()
            logger.debug("Result Hash is {0}".format(result.hash))
        except (bencodepy.EncodingError, Exception) as error:
            logger.exception("Unable to bencode torrent info")
            logger.info("Error is: {0}".format(error))
            logger.info("Torrent bencoded data: {0!r}".format(result.content))
            raise

    return result


# Traceback (AttributeError)
AA AttributeError: module 'bencodepy' has no attribute 'DecodingError'
AA except bencodepy.DecodingError as e:
AA File "/opt/sickchill/sickchill/providers/torrent/TorrentProvider.py", line 109, in _verify_download
AA if self._verify_download(downloaded_filename):
AA File "/opt/sickchill/sickchill/providers/GenericProvider.py", line 105, in download_result
AA newResult = resProvider.download_result(result)
AA File "/opt/sickchill/sickchill/oldbeard/search.py", line 34, in _downloadResult
AA dlResult = _downloadResult(result)
AA File "/opt/sickchill/sickchill/oldbeard/search.py", line 111, in snatchEpisode
AA self.success = search.snatchEpisode(search_result[0])
AA File "/opt/sickchill/sickchill/oldbeard/search_queue.py", line 174, in run
AA Traceback (most recent call last):
AA During handling of the above exception, another exception occurred:
AA AttributeError: module 'bencodepy' has no attribute 'decode_from_file'
AA bencodepy.decode_from_file(file_name)
AA File "/opt/sickchill/sickchill/providers/torrent/TorrentProvider.py", line 108, in _verify_download

# Fixed code
def _get_torrent_hash(result):
    """
    Gets the torrent hash from either the magnet or torrent file content
    params: :result: an instance of the searchResult class
    """
    if result.url.startswith("magnet"):
        result.hash = re.findall(r"urn:btih:([\w]{32,40})", result.url)[0]
        if len(result.hash) == 32:
            result.hash = b16encode(b32decode(result.hash)).lower()
    else:
        if not result.content:
            logger.exception("Torrent without content")
            raise Exception("Torrent without content")

        try:
            torrent_bdecode: Union[Iterable, Dict] = bencodepy.decode(result.content)
        except (bencodepy.BencodeDecodeError, Exception) as error:
            logger.exception("Unable to bdecode torrent")
            logger.info("Error is: {0}".format(error))
            logger.info("Torrent bencoded data: {0!r}".format(result.content))
            raise

        try:
            info = torrent_bdecode[b"info"]
        except Exception:
            logger.exception("Unable to find info field in torrent")
            logger.info("Torrent bencoded data: {0!r}".format(result.content))
            raise

        try:
            result.hash = sha1(bencodepy.encode(info)).hexdigest()
            logger.debug("Result Hash is {0}".format(result.hash))
        except (bencodepy.Ben, Exception) as error:
            logger.exception("Unable to bencode torrent info")
            logger.info("Error is: {0}".format(error))
            logger.info("Torrent bencoded data: {0!r}".format(result.content))
            raise

    return result