# Buggy code
def convert_config_to_tribler74(state_dir=None):
    """
    Convert the download config files to Tribler 7.4 format. The extensions will also be renamed from .state to .conf
    """
    if PY3:
        from lib2to3.refactor import RefactoringTool, get_fixers_from_package

        refactoring_tool = RefactoringTool(
            fixer_names=get_fixers_from_package("lib2to3.fixes")
        )

    state_dir = state_dir or TriblerConfig.get_default_base_state_dir()
    for _, filename in enumerate(
        iglob(os.path.join(state_dir, STATEDIR_CHECKPOINT_DIR, "*.state"))
    ):
        old_config = CallbackConfigParser()
        try:
            old_config.read_file(filename)
        except MissingSectionHeaderError:
            logger.error(
                "Removing download state file %s since it appears to be corrupt",
                filename,
            )
            os.remove(filename)

        # We first need to fix the .state file such that it has the correct metainfo/resumedata
        for section, option in [("state", "metainfo"), ("state", "engineresumedata")]:
            value = old_config.get(section, option, literal_eval=False)
            ungarbled_dict = None
            if PY3:
                value = str(
                    refactoring_tool.refactor_string(value + "\n", option + "_2to3")
                )
                ungarbled_dict = recursive_ungarble_metainfo(ast.literal_eval(value))
            try:
                value = ungarbled_dict or ast.literal_eval(value)
                old_config.set(
                    section, option, base64.b64encode(lt.bencode(value)).decode("utf-8")
                )
            except (ValueError, SyntaxError):
                logger.error(
                    "Removing download state file %s since it could not be converted",
                    filename,
                )
                os.remove(filename)
                continue

        # Remove dlstate since the same information is already stored in the resumedata
        if old_config.has_option("state", "dlstate"):
            old_config.remove_option("state", "dlstate")

        new_config = ConfigObj(infile=filename[:-6] + ".conf", encoding="utf8")
        for section in old_config.sections():
            for key, _ in old_config.items(section):
                val = old_config.get(section, key)
                if section not in new_config:
                    new_config[section] = {}
                new_config[section][key] = val
        new_config.write()
        os.remove(filename)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI/core_manager.py", line 121, in on_received_state
RuntimeError: Unhandled Error
Traceback (most recent call last):
File "site-packages/twisted/internet/base.py", line 434, in fireEvent

File "site-packages/twisted/internet/defer.py", line 322, in addCallback

File "site-packages/twisted/internet/defer.py", line 311, in addCallbacks

File "site-packages/twisted/internet/defer.py", line 654, in _runCallbacks

--- <exception caught here> ---
File "site-packages/twisted/internet/base.py", line 447, in _continueFiring

File "run_tribler.py", line 81, in start_tribler

File "Tribler/Core/Session.py", line 412, in start

File "Tribler/Core/Upgrade/upgrade.py", line 93, in run

File "Tribler/Core/Upgrade/upgrade.py", line 172, in upgrade_config_to_74

File "Tribler/Core/Upgrade/config_converter.py", line 226, in convert_config_to_tribler74

File "lib2to3/refactor.py", line 361, in refactor_string
name, err.__class__.__name__, err)
File "lib2to3/refactor.py", line 358, in refactor_string
tree = self.driver.parse_string(data)
File "lib2to3/pgen2/driver.py", line 104, in parse_string
return self.parse_tokens(tokens, debug)
File "lib2to3/pgen2/driver.py", line 72, in parse_tokens
if p.addtoken(type, value, (prefix, start)):
File "lib2to3/pgen2/parse.py", line 116, in addtoken
ilabel = self.classify(type, value, context)
File "lib2to3/pgen2/parse.py", line 172, in classify
raise ParseError("bad token", type, value, context)
lib2to3.pgen2.parse.ParseError: bad token: type=58, value=' ', context=('', (1, 459))

# Fixed code
def convert_config_to_tribler74(state_dir=None):
    """
    Convert the download config files to Tribler 7.4 format. The extensions will also be renamed from .state to .conf
    """
    refactoring_tool = None
    if PY3:
        from lib2to3.refactor import RefactoringTool, get_fixers_from_package

        refactoring_tool = RefactoringTool(
            fixer_names=get_fixers_from_package("lib2to3.fixes")
        )

    state_dir = state_dir or TriblerConfig.get_default_base_state_dir()
    for _, filename in enumerate(
        iglob(os.path.join(state_dir, STATEDIR_CHECKPOINT_DIR, "*.state"))
    ):
        convert_state_file_to_conf_74(filename, refactoring_tool=refactoring_tool)