# Buggy code
def _get_all_files_as_unicode_with_length(self):
    """Get a generator for files in the torrent def. No filtering
    is possible and all tricks are allowed to obtain a unicode
    list of filenames.
    @return A unicode filename generator.
    """
    if self.metainfo and b"files" in self.metainfo[b"info"]:
        # Multi-file torrent
        join = os.path.join
        files = self.metainfo[b"info"][b"files"]

        for file_dict in files:
            if b"path.utf-8" in file_dict:
                # This file has an utf-8 encoded list of elements.
                # We assume that it is correctly encoded and use
                # it normally
                try:
                    yield (
                        join(
                            *[
                                ensure_unicode(element, "UTF-8")
                                for element in file_dict[b"path.utf-8"]
                            ]
                        ),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

            if b"path" in file_dict:
                # Try to use the 'encoding' field.  If it exists,
                # it should contain something like 'utf-8'
                if b"encoding" in self.metainfo:
                    encoding = self.metainfo[b"encoding"]
                    try:
                        yield (
                            join(
                                *[
                                    ensure_unicode(element, encoding)
                                    for element in file_dict[b"path"]
                                ]
                            ),
                            file_dict[b"length"],
                        )
                        continue
                    except UnicodeError:
                        pass
                    except LookupError:
                        # Some encodings are not supported by
                        # python.  For instance, the MBCS codec
                        # which is used by Windows is not
                        # supported (Jan 2010)
                        pass

                # Try to convert the names in path to unicode,
                # assuming that it was encoded as utf-8
                try:
                    yield (
                        join(
                            *[
                                ensure_unicode(element, "UTF-8")
                                for element in file_dict[b"path"]
                            ]
                        ),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

                # Try to convert the names in path to unicode,
                # without specifying the encoding
                try:
                    yield (
                        join(*[text_type(element) for element in file_dict[b"path"]]),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

                # Convert the names in path to unicode by
                # replacing out all characters that may -even
                # remotely- cause problems with the '?' character
                try:

                    def filter_characters(name):
                        def filter_character(char):
                            if 0 < ord(char) < 128:
                                return char
                            else:
                                self._logger.debug(
                                    "Bad character filter %s, isalnum? %s",
                                    ord(char),
                                    char.isalnum(),
                                )
                                return "?"

                        return "".join([filter_character(char) for char in name])

                    yield (
                        join(
                            *[
                                text_type(filter_characters(element))
                                for element in file_dict[b"path"]
                            ]
                        ),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

    elif self.metainfo:
        # Single-file torrent
        yield self.get_name_as_unicode(), self.metainfo[b"info"][b"length"]


# Traceback (Tribler.Core.exceptions.SaveResumeDataError)
ERROR   1575205842.50    LibtorrentDownloadImpl:805  (LibtorrentDownloadImpl)  Resume data failed to save: <class 'Tribler.Core.exceptions.SaveResumeDataError'>: torrent has no metadata
ERROR   1575205845.53              rest_manager:83   (RESTRequest)  [Failure instance: Traceback: <class 'TypeError'>: decode() argument 1 must be str, not bytes
lib\site-packages\twisted\web\http.py:2146:lineReceived
lib\site-packages\twisted\web\http.py:2237:allContentReceived
lib\site-packages\twisted\web\http.py:937:requestReceived
Tribler\Core\Modules\restapi\rest_manager.py:118:process
--- <exception caught here> ---
lib\site-packages\twisted\web\server.py:217:process
lib\site-packages\twisted\web\server.py:284:render
lib\site-packages\twisted\web\resource.py:265:render
Tribler\Core\Modules\restapi\downloads_endpoint.py:294:render_GET
Tribler\Core\Modules\restapi\downloads_endpoint.py:111:get_files_info_json
Tribler\Core\DownloadState.py:167:get_files_completion
Tribler\Core\TorrentDef.py:422:get_files_with_length
Tribler\Core\TorrentDef.py:363:_get_all_files_as_unicode_with_length
Tribler\Core\TorrentDef.py:363:<listcomp>
Tribler\Core\Utilities\unicode.py:19:ensure_unicode
]
Traceback (most recent call last):
File "lib\site-packages\twisted\web\server.py", line 217, in process
File "lib\site-packages\twisted\web\server.py", line 284, in render
File "lib\site-packages\twisted\web\resource.py", line 265, in render
File "Tribler\Core\Modules\restapi\downloads_endpoint.py", line 294, in render_GET
File "Tribler\Core\Modules\restapi\downloads_endpoint.py", line 111, in get_files_info_json
File "Tribler\Core\DownloadState.py", line 167, in get_files_completion
File "Tribler\Core\TorrentDef.py", line 422, in get_files_with_length
File "Tribler\Core\TorrentDef.py", line 363, in _get_all_files_as_unicode_with_length
File "Tribler\Core\TorrentDef.py", line 363, in <listcomp>
File "Tribler\Core\Utilities\unicode.py", line 19, in ensure_unicode

# Fixed code
def _get_all_files_as_unicode_with_length(self):
    """Get a generator for files in the torrent def. No filtering
    is possible and all tricks are allowed to obtain a unicode
    list of filenames.
    @return A unicode filename generator.
    """
    if self.metainfo and b"files" in self.metainfo[b"info"]:
        # Multi-file torrent
        join = os.path.join
        files = self.metainfo[b"info"][b"files"]

        for file_dict in files:
            if b"path.utf-8" in file_dict:
                # This file has an utf-8 encoded list of elements.
                # We assume that it is correctly encoded and use
                # it normally
                try:
                    yield (
                        join(
                            *[
                                ensure_unicode(element, "UTF-8")
                                for element in file_dict[b"path.utf-8"]
                            ]
                        ),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

            if b"path" in file_dict:
                # Try to use the 'encoding' field.  If it exists,
                # it should contain something like 'utf-8'
                if b"encoding" in self.metainfo:
                    encoding = ensure_unicode(self.metainfo[b"encoding"], "utf8")
                    try:
                        yield (
                            join(
                                *[
                                    ensure_unicode(element, encoding)
                                    for element in file_dict[b"path"]
                                ]
                            ),
                            file_dict[b"length"],
                        )
                        continue
                    except UnicodeError:
                        pass
                    except LookupError:
                        # Some encodings are not supported by
                        # python.  For instance, the MBCS codec
                        # which is used by Windows is not
                        # supported (Jan 2010)
                        pass

                # Try to convert the names in path to unicode,
                # assuming that it was encoded as utf-8
                try:
                    yield (
                        join(
                            *[
                                ensure_unicode(element, "UTF-8")
                                for element in file_dict[b"path"]
                            ]
                        ),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

                # Try to convert the names in path to unicode,
                # without specifying the encoding
                try:
                    yield (
                        join(*[text_type(element) for element in file_dict[b"path"]]),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

                # Convert the names in path to unicode by
                # replacing out all characters that may -even
                # remotely- cause problems with the '?' character
                try:

                    def filter_characters(name):
                        def filter_character(char):
                            if 0 < ord(char) < 128:
                                return char
                            else:
                                self._logger.debug(
                                    "Bad character filter %s, isalnum? %s",
                                    ord(char),
                                    char.isalnum(),
                                )
                                return "?"

                        return "".join([filter_character(char) for char in name])

                    yield (
                        join(
                            *[
                                text_type(filter_characters(element))
                                for element in file_dict[b"path"]
                            ]
                        ),
                        file_dict[b"length"],
                    )
                    continue
                except UnicodeError:
                    pass

    elif self.metainfo:
        # Single-file torrent
        yield self.get_name_as_unicode(), self.metainfo[b"info"][b"length"]