From 2f981e4a5908cdab10d2d0dc2801c5454c2f78d4 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Thu, 2 Oct 2014 12:00:47 +0000 Subject: distbuild: yaml-encode messages before json encoding JSON can only handle unicode strings, but commands can write anything to stdout/stderr, so we do the same trick as for the serialise, and json encode yaml. --- distbuild/jm.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/distbuild/jm.py b/distbuild/jm.py index 513c69fa..615100e4 100644 --- a/distbuild/jm.py +++ b/distbuild/jm.py @@ -22,6 +22,7 @@ import logging import os import socket import sys +import yaml from sm import StateMachine from stringbuffer import StringBuffer @@ -79,7 +80,12 @@ class JsonMachine(StateMachine): def send(self, msg): '''Send a message to the other side.''' - self.sockbuf.write('%s\n' % json.dumps(msg)) + if self.debug_json: + logging.debug('JsonMachine: Sending message %s' % repr(msg)) + s = json.dumps(yaml.safe_dump(msg)) + if self.debug_json: + logging.debug('JsonMachine: As %s' % repr(s)) + self.sockbuf.write('%s\n' % s) def close(self): '''Tell state machine it should shut down. @@ -103,7 +109,7 @@ class JsonMachine(StateMachine): line = line.rstrip() if self.debug_json: logging.debug('JsonMachine: line: %s' % repr(line)) - msg = json.loads(line) + msg = yaml.load(json.loads(line)) self.mainloop.queue_event(self, JsonNewMessage(msg)) def _send_eof(self, event_source, event): -- cgit v1.2.1