handled invalid regex pattern exception inside init

This commit is contained in:
Virendra Patil 2024-06-27 19:21:17 +05:30
parent 24ec45d881
commit 6e91c332ba
1 changed files with 9 additions and 7 deletions

View File

@ -987,6 +987,14 @@ class RaisesContext(ContextManager[_pytest._code.ExceptionInfo[E]]):
self.message = message
self.match_expr = match_expr
self.excinfo: _pytest._code.ExceptionInfo[E] | None = None
if self.match_expr is not None:
re_error = None
try:
re.compile(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': {re_error}")
def __enter__(self) -> _pytest._code.ExceptionInfo[E]:
self.excinfo = _pytest._code.ExceptionInfo.for_later()
@ -1008,11 +1016,5 @@ class RaisesContext(ContextManager[_pytest._code.ExceptionInfo[E]]):
exc_info = cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb))
self.excinfo.fill_unfilled(exc_info)
if self.match_expr is not None:
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': {re_error}")
self.excinfo.match(self.match_expr)
return True