This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add additional attributes to re.error
Type: enhancement Stage: resolved
Components: Regular Expressions Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: ezio.melotti, mrabarnett, pitrou, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-10-08 12:45 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
re_error_attrs.patch serhiy.storchaka, 2014-10-08 12:45 review
re_error_attrs2.patch serhiy.storchaka, 2014-10-08 14:23 review
re_error_attrs3.patch serhiy.storchaka, 2014-10-09 13:32 review
re_error_attrs4.patch serhiy.storchaka, 2014-10-10 08:51 review
Messages (11)
msg228787 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-08 12:45
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, issue19361 and issue22364.
msg228788 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-08 13:16
Sounds ok, but it would be nice to add some tests.
msg228793 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-08 14:23
> Sounds ok, but it would be nice to add some tests.

Thank you. Here is a patch with added test.
msg228820 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2014-10-08 23:01
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.
msg228874 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-09 13:32
Thank you Matthew for your suggestions. Here is updated patch.
msg228972 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-10 08:51
Synchronized with the tip after issue19380 changes.
msg230953 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-11-10 11:09
LGTM.
msg230956 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-11-10 11:51
New changeset 292c4d853662 by Serhiy Storchaka in branch 'default':
Issue #22578: Added attributes to the re.error class.
https://hg.python.org/cpython/rev/292c4d853662
msg230959 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-11-10 12:35
New changeset 07f082b200a7 by Serhiy Storchaka in branch 'default':
Fixed IDLE tests after changing re error messages (issue #22578).
https://hg.python.org/cpython/rev/07f082b200a7
msg230960 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-10 12:43
Thank you Ezio for your review.
msg231920 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-12-01 09:27
New changeset 9711c60e3049 by Serhiy Storchaka in branch 'default':
Removed unused function linecol() (added in issue #22578 by mistake).
https://hg.python.org/cpython/rev/9711c60e3049
History
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66768
2016-10-16 09:02:54serhiy.storchakalinkissue16007 superseder
2014-12-01 09:27:20python-devsetmessages: + msg231920
2014-11-10 12:43:24serhiy.storchakasetstatus: open -> closed
messages: + msg230960

assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2014-11-10 12:35:51python-devsetmessages: + msg230959
2014-11-10 11:51:17python-devsetnosy: + python-dev
messages: + msg230956
2014-11-10 11:09:48ezio.melottisetmessages: + msg230953
2014-11-02 15:07:52serhiy.storchakalinkissue22364 dependencies
2014-10-10 08:51:49serhiy.storchakasetfiles: + re_error_attrs4.patch

messages: + msg228972
2014-10-09 13:32:16serhiy.storchakasetfiles: + re_error_attrs3.patch

messages: + msg228874
2014-10-08 23:01:42mrabarnettsetmessages: + msg228820
2014-10-08 14:23:29serhiy.storchakasetfiles: + re_error_attrs2.patch

messages: + msg228793
2014-10-08 13:16:04pitrousetmessages: + msg228788
2014-10-08 13:11:05pitrousettitle: Add addition attributes to re.error -> Add additional attributes to re.error
2014-10-08 12:46:15serhiy.storchakasetversions: + Python 3.5
nosy: + pitrou, ezio.melotti, mrabarnett

components: + Regular Expressions
type: enhancement
stage: patch review
2014-10-08 12:45:10serhiy.storchakacreate