# Buggy code
def set_byte_priority(self, byteranges, priority):
    pieces = []
    torrent_info = get_info_from_handle(self.handle)
    if not torrent_info:
        self._logger.info(
            "LibtorrentDownloadImpl: could not get info from download handle"
        )

    for fileindex, bytes_begin, bytes_end in byteranges:
        if fileindex >= 0:
            # Ensure the we remain within the file's boundaries
            file_entry = torrent_info.file_at(fileindex)
            bytes_begin = (
                min(file_entry.size, bytes_begin)
                if bytes_begin >= 0
                else file_entry.size + (bytes_begin + 1)
            )
            bytes_end = (
                min(file_entry.size, bytes_end)
                if bytes_end >= 0
                else file_entry.size + (bytes_end + 1)
            )

            startpiece = torrent_info.map_file(fileindex, bytes_begin, 0).piece
            endpiece = torrent_info.map_file(fileindex, bytes_end, 0).piece + 1
            startpiece = max(startpiece, 0)
            endpiece = min(endpiece, torrent_info.num_pieces())

            pieces += range(startpiece, endpiece)
        else:
            self._logger.info(
                "LibtorrentDownloadImpl: could not set priority for incorrect fileindex"
            )

    if pieces:
        pieces = list(set(pieces))
        self.set_piece_priority(pieces, priority)


# Traceback (RuntimeError)
ERROR   1552279832.86                  __init__:1601 (root)  Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 136, in on_read_data
RuntimeError: [Failure instance: Traceback: <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'file_at'
lib\site-packages\twisted\internet\base.py:825:runUntilCurrent
Tribler\Core\Libtorrent\LibtorrentDownloadImpl.py:236:schedule_create_engine
Tribler\Core\Libtorrent\LibtorrentDownloadImpl.py:326:network_create_engine_wrapper
lib\site-packages\twisted\internet\defer.py:306:addCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:649:_runCallbacks
Tribler\Core\Libtorrent\LibtorrentDownloadImpl.py:313:on_torrent_added
Tribler\Core\Libtorrent\LibtorrentDownloadImpl.py:345:set_vod_mode
Tribler\Core\Libtorrent\__init__.py:17:invoke_func
Tribler\Core\Libtorrent\LibtorrentDownloadImpl.py:460:set_byte_priority
]

# Fixed code
def set_byte_priority(self, byteranges, priority):
    pieces = []
    torrent_info = get_info_from_handle(self.handle)
    if not torrent_info:
        self._logger.info(
            "LibtorrentDownloadImpl: could not get info from download handle"
        )

    for fileindex, bytes_begin, bytes_end in byteranges:
        if fileindex >= 0 and torrent_info:
            # Ensure the we remain within the file's boundaries
            file_entry = torrent_info.file_at(fileindex)
            bytes_begin = (
                min(file_entry.size, bytes_begin)
                if bytes_begin >= 0
                else file_entry.size + (bytes_begin + 1)
            )
            bytes_end = (
                min(file_entry.size, bytes_end)
                if bytes_end >= 0
                else file_entry.size + (bytes_end + 1)
            )

            startpiece = torrent_info.map_file(fileindex, bytes_begin, 0).piece
            endpiece = torrent_info.map_file(fileindex, bytes_end, 0).piece + 1
            startpiece = max(startpiece, 0)
            endpiece = min(endpiece, torrent_info.num_pieces())

            pieces += range(startpiece, endpiece)
        else:
            self._logger.info(
                "LibtorrentDownloadImpl: could not set priority for incorrect fileindex"
            )

    if pieces:
        pieces = list(set(pieces))
        self.set_piece_priority(pieces, priority)