def safeExpandUser(filepath):
 retVal = filepath
 try:
  retVal = os.path.expanduser(filepath)
 except UnicodeError:
  _ = locale.getdefaultlocale()
  encoding = (_[1] if (_ and (len(_) > 1)) else UNICODE_ENCODING)
  retVal = getUnicode(os.path.expanduser(filepath.encode(encoding)), encoding=encoding)
 return retVal

def safeExpandUser ( filepath ) : retVal = filepath try : retVal = os . path . expanduser ( filepath ) except UnicodeError : _ = locale . getdefaultlocale ( ) encoding = ( _ [ 1 ] if ( _ and ( len ( _ ) > 1 ) ) else UNICODE_ENCODING ) retVal = getUnicode ( os . path . expanduser ( filepath . encode ( encoding ) ) , encoding = encoding ) return retVal
patch for a python issue18171 .