ox.jsonc fails ungracefully on parse errors if simplejson is not in use #2858

Closed
opened 2015-11-12 11:05:27 +00:00 by wjt · 5 comments
In [1]: import ox
In [2]: ox.jsonc.loads(**{**)
---------------------------------------------------------------------------
<AttributeError>                            Traceback (most recent call last)
<ipython-input-2-631203c85abd> in <module>()
----> 1 ox.jsonc.loads(**{**)

/home/wjt/src/python-ox/ox/jsonc.py in loads(source)
     15         minified = minify(source)
     16         return json.loads(minified)
---> 17     except json.JSONDecodeError as e:
     18         s = minified.split('\n')
     19         context = s[]e.lineno-1[e.colno-1):e.colno+30]max(0,

<AttributeError>: 'module' object has no attribute 'JSONDecodeError'

simplejson defines and raises JSONDecodeError with nice lineno, colno attributes; but json (which is used if simplejson is not installed just raises ValueError with the line/column range information mangled into the string.

ox might or might not try to parse the string and give (useful) contextual information; but at least it shouldn't throw AttributeError.

``` In [1]: import ox In [2]: ox.jsonc.loads(**{**) --------------------------------------------------------------------------- <AttributeError> Traceback (most recent call last) <ipython-input-2-631203c85abd> in <module>() ----> 1 ox.jsonc.loads(**{**) /home/wjt/src/python-ox/ox/jsonc.py in loads(source) 15 minified = minify(source) 16 return json.loads(minified) ---> 17 except json.JSONDecodeError as e: 18 s = minified.split('\n') 19 context = s[]e.lineno-1[e.colno-1):e.colno+30]max(0, <AttributeError>: 'module' object has no attribute 'JSONDecodeError' ``` `simplejson` defines and raises `JSONDecodeError` with nice `lineno`, `colno` attributes; but `json` (which is used if `simplejson` is not installed just raises `ValueError` with the line/column range information mangled into the string. ox might or might not try to parse the string and give (useful) contextual information; but at least it shouldn't throw `AttributeError`.
j added the
python-ox
label 2015-11-12 11:05:27 +00:00
j added this to the 14.04 milestone 2015-11-12 11:05:27 +00:00
j self-assigned this 2015-11-12 11:05:27 +00:00
j added the
normal
defect
labels 2015-11-12 11:05:27 +00:00
Author

Attachment 0001-jsonc-handle-parse-errors-from-json-gracefully-fixes.patch (1967 bytes) added

**Attachment** 0001-jsonc-handle-parse-errors-from-json-gracefully-fixes.patch (1967 bytes) added
Owner

Currently never uses simplejson.JSONDecodeError since you check for _JSONDecodeError not JSONDecodeError. Since that also works with simplejson, all those checks can be removed.

Needs to be fixed to work with python3.4+ (python-ox also should work with python 3.4+) since message is no longer available:

AttributeError: 'ValueError' object has no attribute 'message'

will push with those changes fixed in a bit.

Currently never uses simplejson.JSONDecodeError since you check for _JSONDecodeError not JSONDecodeError. Since that also works with simplejson, all those checks can be removed. Needs to be fixed to work with python3.4+ (python-ox also should work with python 3.4+) since message is no longer available: `AttributeError: 'ValueError' object has no attribute 'message'` will push with those changes fixed in a bit.
Owner

In [python-ox,735]changeset:"python-ox,735":

#!CommitTicketReference repository="" revision="python-ox,735"
jsonc: handle parse errors from 'json' gracefully (fixes #2858)

- JSONDecodeError is only available in simplejson, use ValueError
- imporove error context output
In [python-ox,735]changeset:"python-ox,735": ``` #!CommitTicketReference repository="" revision="python-ox,735" jsonc: handle parse errors from 'json' gracefully (fixes #2858) - JSONDecodeError is only available in simplejson, use ValueError - imporove error context output ```
j added the
fixed
label 2015-11-12 12:02:39 +00:00
j closed this issue 2015-11-12 12:02:39 +00:00
Owner

[]changeset:pandora,5071 uses this and improves the error message if an invalid config files is passed.

[]changeset:pandora,5071 uses this and improves the error message if an invalid config files is passed.
Author

Replying to [j]comment:3:

[]changeset:pandora,5071 uses this and improves the error message if an invalid config files is passed.

Great, failing hard during init would have saved me a bit of debugging time. Thanks!

Replying to [j]comment:3: > []changeset:pandora,5071 uses this and improves the error message if an invalid config files is passed. Great, failing hard during init would have saved me a bit of debugging time. Thanks!
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: 0x2620/pandora#2858
No description provided.