Skip to content

exceptions

Module for Exceptions.

Classes:

CatchHandlerStmt

CatchHandlerStmt(body: Block[AST], name: Optional[Identifier] = None, types: Optional[Iterable[Identifier] | ASTNodes[Identifier]] = None, parent: Optional[ASTNodes] = None, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: StatementType

AST class for catch statements.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/exceptions.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def __init__(
    self,
    body: Block[AST],
    name: Optional[Identifier] = None,
    types: Optional[Iterable[Identifier] | ASTNodes[Identifier]] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the instance."""
    super().__init__(loc=loc, parent=parent)
    self.body = body
    self.name = name

    if types:
        if isinstance(types, ASTNodes):
            self.types = types
        else:
            self.types = ASTNodes[Identifier]()
            for t in types:
                self.types.append(t)
    else:
        self.types = None

    self.kind = ASTKind.CatchHandlerStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/exceptions.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "CATCH-HANDLER-STMT"
    body_dict = {"body": self.body.get_struct(simplified)}
    name_dict = (
        {"name": self.name.get_struct(simplified)} if self.name else {}
    )
    types_dict = (
        {"types": self.types.get_struct(simplified)} if self.types else {}
    )

    value: DictDataTypesStruct = {
        **cast(DictDataTypesStruct, body_dict),
        **cast(DictDataTypesStruct, name_dict),
        **cast(DictDataTypesStruct, types_dict),
    }
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
294
295
296
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
288
289
290
291
292
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

ExceptionHandlerStmt

ExceptionHandlerStmt(body: Block[AST], handlers: Iterable[CatchHandlerStmt] | ASTNodes[CatchHandlerStmt], finally_handler: Optional[FinallyHandlerStmt] = None, parent: Optional[ASTNodes] = None, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: StatementType

AST class for try statements.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/exceptions.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def __init__(
    self,
    body: Block[AST],
    handlers: Iterable[CatchHandlerStmt] | ASTNodes[CatchHandlerStmt],
    finally_handler: Optional[FinallyHandlerStmt] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the instance."""
    super().__init__(loc=loc, parent=parent)
    self.body = body

    if isinstance(handlers, ASTNodes):
        self.handlers = handlers
    else:
        self.handlers = ASTNodes[CatchHandlerStmt]()
        for h in handlers:
            self.handlers.append(h)

    self.finally_handler = finally_handler

    self.kind = ASTKind.ExceptionHandlerStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/exceptions.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "EXCEPTION-HANDLER-STMT"

    body_dict = {"body": self.body.get_struct(simplified)}
    handlers_dict = {"handlers": self.handlers.get_struct(simplified)}
    finally_dict = (
        {"finally_handler": self.finally_handler.get_struct(simplified)}
        if self.finally_handler
        else {}
    )

    value: DictDataTypesStruct = {
        **cast(DictDataTypesStruct, body_dict),
        **cast(DictDataTypesStruct, handlers_dict),
        **cast(DictDataTypesStruct, finally_dict),
    }
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
294
295
296
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
288
289
290
291
292
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

FinallyHandlerStmt

FinallyHandlerStmt(body: Block[AST], parent: Optional[ASTNodes] = None, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: StatementType

AST class for finally statements.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/exceptions.py
177
178
179
180
181
182
183
184
185
186
def __init__(
    self,
    body: Block[AST],
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the instance."""
    super().__init__(loc=loc, parent=parent)
    self.body = body
    self.kind = ASTKind.FinallyHandlerStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/exceptions.py
192
193
194
195
196
197
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "FINALLY-STMT"
    value: DictDataTypesStruct = {"body": self.body.get_struct(simplified)}

    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
294
295
296
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
288
289
290
291
292
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

ThrowStmt

ThrowStmt(exception: Optional[Expr] = None, parent: Optional[ASTNodes] = None, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: StatementType

AST class for throw statements.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/exceptions.py
32
33
34
35
36
37
38
39
40
41
def __init__(
    self,
    exception: Optional[Expr] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the instance."""
    super().__init__(loc=loc, parent=parent)
    self.exception = exception
    self.kind = ASTKind.ThrowStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/exceptions.py
50
51
52
53
54
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "THROW-STMT"
    value = self.exception.get_struct(simplified) if self.exception else ""
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
294
295
296
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
288
289
290
291
292
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )