@@ -2500,40 +2500,79 @@ def assertRaisesRegexp(_exception, _regexp, _callable=None, *args, **kwargs):
25002500
25012501class _AssertRaisesContextmanager (object ):
25022502 """
2503- Handles the behind the scenes work
2504- for assertRaises and assertRaisesRegexp
2503+ Context manager behind assertRaisesRegexp.
25052504 """
25062505
2507- def __init__ (self , exception , regexp = None , * args , ** kwargs ):
2506+ def __init__ (self , exception , regexp = None ):
2507+ """
2508+ Initialize an _AssertRaisesContextManager instance.
2509+
2510+ Parameters
2511+ ----------
2512+ exception : class
2513+ The expected Exception class.
2514+ regexp : str, default None
2515+ The regex to compare against the Exception message.
2516+ """
2517+
25082518 self .exception = exception
2519+
25092520 if regexp is not None and not hasattr (regexp , "search" ):
25102521 regexp = re .compile (regexp , re .DOTALL )
2522+
25112523 self .regexp = regexp
25122524
25132525 def __enter__ (self ):
25142526 return self
25152527
2516- def __exit__ (self , exc_type , exc_value , traceback ):
2528+ def __exit__ (self , exc_type , exc_value , trace_back ):
25172529 expected = self .exception
2518- if not exc_type :
2519- name = getattr (expected , "__name__" , str (expected ))
2520- raise AssertionError ("{0} not raised." .format (name ))
2521- if issubclass (exc_type , expected ):
2522- return self .handle_success (exc_type , exc_value , traceback )
2523- return self .handle_failure (exc_type , exc_value , traceback )
2524-
2525- def handle_failure (* args , ** kwargs ):
2526- # Failed, so allow Exception to bubble up
2527- return False
25282530
2529- def handle_success (self , exc_type , exc_value , traceback ):
2530- if self .regexp is not None :
2531- val = str (exc_value )
2532- if not self .regexp .search (val ):
2533- e = AssertionError ('"%s" does not match "%s"' %
2534- (self .regexp .pattern , str (val )))
2535- raise_with_traceback (e , traceback )
2536- return True
2531+ if not exc_type :
2532+ exp_name = getattr (expected , "__name__" , str (expected ))
2533+ raise AssertionError ("{0} not raised." .format (exp_name ))
2534+
2535+ return self .exception_matches (exc_type , exc_value , trace_back )
2536+
2537+ def exception_matches (self , exc_type , exc_value , trace_back ):
2538+ """
2539+ Check that the Exception raised matches the expected Exception
2540+ and expected error message regular expression.
2541+
2542+ Parameters
2543+ ----------
2544+ exc_type : class
2545+ The type of Exception raised.
2546+ exc_value : Exception
2547+ The instance of `exc_type` raised.
2548+ trace_back : stack trace object
2549+ The traceback object associated with `exc_value`.
2550+
2551+ Returns
2552+ -------
2553+ is_matched : bool
2554+ Whether or not the Exception raised matches the expected
2555+ Exception class and expected error message regular expression.
2556+
2557+ Raises
2558+ ------
2559+ AssertionError : The error message provided does not match
2560+ the expected error message regular expression.
2561+ """
2562+
2563+ if issubclass (exc_type , self .exception ):
2564+ if self .regexp is not None :
2565+ val = str (exc_value )
2566+
2567+ if not self .regexp .search (val ):
2568+ e = AssertionError ('"%s" does not match "%s"' %
2569+ (self .regexp .pattern , str (val )))
2570+ raise_with_traceback (e , trace_back )
2571+
2572+ return True
2573+ else :
2574+ # Failed, so allow Exception to bubble up.
2575+ return False
25372576
25382577
25392578@contextmanager
0 commit comments