web_config: Interpret fish output as utf-8

Use the unicode replacement character in place of non-utf-8 sequences.
This commit is contained in:
Kevin Ballard 2014-10-09 20:21:26 -07:00
parent 7493c9a040
commit cc7f1755aa

View File

@ -35,13 +35,25 @@ except ImportError:
FISH_BIN_PATH = False # will be set later
def run_fish_cmd(text):
from subprocess import PIPE
p = subprocess.Popen([FISH_BIN_PATH], stdin=PIPE, stdout=PIPE, stderr=PIPE)
# ensure that fish is using UTF-8
ctype = os.environ.get("LC_ALL", os.environ.get("LC_CTYPE", os.environ.get("LANG")))
env = None
if re.search(r"\.utf-?8$", ctype, flags=re.I) is None:
# override LC_CTYPE with en_US.UTF-8
# We're assuming this locale exists.
# Fish makes the same assumption in config.fish
env = os.environ.copy()
env.update(LC_CTYPE="en_US.UTF-8", LANG="en_US.UTF-8")
p = subprocess.Popen([FISH_BIN_PATH], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)
if IS_PY2:
out, err = p.communicate(text)
# interpret as utf-8 in a lossy fashion
out = unicode(out, 'utf-8', 'replace').encode('utf-8')
err = unicode(err, 'utf-8', 'replace').encode('utf-8')
else:
out, err = p.communicate(bytes(text, 'utf-8'))
out = str(out, 'utf-8')
err = str(err, 'utf-8')
out = str(out, 'utf-8', 'replace')
err = str(err, 'utf-8', 'replace')
return(out, err)
def escape_fish_cmd(text):