From 3089c17b5bf0c7563173bd4336fa5b5c04ce636b Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Thu, 27 Jun 2024 00:35:26 +0530 Subject: [PATCH] updated invalid regex pattern handling --- src/_pytest/python_api.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index e3c599e4f..24c6c1c3b 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -1007,9 +1007,12 @@ class RaisesContext(ContextManager[_pytest._code.ExceptionInfo[E]]): # Cast to narrow the exception type now that it's verified. exc_info = cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb)) self.excinfo.fill_unfilled(exc_info) - try: - if self.match_expr is not None: + if self.match_expr is not None: + re_error = None + try: self.excinfo.match(self.match_expr) - except re.error as e: - fail(f"Invalid regex pattern provided to 'match': {e}") + except re.error as e: + re_error = e + if re_error is not None: + fail(f"Invalid regex pattern provided to 'match': {re_error}") return True