1 Commits

Author SHA1 Message Date
5b878378f3 Translate UTF-8 in command output more robustly 2013-07-22 13:03:09 -04:00
2 changed files with 23 additions and 1 deletions

View File

@@ -60,12 +60,16 @@ class AppProcess(object):
self.manager = manager self.manager = manager
def process_status(self, pid): def process_status(self, pid):
# We need to convert the log (which is bytes) to Unicode
# characters, in order to send it via JSON. Treat it as UTF-8
# but replace invalid characters with markers.
log = self.manager[pid].log.decode('utf-8', errors='replace')
return { return {
"pid": pid, "pid": pid,
"alive": self.manager[pid].alive, "alive": self.manager[pid].alive,
"exitcode": self.manager[pid].exitcode, "exitcode": self.manager[pid].exitcode,
"start_time": self.manager[pid].start_time, "start_time": self.manager[pid].start_time,
"log": self.manager[pid].log, "log": log
} }
# /process/status # /process/status

View File

@@ -372,3 +372,21 @@ class TestClient(object):
# kill all processes # kill all processes
for pid in client.get("process/list"): for pid in client.get("process/list"):
client.post("process/remove", { "pid": pid }) client.post("process/remove", { "pid": pid })
def test_client_10_unicode(self):
client = HTTPClient(baseurl = testurl, post_json = True)
eq_(client.get("process/list"), [])
def verify(cmd, result):
pid = client.post("run/command", { "argv": [ "sh", "-c", cmd ] })
eq_(client.get("process/list"), [pid])
status = self.wait_end(client, pid)
eq_(result, status["log"])
# Unicode should work
verify("echo -n hello", "hello")
verify(u"echo -n ☠", u"")
verify("echo -ne \\\\xe2\\\\x98\\\\xa0", u"")
# Programs that spit out invalid UTF-8 should get replacement
# markers
verify("echo -ne \\\\xae", u"\ufffd")