summaryrefslogtreecommitdiff
path: root/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_raised.py
blob: aa37a45306edbf35d3f9997eb5d30122f91644f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# pylint:disable=too-few-public-methods,import-error,missing-docstring, not-callable, import-outside-toplevel
"""test pb with exceptions and old/new style classes"""


class ValidException(Exception):
    """Valid Exception."""

class OldStyleClass:
    """Not an exception."""

class NewStyleClass:
    """Not an exception."""


def good_case():
    """raise"""
    raise ValidException('hop')

def good_case1():
    """zlib.error is defined in C module."""
    import zlib
    raise zlib.error(4)

def good_case2():
    """decimal.DivisionByZero is defined in C on Python 3."""
    import decimal
    raise decimal.DivisionByZero(4)

def good_case3():
    """io.BlockingIOError is defined in C."""
    import io
    raise io.BlockingIOError

def bad_case0():
    """raise"""
    # +2:<3.0:[nonstandard-exception]
    # +1:>=3.0:[raising-non-exception]
    raise OldStyleClass('hop')

def bad_case1():
    """raise"""
    raise NewStyleClass()  # [raising-non-exception]

def bad_case2():
    """raise"""
    # +2:<3.0:[nonstandard-exception]
    # +1:>=3.0:[raising-non-exception]
    raise OldStyleClass('hop')

def bad_case3():
    """raise"""
    raise NewStyleClass  # [raising-non-exception]

def bad_case4():
    """raise"""
    raise NotImplemented('hop')  # [notimplemented-raised]

def bad_case5():
    """raise"""
    raise 1  # [raising-bad-type]

def bad_case6():
    """raise"""
    raise None  # [raising-bad-type]

def bad_case7():
    """raise list"""
    raise list # [raising-non-exception]

def bad_case8():
    """raise tuple"""
    raise tuple # [raising-non-exception]

def bad_case9():
    """raise dict"""
    raise dict # [raising-non-exception]

def unknown_bases():
    """Don't emit when we don't know the bases."""
    from lala import bala  # pylint: disable=import-outside-toplevel
    class MyException(bala):
        pass
    raise MyException


def exception_instance_regression():
    """Exceptions have a particular class type"""
    try:
        int("9a")
    except ValueError as exc:
        raise exc


def reusing_same_name_picks_the_latest_raised_value():
    class Error(Exception):
        """some error"""

    exceptions = tuple([ValueError, TypeError])
    try:
        raise ValueError
    except exceptions as exc:  # pylint: disable=catching-non-exception
        # https://github.com/pylint-dev/pylint/issues/1756
        exc = Error(exc)
        if exc:
            raise exc


def bad_case10():
    """raise string"""
    raise "string"  # [raising-bad-type]


class AmbiguousValue:
    """Don't emit when there is ambiguity on the node for the exception."""
    def __init__(self):
        self.stored_exception = None

    def fail(self):
        try:
            1 / 0
        except ZeroDivisionError as zde:
            self.stored_exception = zde

    def raise_stored_exception(self):
        if self.stored_exception is not None:
            exc = self.stored_exception
            self.stored_exception = None
            raise exc