| 
5 | 5 | from functools import wraps  | 
6 | 6 | import gzip  | 
7 | 7 | import os  | 
8 |  | -import re  | 
9 | 8 | from shutil import rmtree  | 
10 | 9 | import string  | 
11 | 10 | import tempfile  | 
 | 
23 | 22 | )  | 
24 | 23 | 
 
  | 
25 | 24 | import pandas._libs.testing as _testing  | 
26 |  | -from pandas.compat import _get_lzma_file, _import_lzma, raise_with_traceback  | 
 | 25 | +from pandas.compat import _get_lzma_file, _import_lzma  | 
27 | 26 | 
 
  | 
28 | 27 | from pandas.core.dtypes.common import (  | 
29 | 28 |     is_bool,  | 
@@ -2404,143 +2403,6 @@ def wrapper(*args, **kwargs):  | 
2404 | 2403 | with_connectivity_check = network  | 
2405 | 2404 | 
 
  | 
2406 | 2405 | 
 
  | 
2407 |  | -def assert_raises_regex(_exception, _regexp, _callable=None, *args, **kwargs):  | 
2408 |  | -    r"""  | 
2409 |  | -    Check that the specified Exception is raised and that the error message  | 
2410 |  | -    matches a given regular expression pattern. This may be a regular  | 
2411 |  | -    expression object or a string containing a regular expression suitable  | 
2412 |  | -    for use by `re.search()`. This is a port of the `assertRaisesRegexp`  | 
2413 |  | -    function from unittest in Python 2.7.  | 
2414 |  | -
  | 
2415 |  | -    .. deprecated:: 0.24.0  | 
2416 |  | -        Use `pytest.raises` instead.  | 
2417 |  | -
  | 
2418 |  | -    Examples  | 
2419 |  | -    --------  | 
2420 |  | -    >>> assert_raises_regex(ValueError, 'invalid literal for.*XYZ', int, 'XYZ')  | 
2421 |  | -    >>> import re  | 
2422 |  | -    >>> assert_raises_regex(ValueError, re.compile('literal'), int, 'XYZ')  | 
2423 |  | -
  | 
2424 |  | -    If an exception of a different type is raised, it bubbles up.  | 
2425 |  | -
  | 
2426 |  | -    >>> assert_raises_regex(TypeError, 'literal', int, 'XYZ')  | 
2427 |  | -    Traceback (most recent call last):  | 
2428 |  | -        ...  | 
2429 |  | -    ValueError: invalid literal for int() with base 10: 'XYZ'  | 
2430 |  | -    >>> dct = dict()  | 
2431 |  | -    >>> assert_raises_regex(KeyError, 'pear', dct.__getitem__, 'apple')  | 
2432 |  | -    Traceback (most recent call last):  | 
2433 |  | -        ...  | 
2434 |  | -    AssertionError: "pear" does not match "'apple'"  | 
2435 |  | -
  | 
2436 |  | -    You can also use this in a with statement.  | 
2437 |  | -
  | 
2438 |  | -    >>> with assert_raises_regex(TypeError, r'unsupported operand type\(s\)'):  | 
2439 |  | -    ...     1 + {}  | 
2440 |  | -    >>> with assert_raises_regex(TypeError, 'banana'):  | 
2441 |  | -    ...     'apple'[0] = 'b'  | 
2442 |  | -    Traceback (most recent call last):  | 
2443 |  | -        ...  | 
2444 |  | -    AssertionError: "banana" does not match "'str' object does not support \  | 
2445 |  | -item assignment"  | 
2446 |  | -    """  | 
2447 |  | -    warnings.warn(  | 
2448 |  | -        (  | 
2449 |  | -            "assert_raises_regex has been deprecated and will "  | 
2450 |  | -            "be removed in the next release. Please use "  | 
2451 |  | -            "`pytest.raises` instead."  | 
2452 |  | -        ),  | 
2453 |  | -        FutureWarning,  | 
2454 |  | -        stacklevel=2,  | 
2455 |  | -    )  | 
2456 |  | - | 
2457 |  | -    manager = _AssertRaisesContextmanager(exception=_exception, regexp=_regexp)  | 
2458 |  | -    if _callable is not None:  | 
2459 |  | -        with manager:  | 
2460 |  | -            _callable(*args, **kwargs)  | 
2461 |  | -    else:  | 
2462 |  | -        return manager  | 
2463 |  | - | 
2464 |  | - | 
2465 |  | -class _AssertRaisesContextmanager:  | 
2466 |  | -    """  | 
2467 |  | -    Context manager behind `assert_raises_regex`.  | 
2468 |  | -    """  | 
2469 |  | - | 
2470 |  | -    def __init__(self, exception, regexp=None):  | 
2471 |  | -        """  | 
2472 |  | -        Initialize an _AssertRaisesContextManager instance.  | 
2473 |  | -
  | 
2474 |  | -        Parameters  | 
2475 |  | -        ----------  | 
2476 |  | -        exception : class  | 
2477 |  | -            The expected Exception class.  | 
2478 |  | -        regexp : str, default None  | 
2479 |  | -            The regex to compare against the Exception message.  | 
2480 |  | -        """  | 
2481 |  | - | 
2482 |  | -        self.exception = exception  | 
2483 |  | - | 
2484 |  | -        if regexp is not None and not hasattr(regexp, "search"):  | 
2485 |  | -            regexp = re.compile(regexp, re.DOTALL)  | 
2486 |  | - | 
2487 |  | -        self.regexp = regexp  | 
2488 |  | - | 
2489 |  | -    def __enter__(self):  | 
2490 |  | -        return self  | 
2491 |  | - | 
2492 |  | -    def __exit__(self, exc_type, exc_value, trace_back):  | 
2493 |  | -        expected = self.exception  | 
2494 |  | - | 
2495 |  | -        if not exc_type:  | 
2496 |  | -            exp_name = getattr(expected, "__name__", str(expected))  | 
2497 |  | -            raise AssertionError("{name} not raised.".format(name=exp_name))  | 
2498 |  | - | 
2499 |  | -        return self.exception_matches(exc_type, exc_value, trace_back)  | 
2500 |  | - | 
2501 |  | -    def exception_matches(self, exc_type, exc_value, trace_back):  | 
2502 |  | -        """  | 
2503 |  | -        Check that the Exception raised matches the expected Exception  | 
2504 |  | -        and expected error message regular expression.  | 
2505 |  | -
  | 
2506 |  | -        Parameters  | 
2507 |  | -        ----------  | 
2508 |  | -        exc_type : class  | 
2509 |  | -            The type of Exception raised.  | 
2510 |  | -        exc_value : Exception  | 
2511 |  | -            The instance of `exc_type` raised.  | 
2512 |  | -        trace_back : stack trace object  | 
2513 |  | -            The traceback object associated with `exc_value`.  | 
2514 |  | -
  | 
2515 |  | -        Returns  | 
2516 |  | -        -------  | 
2517 |  | -        is_matched : bool  | 
2518 |  | -            Whether or not the Exception raised matches the expected  | 
2519 |  | -            Exception class and expected error message regular expression.  | 
2520 |  | -
  | 
2521 |  | -        Raises  | 
2522 |  | -        ------  | 
2523 |  | -        AssertionError : The error message provided does not match  | 
2524 |  | -                         the expected error message regular expression.  | 
2525 |  | -        """  | 
2526 |  | - | 
2527 |  | -        if issubclass(exc_type, self.exception):  | 
2528 |  | -            if self.regexp is not None:  | 
2529 |  | -                val = str(exc_value)  | 
2530 |  | - | 
2531 |  | -                if not self.regexp.search(val):  | 
2532 |  | -                    msg = '"{pat}" does not match "{val}"'.format(  | 
2533 |  | -                        pat=self.regexp.pattern, val=val  | 
2534 |  | -                    )  | 
2535 |  | -                    e = AssertionError(msg)  | 
2536 |  | -                    raise_with_traceback(e, trace_back)  | 
2537 |  | - | 
2538 |  | -            return True  | 
2539 |  | -        else:  | 
2540 |  | -            # Failed, so allow Exception to bubble up.  | 
2541 |  | -            return False  | 
2542 |  | - | 
2543 |  | - | 
2544 | 2406 | @contextmanager  | 
2545 | 2407 | def assert_produces_warning(  | 
2546 | 2408 |     expected_warning=Warning,  | 
 | 
0 commit comments