Esempio n. 1
0
def test_raise_from_none():
    try:
        raise_from(IOError, None)
        assert False
    except IOError:
        exc, exc_tb = sys.exc_info()[1:]

    assert exc.__suppress_context__
    assert exc.__context__ is None
    assert exc.__cause__ is None
Esempio n. 2
0
def raise_for_float_problem(suspicious, scheme):
    if not isinstance(suspicious, float):
        cause = ValueError("Should be a float")
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, cause)

    if abs(suspicious - scheme) >= sys.float_info.epsilon:
        cause = ValueError("Should be {0}".format(scheme))
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, cause)
Esempio n. 3
0
    def test_string_itisbullshiterror(self):
        error_to_check = ValueError("KeyError")
        for line in ("3 line", "2 line", "4 line"):
            try:
                current_error = ItIsBullshitError(line)
                pep3134.raise_from(current_error, error_to_check)
            except ItIsBullshitError as err:
                error_to_check = err

        assert list(error_to_check) == ['4 line', '2 line', '3 line',
                                        'KeyError']
Esempio n. 4
0
def raise_for_string_problem(suspicious, scheme):
    if not isinstance(suspicious, string_types):
        cause = ValueError("Should be a string")
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, cause)

    # noinspection PyTypeChecker
    if re.match(scheme, suspicious, re.UNICODE) is None:
        cause = ValueError("Regex mismatch: {0}".format(scheme))
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, cause)
    def test_string_itisbullshiterror(self):
        error_to_check = ValueError("KeyError")
        for line in ("3 line", "2 line", "4 line"):
            try:
                current_error = ItIsBullshitError(line)
                pep3134.raise_from(current_error, error_to_check)
            except ItIsBullshitError as err:
                error_to_check = err

        assert list(error_to_check) == [
            '4 line', '2 line', '3 line', 'KeyError'
        ]
Esempio n. 6
0
def test_reraise():
    try:
        raise_from(IOError, KeyError)
        assert False
    except IOError:
        try:
            reraise()
        except IOError:
            reraised, reraised_tb = sys.exc_info()[1:]

        assert not reraised.__suppress_context__
        assert reraised.__cause__ is None
Esempio n. 7
0
def raise_for_tuple_problem(suspicious, scheme):
    if not scheme:
        raise TypeError("Tuple scheme should contain at least 1 element")

    error = None
    for type_validator in scheme:
        try:
            raise_for_problem(suspicious, type_validator)
            break
        except ItIsBullshitError as err:
            error = err
    else:
        if error is not None:
            final_error = ItIsBullshitError(suspicious)
            pep3134.raise_from(final_error, error)
Esempio n. 8
0
def test_raise_from_proxy_exc():
    try:
        raise_(TypeError, "OK")
        assert False
    except TypeError:
        cause, cause_tb = sys.exc_info()[1:]

    try:
        raise_from(IOError, cause)
    except IOError:
        exc, exc_tb = sys.exc_info()[1:]

    assert exc.__suppress_context__
    assert exc.__context__ is None
    assert exc.__cause__ is cause
    assert exc.__traceback__ is exc_tb
    assert exc.__cause__.__traceback__ is cause_tb
Esempio n. 9
0
def raise_for_list_problem(suspicious, scheme):
    if not scheme:
        raise TypeError("List scheme should contain at least 1 element")

    if len(scheme) != 1:
        raise TypeError("List scheme should contain only 1 element")

    if not isinstance(suspicious, (tuple, list)):
        cause = ValueError("Type mismatch, should be a list or a tuple")
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, cause)

    for item in suspicious:
        try:
            raise_for_problem(item, scheme[0])
        except ItIsBullshitError as err:
            error = ItIsBullshitError(suspicious)
            pep3134.raise_from(error, err)
Esempio n. 10
0
def test_raise_from_ordinary_exc():
    try:
        raise TypeError("OK")
    except TypeError:
        cause, cause_tb = sys.exc_info()[1:]

    try:
        raise_from(IOError, cause)
        assert False
    except IOError:
        exc, exc_tb = sys.exc_info()[1:]

    if sys.version_info[0] == 2:
        assert exc.__cause__ is not cause
        assert hasattr(exc.__cause__, "__pep3134__")
    else:
        assert exc.__cause__ is cause
    assert exc.__suppress_context__
    assert exc.__context__ is None
    assert exc.__traceback__ is exc_tb
    assert exc.__cause__.__traceback__ is cause_tb
Esempio n. 11
0
def raise_for_dict_problem(suspicious, scheme):
    if not scheme:
        raise TypeError("Dict scheme should contain at least 1 element")

    if not isinstance(suspicious, dict):
        cause = ValueError("Type mismatch, should be a dict")
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, cause)

    for key, validator in iteritems(scheme):
        original_validator = validator
        if isinstance(validator, OrSkipped):
            original_validator = validator.validator

        if key not in suspicious:
            if not isinstance(validator, OrSkipped):
                cause = ValueError("Missed key {0}".format(key))
                error = ItIsBullshitError(suspicious)
                pep3134.raise_from(error, cause)
        else:
            if suspicious[key] is not validator:
                try:
                    raise_for_problem(suspicious[key], original_validator)
                except ItIsBullshitError as err:
                    error = ItIsBullshitError(suspicious)
                    pep3134.raise_from(error, err)
Esempio n. 12
0
def raise_for_problem(suspicious, scheme):
    if isinstance(scheme, OrSkipped):
        raise TypeError("Scheme could not be OrSkipped here")

    if shallow_check(suspicious, scheme):
        return

    if isinstance(scheme, dict):
        raise_for_dict_problem(suspicious, scheme)
    elif isinstance(scheme, list):
        raise_for_list_problem(suspicious, scheme)
    elif isinstance(scheme, tuple):
        raise_for_tuple_problem(suspicious, scheme)
    elif isinstance(scheme, string_types):
        raise_for_string_problem(suspicious, scheme)
    elif isinstance(scheme, float):
        raise_for_float_problem(suspicious, scheme)
    elif isinstance(scheme, FUNC_TYPE):
        raise_for_callable_problem(suspicious, scheme)
    else:
        cause = ValueError("Scheme mismatch {0}".format(scheme))
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, cause)
Esempio n. 13
0
def test_raise_from_fail(input_, cause_):
    with pytest.raises(TypeError):
        raise_from(input_, cause_)
Esempio n. 14
0
def test_raise_from(input_, cause_):
    with pytest.raises(IOError):
        raise_from(input_, cause_)
Esempio n. 15
0
def raise_for_callable_problem(suspicious, validator):
    try:
        validator(suspicious)
    except Exception as err:  # pylint: disable=W0703
        error = ItIsBullshitError(suspicious)
        pep3134.raise_from(error, err)