@@ -2352,141 +2352,6 @@ def wrapper(*args, **kwargs):
23522352with_connectivity_check = network
23532353
23542354
2355- def assert_raises_regex (_exception , _regexp , _callable = None , * args , ** kwargs ):
2356- r"""
2357- Check that the specified Exception is raised and that the error message
2358- matches a given regular expression pattern. This may be a regular
2359- expression object or a string containing a regular expression suitable
2360- for use by `re.search()`. This is a port of the `assertRaisesRegexp`
2361- function from unittest in Python 2.7.
2362-
2363- .. deprecated:: 0.24.0
2364- Use `pytest.raises` instead.
2365-
2366- Examples
2367- --------
2368- >>> assert_raises_regex(ValueError, 'invalid literal for.*XYZ', int, 'XYZ')
2369- >>> import re
2370- >>> assert_raises_regex(ValueError, re.compile('literal'), int, 'XYZ')
2371-
2372- If an exception of a different type is raised, it bubbles up.
2373-
2374- >>> assert_raises_regex(TypeError, 'literal', int, 'XYZ')
2375- Traceback (most recent call last):
2376- ...
2377- ValueError: invalid literal for int() with base 10: 'XYZ'
2378- >>> dct = dict()
2379- >>> assert_raises_regex(KeyError, 'pear', dct.__getitem__, 'apple')
2380- Traceback (most recent call last):
2381- ...
2382- AssertionError: "pear" does not match "'apple'"
2383-
2384- You can also use this in a with statement.
2385-
2386- >>> with assert_raises_regex(TypeError, r'unsupported operand type\(s\)'):
2387- ... 1 + {}
2388- >>> with assert_raises_regex(TypeError, 'banana'):
2389- ... 'apple'[0] = 'b'
2390- Traceback (most recent call last):
2391- ...
2392- AssertionError: "banana" does not match "'str' object does not support \
2393- item assignment"
2394- """
2395- warnings .warn (
2396- (
2397- "assert_raises_regex has been deprecated and will "
2398- "be removed in the next release. Please use "
2399- "`pytest.raises` instead."
2400- ),
2401- FutureWarning ,
2402- stacklevel = 2 ,
2403- )
2404-
2405- manager = _AssertRaisesContextmanager (exception = _exception , regexp = _regexp )
2406- if _callable is not None :
2407- with manager :
2408- _callable (* args , ** kwargs )
2409- else :
2410- return manager
2411-
2412-
2413- class _AssertRaisesContextmanager :
2414- """
2415- Context manager behind `assert_raises_regex`.
2416- """
2417-
2418- def __init__ (self , exception , regexp = None ):
2419- """
2420- Initialize an _AssertRaisesContextManager instance.
2421-
2422- Parameters
2423- ----------
2424- exception : class
2425- The expected Exception class.
2426- regexp : str, default None
2427- The regex to compare against the Exception message.
2428- """
2429-
2430- self .exception = exception
2431-
2432- if regexp is not None and not hasattr (regexp , "search" ):
2433- regexp = re .compile (regexp , re .DOTALL )
2434-
2435- self .regexp = regexp
2436-
2437- def __enter__ (self ):
2438- return self
2439-
2440- def __exit__ (self , exc_type , exc_value , trace_back ):
2441- expected = self .exception
2442-
2443- if not exc_type :
2444- exp_name = getattr (expected , "__name__" , str (expected ))
2445- raise AssertionError (f"{ exp_name } not raised." )
2446-
2447- return self .exception_matches (exc_type , exc_value , trace_back )
2448-
2449- def exception_matches (self , exc_type , exc_value , trace_back ):
2450- """
2451- Check that the Exception raised matches the expected Exception
2452- and expected error message regular expression.
2453-
2454- Parameters
2455- ----------
2456- exc_type : class
2457- The type of Exception raised.
2458- exc_value : Exception
2459- The instance of `exc_type` raised.
2460- trace_back : stack trace object
2461- The traceback object associated with `exc_value`.
2462-
2463- Returns
2464- -------
2465- is_matched : bool
2466- Whether or not the Exception raised matches the expected
2467- Exception class and expected error message regular expression.
2468-
2469- Raises
2470- ------
2471- AssertionError : The error message provided does not match
2472- the expected error message regular expression.
2473- """
2474-
2475- if issubclass (exc_type , self .exception ):
2476- if self .regexp is not None :
2477- val = str (exc_value )
2478-
2479- if not self .regexp .search (val ):
2480- msg = f'"{ self .regexp .pattern } " does not match "{ val } "'
2481- e = AssertionError (msg )
2482- raise_with_traceback (e , trace_back )
2483-
2484- return True
2485- else :
2486- # Failed, so allow Exception to bubble up.
2487- return False
2488-
2489-
24902355@contextmanager
24912356def assert_produces_warning (
24922357 expected_warning = Warning ,
0 commit comments