-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fixed handling of invalid regex pattern #12526
Fixed handling of invalid regex pattern #12526
Conversation
for more information, see https://pre-commit.ci
… fix-invalid-regex-handling
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
This definitely makes things a bit nicer: test_raises.py:3: in <module>
raise ValueError()
E ValueError
During handling of the above exception, another exception occurred:
/usr/lib/python3.12/re/__init__.py:177: in search
return _compile(pattern, flags).search(string)
/usr/lib/python3.12/re/__init__.py:307: in _compile
p = _compiler.compile(pattern, flags)
/usr/lib/python3.12/re/_compiler.py:745: in compile
p = _parser.parse(p, flags)
/usr/lib/python3.12/re/_parser.py:979: in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
/usr/lib/python3.12/re/_parser.py:460: in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
/usr/lib/python3.12/re/_parser.py:568: in _parse
raise source.error("unterminated character set",
E re.error: unterminated character set at position 24
During handling of the above exception, another exception occurred:
test_raises.py:2: in <module>
with pytest.raises(ValueError, match="invalid regex character ["):
E Failed: Invalid regex pattern provided to 'match': unterminated character set at position 24 But it's still pretty verbose due to the chaining... This could be improved by using test_raises.py:2: in <module>
with pytest.raises(ValueError, match="invalid regex character ["):
E Failed: Invalid regex pattern provided to 'match': unterminated character set at position 24 though we don't really do this kind of thing elsewhere so far. I'd wait to see what others think before proceeding with that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the improvement.
The proposed solution of catching the exception in __exit__
has the disadvantage that the fail
is raised from the __exit__
(and is thus chained to the exception under test), and is not checked at all in the "did not raise" case. I therefore think it'd be better to check it eagerly in the __init__
.
I also agree with @The-Compiler. We can use fail.Exception
, but another way is to just make sure to call fail
outside of the except
block, something like:
re_error = None
try:
self.excinfo.match(self.match_expr)
except re.error as e:
re_error = e
if re_error is not None:
fail(f"Invalid regex pattern provided to 'match': {e}")
a little verbose, but OK.
I have added the changes you asked for, please let me know if anything else needs to be done. Thanks for the input! |
@virendrapatil24 The proposed solution of catching the exception in |
@bluetech So I need to compile the regex pattern in the |
Right, that would be a good way to do it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good to me!
closes #12505
RaisesContext
class insrc/_pytest/python_api.py
to catchre.error
and callpytest.fail
with a clear error message.testing/python/raises.py
to validate the new behavior.