diff options
author | Walter Bender <walter@sugarlabs.org> | 2010-11-18 02:29:15 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2010-11-18 02:29:15 (GMT) |
commit | 20e252c1e3db408ebb005a705dcf41d70c758c5e (patch) | |
tree | d3f7bd2957409afa45e5a9c470365b77af5c5d76 /TurtleArt | |
parent | 45fa420747151cc139310c3c4e9b71e945683a43 (diff) |
catch ValueError exception in json encoding
Diffstat (limited to 'TurtleArt')
-rw-r--r-- | TurtleArt/tautils.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/TurtleArt/tautils.py b/TurtleArt/tautils.py index 7139f3b..f6d410b 100644 --- a/TurtleArt/tautils.py +++ b/TurtleArt/tautils.py @@ -62,7 +62,7 @@ def convert(x, fn, try_ord=True): The strategy for mixing numbers and strings is to first try converting the string to a float; then if the string is a single character, try converting it to an ord; finally, just treat it as a - string. Numbers appended to strings are first trreated as ints, then + string. Numbers appended to strings are first treated as ints, then floats. ''' try: @@ -106,11 +106,17 @@ def json_load(text): _listdata = json.read(text) else: # strip out leading and trailing whitespace, nulls, and newlines - text = text.lstrip() - text = text.replace('\12', '') - text = text.replace('\00', '') - _io = StringIO(text.rstrip()) - _listdata = jload(_io) + clean_text = text.lstrip() + clean_text = clean_text.replace('\12', '') + clean_text = clean_text.replace('\00', '') + _io = StringIO(clean_text.rstrip()) + try: + _listdata = jload(_io) + except ValueError: + # assume that text is ascii list + _listdata = text.split() + for i, value in enumerate(_listdata): + _listdata[i] = convert(value, float) # json converts tuples to lists, so we need to convert back, return _tuplify(_listdata) |