forked from M-Labs/artiq
1
0
Fork 0

pyon: move string escaping table, add more info in errors

This commit is contained in:
Robert Jördens 2016-05-22 14:02:51 +02:00
parent 0857cfdcb1
commit fbd3db5753
1 changed files with 11 additions and 6 deletions

View File

@ -58,6 +58,14 @@ for _t in _numpy_scalar:
_encode_map[getattr(numpy, _t)] = "npscalar" _encode_map[getattr(numpy, _t)] = "npscalar"
_str_translation = {
ord("\""): "\\\"",
ord("\\"): "\\\\",
ord("\n"): "\\n",
ord("\r"): "\\r",
}
class _Encoder: class _Encoder:
def __init__(self, pretty): def __init__(self, pretty):
self.pretty = pretty self.pretty = pretty
@ -80,11 +88,7 @@ class _Encoder:
def encode_str(self, x): def encode_str(self, x):
# Do not use repr() for JSON compatibility. # Do not use repr() for JSON compatibility.
tt = { return "\"" + x.translate(_str_translation) + "\""
ord("\""): "\\\"", ord("\\"): "\\\\",
ord("\n"): "\\n", ord("\r"): "\\r"
}
return "\"" + x.translate(tt) + "\""
def encode_bytes(self, x): def encode_bytes(self, x):
return repr(x) return repr(x)
@ -155,7 +159,8 @@ class _Encoder:
def encode(self, x): def encode(self, x):
ty = _encode_map.get(type(x), None) ty = _encode_map.get(type(x), None)
if ty is 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) return getattr(self, "encode_" + ty)(x)