# Buggy code
def set_selected_files(self, selected_files=None):
    if not isinstance(self.tdef, TorrentDefNoMetainfo):
        if selected_files is None:
            selected_files = self.get_selected_files()
        else:
            DownloadConfigInterface.set_selected_files(self, selected_files)

        is_multifile = len(self.orig_files) > 1
        commonprefix = os.path.commonprefix(self.orig_files) if is_multifile else ""
        swarmname = commonprefix.partition(os.path.sep)[0]
        unwanteddir = os.path.join(swarmname, ".unwanted")
        unwanteddir_abs = os.path.join(
            self.get_save_path().decode("utf-8"), unwanteddir
        )

        torrent_info = get_info_from_handle(self.handle)
        if not torrent_info or not hasattr(torrent_info, "files"):
            self._logger.error(
                "File info not available for torrent [%s]", self.correctedinfoname
            )
            return

        filepriorities = []
        torrent_storage = torrent_info.files()

        for index, orig_path in enumerate(self.orig_files):
            filename = orig_path[len(swarmname) + 1 :] if swarmname else orig_path

            if filename in selected_files or not selected_files:
                filepriorities.append(1)
                new_path = orig_path
            else:
                filepriorities.append(0)
                new_path = os.path.join(
                    unwanteddir, "%s%d" % (hexlify(self.tdef.get_infohash()), index)
                )

            # as from libtorrent 1.0, files returning file_storage (lazy-iterable)
            if hasattr(lt, "file_storage") and isinstance(
                torrent_storage, lt.file_storage
            ):
                cur_path = torrent_storage.at(index).path.decode("utf-8")
            else:
                cur_path = torrent_storage[index].path.decode("utf-8")

            if cur_path != new_path:
                if not os.path.exists(unwanteddir_abs) and unwanteddir in new_path:
                    try:
                        os.makedirs(unwanteddir_abs)
                        if sys.platform == "win32":
                            ctypes.windll.kernel32.SetFileAttributesW(
                                unwanteddir_abs, 2
                            )  # 2 = FILE_ATTRIBUTE_HIDDEN
                    except OSError:
                        self._logger.error(
                            "LibtorrentDownloadImpl: could not create %s"
                            % unwanteddir_abs
                        )
                        # Note: If the destination directory can't be accessed, libtorrent will not be able to store the files.
                        # This will result in a DLSTATUS_STOPPED_ON_ERROR.

                # Path should be unicode if Libtorrent is using std::wstring (on Windows),
                # else we use str (on Linux).
                try:
                    self.handle.rename_file(index, new_path)
                except TypeError:
                    self.handle.rename_file(index, new_path.encode("utf-8"))

        # if in share mode, don't change priority of the file
        if not self.get_share_mode():
            self.handle.prioritize_files(filepriorities)

        self.unwanteddir_abs = unwanteddir_abs


# Traceback (RuntimeError)
ERROR   1545093433.24            tribler_window:68   (root)  Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 133, in on_read_data
RuntimeError: [Failure instance: Traceback: <type 'exceptions.AttributeError'>: 'LibtorrentDownloadImpl' object has no attribute 'unwanteddir_abs'
lib\site-packages\twisted\internet\base.py:1243:run
lib\site-packages\twisted\internet\base.py:1252:mainLoop
lib\site-packages\twisted\internet\base.py:878:runUntilCurrent
lib\site-packages\twisted\internet\task.py:239:__call__
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:150:maybeDeferred
Tribler\Core\Libtorrent\LibtorrentMgr.py:610:_task_process_alerts
Tribler\Core\Libtorrent\LibtorrentMgr.py:406:process_alert
Tribler\Core\Libtorrent\__init__.py:17:invoke_func
Tribler\Core\Libtorrent\LibtorrentDownloadImpl.py:514:process_alert
Tribler\Core\Libtorrent\LibtorrentDownloadImpl.py:614:on_file_renamed_alert
]

# Fixed code
def set_selected_files(self, selected_files=None):
    if not isinstance(self.tdef, TorrentDefNoMetainfo):
        if selected_files is None:
            selected_files = self.get_selected_files()
        else:
            DownloadConfigInterface.set_selected_files(self, selected_files)

        unwanteddir_abs = os.path.join(
            self.get_save_path().decode("utf-8"), self.unwanted_directory_name
        )
        torrent_info = get_info_from_handle(self.handle)
        if not torrent_info or not hasattr(torrent_info, "files"):
            self._logger.error(
                "File info not available for torrent [%s]", self.correctedinfoname
            )
            return

        filepriorities = []
        torrent_storage = torrent_info.files()

        for index, orig_path in enumerate(self.orig_files):
            filename = (
                orig_path[len(self.swarmname) + 1 :] if self.swarmname else orig_path
            )

            if filename in selected_files or not selected_files:
                filepriorities.append(1)
                new_path = orig_path
            else:
                filepriorities.append(0)
                new_path = os.path.join(
                    self.unwanted_directory_name,
                    "%s%d" % (hexlify(self.tdef.get_infohash()), index),
                )

            # as from libtorrent 1.0, files returning file_storage (lazy-iterable)
            if hasattr(lt, "file_storage") and isinstance(
                torrent_storage, lt.file_storage
            ):
                cur_path = torrent_storage.at(index).path.decode("utf-8")
            else:
                cur_path = torrent_storage[index].path.decode("utf-8")

            if cur_path != new_path:
                if (
                    not os.path.exists(unwanteddir_abs)
                    and self.unwanted_directory_name in new_path
                ):
                    try:
                        os.makedirs(unwanteddir_abs)
                        if sys.platform == "win32":
                            ctypes.windll.kernel32.SetFileAttributesW(
                                unwanteddir_abs, 2
                            )  # 2 = FILE_ATTRIBUTE_HIDDEN
                    except OSError:
                        self._logger.error(
                            "LibtorrentDownloadImpl: could not create %s"
                            % unwanteddir_abs
                        )
                        # Note: If the destination directory can't be accessed, libtorrent will not be able to store the files.
                        # This will result in a DLSTATUS_STOPPED_ON_ERROR.

                # Path should be unicode if Libtorrent is using std::wstring (on Windows),
                # else we use str (on Linux).
                try:
                    self.handle.rename_file(index, new_path)
                except TypeError:
                    self.handle.rename_file(index, new_path.encode("utf-8"))

        # if in share mode, don't change priority of the file
        if not self.get_share_mode():
            self.handle.prioritize_files(filepriorities)