@@ -2500,40 +2500,79 @@ def assertRaisesRegexp(_exception, _regexp, _callable=None, *args, **kwargs):
2500
2500
2501
2501
class _AssertRaisesContextmanager (object ):
2502
2502
"""
2503
- Handles the behind the scenes work
2504
- for assertRaises and assertRaisesRegexp
2503
+ Context manager behind assertRaisesRegexp.
2505
2504
"""
2506
2505
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
+
2508
2518
self .exception = exception
2519
+
2509
2520
if regexp is not None and not hasattr (regexp , "search" ):
2510
2521
regexp = re .compile (regexp , re .DOTALL )
2522
+
2511
2523
self .regexp = regexp
2512
2524
2513
2525
def __enter__ (self ):
2514
2526
return self
2515
2527
2516
- def __exit__ (self , exc_type , exc_value , traceback ):
2528
+ def __exit__ (self , exc_type , exc_value , trace_back ):
2517
2529
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
2528
2530
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
2537
2576
2538
2577
2539
2578
@contextmanager
0 commit comments