Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize exceptions thrown by JSON parser #63560

Closed
musically-ut mannequin opened this issue Oct 23, 2013 · 9 comments
Closed

Specialize exceptions thrown by JSON parser #63560

musically-ut mannequin opened this issue Oct 23, 2013 · 9 comments
Assignees
Labels
extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@musically-ut
Copy link
Mannequin

musically-ut mannequin commented Oct 23, 2013

BPO 19361
Nosy @rhettinger, @etrepum, @pitrou, @ezio-melotti, @serhiy-storchaka, @musically-ut
Files
  • json_JSONDecodeError.patch
  • json_JSONDecodeError_2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-01-26.11:17:41.849>
    created_at = <Date 2013-10-23.11:22:14.054>
    labels = ['extension-modules', 'type-feature', 'library']
    title = 'Specialize exceptions thrown by JSON parser'
    updated_at = <Date 2015-01-26.11:17:42.374>
    user = 'https://github.com/musically-ut'

    bugs.python.org fields:

    activity = <Date 2015-01-26.11:17:42.374>
    actor = 'python-dev'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-01-26.11:17:41.849>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules', 'Library (Lib)']
    creation = <Date 2013-10-23.11:22:14.054>
    creator = 'musically_ut'
    dependencies = []
    files = ['36810', '37328']
    hgrepos = []
    issue_num = 19361
    keywords = ['patch', 'needs review']
    message_count = 9.0
    messages = ['201015', '201025', '201028', '218930', '218935', '228555', '231501', '231907', '234734']
    nosy_count = 9.0
    nosy_names = ['rhettinger', 'bob.ippolito', 'pitrou', 'ezio.melotti', 'Arfrever', 'cvrebert', 'python-dev', 'serhiy.storchaka', 'musically_ut']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue19361'
    versions = ['Python 3.5']

    @musically-ut
    Copy link
    Mannequin Author

    musically-ut mannequin commented Oct 23, 2013

    The JSON parser currently throws exceptions which contain information about where the error happened but this information is encoded in a string and not available to the programmer. This leads to issues like this: http://stackoverflow.com/questions/19519409/how-to-get-error-location-from-json-loads-in-python

    Would it be a problem if the errors thrown were specialized exceptions, e.g. JSONParsingError, which has ValueError as a base class with these details (line number, char number, etc.) exposed as public members? The changes required seem to be limited to changing http://hg.python.org/cpython/file/4c4f31a1b706/Lib/json/decoder.py and the related documentation.

    @musically-ut musically-ut mannequin added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Oct 23, 2013
    @ezio-melotti ezio-melotti added the stdlib Python modules in the Lib dir label Oct 23, 2013
    @serhiy-storchaka
    Copy link
    Member

    Simplejson already have JSONDecodeError.

    class JSONDecodeError(ValueError):
        """Subclass of ValueError with the following additional properties:
    msg: The unformatted error message
    doc: The JSON document being parsed
    pos: The start index of doc where parsing failed
    end: The end index of doc where parsing failed (may be None)
    lineno: The line corresponding to pos
    colno: The column corresponding to pos
    endlineno: The line corresponding to end (may be None)
    endcolno: The column corresponding to end (may be None)
    
    """
    

    Adding it to the stdlib is only matter of time.

    @musically-ut
    Copy link
    Mannequin Author

    musically-ut mannequin commented Oct 23, 2013

    Excellent!

    Is there an issue for merging Simplejson into stdlib?
    If so, can this issue be linked to that issue?

    @rhettinger
    Copy link
    Contributor

    Adding it to the stdlib is only matter of time.

    I think we can go forward with it right now.

    @etrepum
    Copy link
    Mannequin

    etrepum mannequin commented May 22, 2014

    simplejson v3.5.1 is probably a good target, let me know if there's anything that you'd like me to merge back in from stdlib to make maintenance easier. I haven't been tracking stdlib except when I've been added to issues.

    @serhiy-storchaka
    Copy link
    Member

    Here is a patch which adopts JSONDecodeError from simplejson.

    Unlike to simplejson it is located in the json.decoder module (json uses different way to pass an error from scanner to decoder).

    @serhiy-storchaka serhiy-storchaka self-assigned this Oct 5, 2014
    @serhiy-storchaka
    Copy link
    Member

    While we here may be change error messages?

    The only error which contains end position is "Extra data". And it doesn't look clean to me why end position should be mentioned at all (it is always len(data) here). I suggest to drop it, this will make error message cleaner and JSONDecodeError simpler.

    May be mention line and column in error message only if JSON data is multiline? Most machine generated data has one line only. Instead of "Expecting ',' delimiter: line 1 column 123 (char 124)" I suggest "Expecting ',' delimiter at position 124". Same as in re.error.

    @serhiy-storchaka
    Copy link
    Member

    Updated patch provides JSONDecodeError without end* attributes and with changed message for "Extra data".

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 26, 2015

    New changeset 07af9847dbec by Serhiy Storchaka in branch 'default':
    Issue bpo-19361: JSON decoder now raises JSONDecodeError instead of ValueError.
    https://hg.python.org/cpython/rev/07af9847dbec

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants