Permalink
Browse files

Use repr() to pretty print because it can handle arbitrary bytes.

json.dumps() crashes on invalid utf-8.
  • Loading branch information...
Andy Chu
Andy Chu committed Oct 18, 2017
1 parent bc3a9fd commit de4b70284851af8ac052f7a60ddde1a5e1bffa74
Showing with 7 additions and 2 deletions.
  1. +7 −2 asdl/format.py
View
@@ -316,13 +316,18 @@ def MakeTree(obj, abbrev_hook=None, omit_empty=True):
# This is word characters, - and _, as well as path name characters . and /.
_PLAIN_RE = re.compile(r'^[a-zA-Z0-9\-_./]+$')
# NOTE: Turning JSON back on can be a cheap hack for detecting invalid unicode.
# But we want to write our own AST walker for that.
def _PrettyString(s):
if '\n' in s:
return json.dumps(s) # account for the fact that $ matches the newline
#return json.dumps(s) # account for the fact that $ matches the newline
return repr(s)
if _PLAIN_RE.match(s):
return s
else:
return json.dumps(s)
#return json.dumps(s)
return repr(s)
INDENT = 2

0 comments on commit de4b702

Please sign in to comment.