def METHOD_NAME(filename):
    """
    Encode filename to the filesystem encoding with 'surrogateescape' error
    handler, return bytes unchanged. On Windows, use 'strict' error handler if
    the file system encoding is 'mbcs' (which is the default encoding).
    """
    encoding = sys.getfilesystemencoding()
    errors = 'surrogateescape'
    if isinstance(filename, bytes):
        return filename
    elif isinstance(filename, str):
        return filename.encode(encoding, errors)
    else:
        raise TypeError("expect bytes or str, not %s" % type(filename).__name__)