Get bytestring filename and return unicode.
    First, try to decode from default file system encoding
    If that fails, use ``chardet`` module to guess encoding.
    As a last resort, try to decode as utf-8.

    If the argument already is unicode, return as is

def _decode_filename_to_unicode(f):
    '''Get bytestring filename and return unicode.
    First, try to decode from default file system encoding
    If that fails, use ``chardet`` module to guess encoding.
    As a last resort, try to decode as utf-8.

    If the argument already is unicode, return as is'''

    log.debug('_decode_filename_to_unicode(%s)', repr(f))
    if isinstance(f, unicode):
        return f
    try:
        return f.decode(sys.getfilesystemencoding())
    except UnicodeDecodeError:
        charguess = chardet.detect(f)
        log.debug("chardet filename: %r -> %r", f, charguess)
        if charguess['encoding'] is not None:
            try:
                return f.decode(charguess['encoding'])
            except UnicodeDecodeError:
                pass
        log.warning('Cannot understand decoding of this filename: %r (guessed %r, but was wrong)',
                    f, charguess)
        log.debug('Trying utf-8 to decode %r', f)
        try:
            return f.decode('utf-8')
        except UnicodeDecodeError:
            pass
        log.debug('Trying latin1 to decode %r', f)
        try:
            return f.decode('latin1')
        except UnicodeDecodeError:
            log.warning('Exhausted all options. Decoding %r to safe ascii', f)
            return f.decode('ascii', errors='ignore')