import locale


def decode_bytes(bytes_to_decode):
    """
    Decode and return a byte string using utf-8, falling back to system's encoding if that fails.

    Source: Sublime Linter
    https://github.com/SublimeLinter/SublimeLinter/blob/master/lint/util.py#L272
    """
    if not bytes_to_decode:
        return ''

    try:
        return bytes_to_decode.decode('utf-8')
    except UnicodeError:
        return bytes_to_decode.decode(locale.getpreferredencoding(), errors='replace')