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

Add additional attributes to re.error #66768

Closed
serhiy-storchaka opened this issue Oct 8, 2014 · 11 comments
Closed

Add additional attributes to re.error #66768

serhiy-storchaka opened this issue Oct 8, 2014 · 11 comments
Assignees
Labels
topic-regex type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 22578
Nosy @pitrou, @ezio-melotti, @serhiy-storchaka
Files
  • re_error_attrs.patch
  • re_error_attrs2.patch
  • re_error_attrs3.patch
  • re_error_attrs4.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 2014-11-10.12:43:24.800>
    created_at = <Date 2014-10-08.12:45:10.228>
    labels = ['expert-regex', 'type-feature']
    title = 'Add additional attributes to re.error'
    updated_at = <Date 2014-12-01.09:27:20.102>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2014-12-01.09:27:20.102>
    actor = 'python-dev'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2014-11-10.12:43:24.800>
    closer = 'serhiy.storchaka'
    components = ['Regular Expressions']
    creation = <Date 2014-10-08.12:45:10.228>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['36834', '36836', '36852', '36860']
    hgrepos = []
    issue_num = 22578
    keywords = ['patch']
    message_count = 11.0
    messages = ['228787', '228788', '228793', '228820', '228874', '228972', '230953', '230956', '230959', '230960', '231920']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'ezio.melotti', 'mrabarnett', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue22578'
    versions = ['Python 3.5']

    @serhiy-storchaka
    Copy link
    Member Author

    Proposed patch adds additional attributes to the re.error exception: msg, pattern, pos, colno, lineno. It also adds helpful information to error message.

    Examples:

    >>> re.compile(r"abc\u123")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/serhiy/py/cpython/Lib/re.py", line 220, in compile
        return _compile(pattern, flags)
      File "/home/serhiy/py/cpython/Lib/re.py", line 287, in _compile
        p = sre_compile.compile(pattern, flags)
      File "/home/serhiy/py/cpython/Lib/sre_compile.py", line 465, in compile
        p = sre_parse.parse(p, flags)
      File "/home/serhiy/py/cpython/Lib/sre_parse.py", line 797, in parse
        p = _parse_sub(source, pattern, 0)
      File "/home/serhiy/py/cpython/Lib/sre_parse.py", line 387, in _parse_sub
        itemsappend(_parse(source, state))
      File "/home/serhiy/py/cpython/Lib/sre_parse.py", line 767, in _parse
        code = _escape(source, this, state)
      File "/home/serhiy/py/cpython/Lib/sre_parse.py", line 378, in _escape
        raise source.error("bogus escape: %s" % repr(escape), len(escape))
    sre_constants.error: bogus escape: '\\u123' at position 3
    >>> re.compile("""
    ...     (?x)
    ...     .++
    ... """)
    Traceback (most recent call last):
      File "<stdin>", line 4, in <module>
      File "/home/serhiy/py/cpython/Lib/re.py", line 220, in compile
        return _compile(pattern, flags)
      File "/home/serhiy/py/cpython/Lib/re.py", line 287, in _compile
        p = sre_compile.compile(pattern, flags)
      File "/home/serhiy/py/cpython/Lib/sre_compile.py", line 465, in compile
        p = sre_parse.parse(p, flags)
      File "/home/serhiy/py/cpython/Lib/sre_parse.py", line 797, in parse
        p = _parse_sub(source, pattern, 0)
      File "/home/serhiy/py/cpython/Lib/sre_parse.py", line 387, in _parse_sub
        itemsappend(_parse(source, state))
      File "/home/serhiy/py/cpython/Lib/sre_parse.py", line 602, in _parse
        source.tell() - here + len(this))
    sre_constants.error: multiple repeat at position 16 (line 3, column 7)

    See also PEP-473, bpo-19361 and bpo-22364.

    @serhiy-storchaka serhiy-storchaka added topic-regex type-feature A feature request or enhancement labels Oct 8, 2014
    @pitrou pitrou changed the title Add addition attributes to re.error Add additional attributes to re.error Oct 8, 2014
    @pitrou
    Copy link
    Member

    pitrou commented Oct 8, 2014

    Sounds ok, but it would be nice to add some tests.

    @serhiy-storchaka
    Copy link
    Member Author

    Sounds ok, but it would be nice to add some tests.

    Thank you. Here is a patch with added test.

    @mrabarnett
    Copy link
    Mannequin

    mrabarnett mannequin commented Oct 8, 2014

    I prefer to include the line and column numbers if it's a multi-line pattern, not just if the line number is > 1.

    BTW, it's shorter if you do this:

        self.colno = pos - pattern.rfind(newline, 0, pos)

    If there's no newline, .rfind will return -1.

    @serhiy-storchaka
    Copy link
    Member Author

    Thank you Matthew for your suggestions. Here is updated patch.

    @serhiy-storchaka
    Copy link
    Member Author

    Synchronized with the tip after bpo-19380 changes.

    @ezio-melotti
    Copy link
    Member

    LGTM.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 10, 2014

    New changeset 292c4d853662 by Serhiy Storchaka in branch 'default':
    Issue bpo-22578: Added attributes to the re.error class.
    https://hg.python.org/cpython/rev/292c4d853662

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 10, 2014

    New changeset 07f082b200a7 by Serhiy Storchaka in branch 'default':
    Fixed IDLE tests after changing re error messages (issue bpo-22578).
    https://hg.python.org/cpython/rev/07f082b200a7

    @serhiy-storchaka
    Copy link
    Member Author

    Thank you Ezio for your review.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 1, 2014

    New changeset 9711c60e3049 by Serhiy Storchaka in branch 'default':
    Removed unused function linecol() (added in issue bpo-22578 by mistake).
    https://hg.python.org/cpython/rev/9711c60e3049

    @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
    topic-regex type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants