-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Comments
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) |
Sounds ok, but it would be nice to add some tests. |
Thank you. Here is a patch with added test. |
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. |
Thank you Matthew for your suggestions. Here is updated patch. |
Synchronized with the tip after bpo-19380 changes. |
LGTM. |
New changeset 292c4d853662 by Serhiy Storchaka in branch 'default': |
New changeset 07f082b200a7 by Serhiy Storchaka in branch 'default': |
Thank you Ezio for your review. |
New changeset 9711c60e3049 by Serhiy Storchaka in branch 'default': |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: