From fbd3db5753c3d1f20f67a0e2027d0c15756d6ce4 Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Sun, 22 May 2016 14:02:51 +0200 Subject: [PATCH] pyon: move string escaping table, add more info in errors --- artiq/protocols/pyon.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/artiq/protocols/pyon.py b/artiq/protocols/pyon.py index b72a7f9b3..28ee7e904 100644 --- a/artiq/protocols/pyon.py +++ b/artiq/protocols/pyon.py @@ -58,6 +58,14 @@ for _t in _numpy_scalar: _encode_map[getattr(numpy, _t)] = "npscalar" +_str_translation = { + ord("\""): "\\\"", + ord("\\"): "\\\\", + ord("\n"): "\\n", + ord("\r"): "\\r", +} + + class _Encoder: def __init__(self, pretty): self.pretty = pretty @@ -80,11 +88,7 @@ class _Encoder: def encode_str(self, x): # Do not use repr() for JSON compatibility. - tt = { - ord("\""): "\\\"", ord("\\"): "\\\\", - ord("\n"): "\\n", ord("\r"): "\\r" - } - return "\"" + x.translate(tt) + "\"" + return "\"" + x.translate(_str_translation) + "\"" def encode_bytes(self, x): return repr(x) @@ -155,7 +159,8 @@ class _Encoder: def encode(self, x): ty = _encode_map.get(type(x), None) if ty is None: - raise TypeError(repr(x) + " is not PYON serializable") + raise TypeError("`{!r}` ({}) is not PYON serializable" + .format(x, type(x))) return getattr(self, "encode_" + ty)(x)