# Buggy code
def get_repo_data(saltenv="base"):
    """
    Returns the existing package meteadata db. Will create it, if it does not
    exist, however will not refresh it.

    Args:
        saltenv (str): Salt environment. Default ``base``

    Returns:
        dict: A dict containing contents of metadata db.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.get_repo_data
    """
    # we only call refresh_db if it does not exist, as we want to return
    # the existing data even if its old, other parts of the code call this,
    # but they will call refresh if they need too.
    repo_details = _get_repo_details(saltenv)

    if repo_details.winrepo_age == -1:
        # no repo meta db
        log.debug("No winrepo.p cache file. Refresh pkg db now.")
        refresh_db(saltenv=saltenv)

    if "winrepo.data" in __context__:
        log.trace("get_repo_data returning results from __context__")
        return __context__["winrepo.data"]
    else:
        log.trace("get_repo_data called reading from disk")

    try:
        serial = salt.payload.Serial(__opts__)
        with salt.utils.files.fopen(repo_details.winrepo_file, "rb") as repofile:
            try:
                repodata = salt.utils.data.decode(serial.loads(repofile.read()) or {})
                __context__["winrepo.data"] = repodata
                return repodata
            except Exception as exc:
                log.exception(exc)
                return {}
    except IOError as exc:
        log.error("Not able to read repo file")
        log.exception(exc)
        return {}


# Traceback (UnicodeEncodeError)
The minion function caused an exception: Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\minion.py", line 1624, in _thread_return
return_data = minion_instance.executors[fname](opts, data, func, args, kwargs)
File "c:\salt\bin\lib\site-packages\salt\executors\direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "c:\salt\bin\lib\site-packages\salt\modules\win_pkg.py", line 1142, in install
old = list_pkgs(saltenv=saltenv, refresh=refresh, versions_as_list=True)
File "c:\salt\bin\lib\site-packages\salt\modules\win_pkg.py", line 400, in list_pkgs
name_map = _get_name_map(saltenv)
File "c:\salt\bin\lib\site-packages\salt\modules\win_pkg.py", line 1846, in _get_name_map
u_name_map[k.decode('utf-8')] = name_map[k]
File "c:\salt\bin\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-11: ordinal not in range(128)

# Fixed code
def get_repo_data(saltenv="base"):
    """
    Returns the existing package meteadata db. Will create it, if it does not
    exist, however will not refresh it.

    Args:
        saltenv (str): Salt environment. Default ``base``

    Returns:
        dict: A dict containing contents of metadata db.

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.get_repo_data
    """
    # we only call refresh_db if it does not exist, as we want to return
    # the existing data even if its old, other parts of the code call this,
    # but they will call refresh if they need too.
    repo_details = _get_repo_details(saltenv)

    if repo_details.winrepo_age == -1:
        # no repo meta db
        log.debug("No winrepo.p cache file. Refresh pkg db now.")
        refresh_db(saltenv=saltenv)

    if "winrepo.data" in __context__:
        log.trace("get_repo_data returning results from __context__")
        return __context__["winrepo.data"]
    else:
        log.trace("get_repo_data called reading from disk")

    try:
        serial = salt.payload.Serial(__opts__)
        with salt.utils.files.fopen(repo_details.winrepo_file, "rb") as repofile:
            try:
                repodata = salt.utils.data.decode(
                    serial.loads(repofile.read(), encoding="utf-8") or {}
                )
                __context__["winrepo.data"] = repodata
                return repodata
            except Exception as exc:
                log.exception(exc)
                return {}
    except IOError as exc:
        log.error("Not able to read repo file")
        log.exception(exc)
        return {}