fix regression from commit 20bcbcc2

There were two places in the code that used the anti-pattern of
returning True on success else an error message. In python you should
always be able to replace `if x == True:` with just `if x:`. Which is
what the lint tool recommended. Unfortunately I didn't notice how the
return value was being used. This fixes that by changing the two
affected functions to return an error message or None on success.

This also adds `from __future__ import print_function` since the code
uses the `print(msg)` function form rather than the `print msg`
statement form. The former works by accident on python2 because the
parens are interpreted as creating parenthesized expression that
devolves to the single string inside the parens. So while the future
import isn't strictly speaking necessary it will help avoid mistakes in
the future if more complex `print()` calls are added.

Partial fix for #3620
This commit is contained in:
Kurtis Rader 2016-12-04 15:44:57 -08:00
parent 41acaced3f
commit 7bb3bf7c74

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import print_function
import binascii import binascii
import cgi import cgi
import glob import glob
@ -40,7 +41,6 @@ except ImportError:
def run_fish_cmd(text): def run_fish_cmd(text):
from subprocess import PIPE
# Ensure that fish is using UTF-8. # Ensure that fish is using UTF-8.
ctype = os.environ.get("LC_ALL", os.environ.get("LC_CTYPE", ctype = os.environ.get("LC_ALL", os.environ.get("LC_CTYPE",
os.environ.get("LANG"))) os.environ.get("LANG")))
@ -51,12 +51,13 @@ def run_fish_cmd(text):
# Fish makes the same assumption in config.fish # Fish makes the same assumption in config.fish
env = os.environ.copy() env = os.environ.copy()
env.update(LC_CTYPE="en_US.UTF-8", LANG="en_US.UTF-8") 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, p = subprocess.Popen([FISH_BIN_PATH], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=env) env=env)
out, err = p.communicate(text.encode('utf-8')) out, err = p.communicate(text.encode('utf-8'))
out = out.decode('utf-8', 'replace') out = out.decode('utf-8', 'replace')
err = err.decode('utf-8', 'replace') err = err.decode('utf-8', 'replace')
return(out, err) return out, err
def escape_fish_cmd(text): def escape_fish_cmd(text):
@ -836,18 +837,18 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_remove_abbreviation(self, abbreviation): def do_remove_abbreviation(self, abbreviation):
out, err = run_fish_cmd('abbr --erase %s' % abbreviation['word']) out, err = run_fish_cmd('abbr --erase %s' % abbreviation['word'])
if out or err: if err:
return err return err
else: else:
return True return None
def do_save_abbreviation(self, abbreviation): def do_save_abbreviation(self, abbreviation):
out, err = run_fish_cmd('abbr --add \'%s\' \'%s\'' % ( out, err = run_fish_cmd('abbr --add \'%s\' \'%s\'' % (
abbreviation['word'], abbreviation['phrase'])) abbreviation['word'], abbreviation['phrase']))
if out or err: if err:
return out return err
else: else:
return True return None
def secure_startswith(self, haystack, needle): def secure_startswith(self, haystack, needle):
if len(haystack) < len(needle): if len(haystack) < len(needle):
@ -975,17 +976,17 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
else: else:
output = ["Unable to set prompt"] output = ["Unable to set prompt"]
elif p == '/save_abbreviation/': elif p == '/save_abbreviation/':
r = self.do_save_abbreviation(postvars) errmsg = self.do_save_abbreviation(postvars)
if r: if errmsg:
output = ["OK"] output = [errmsg]
else: else:
output = [r] output = ["OK"]
elif p == '/remove_abbreviation/': elif p == '/remove_abbreviation/':
r = self.do_remove_abbreviation(postvars) errmsg = self.do_remove_abbreviation(postvars)
if r: if errmsg:
output = ["OK"] output = [errmsg]
else: else:
output = [r] output = ["OK"]
else: else:
return self.send_error(404) return self.send_error(404)