Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/src/output/exceptions.py
1398 views
1
# vim:fileencoding=utf-8
2
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3
from __python__ import hash_literals
4
5
from output.statements import print_bracketed
6
7
8
def print_try(self, output):
9
else_var_name = None
10
11
def update_output_var(output):
12
output.indent(), output.assign(else_var_name), output.print(
13
'true'), output.end_statement()
14
15
if self.belse:
16
else_var_name = output.new_try_else_counter()
17
output.assign('var ' + else_var_name), output.print(
18
'false'), output.end_statement(), output.indent()
19
output.print("try")
20
output.space()
21
print_bracketed(self, output, False, None, None,
22
update_output_var if else_var_name else None)
23
if self.bcatch:
24
output.space()
25
print_catch(self.bcatch, output)
26
27
if self.bfinally:
28
output.space()
29
print_finally(self.bfinally, output, self.belse, else_var_name)
30
elif self.belse:
31
output.newline()
32
print_else(self.belse, else_var_name, output)
33
34
35
def print_catch(self, output):
36
output.print("catch")
37
output.space()
38
output.with_parens(lambda: output.print("ρσ_Exception"))
39
output.space()
40
41
def f_exception():
42
output.indent()
43
output.spaced('ρσ_last_exception', '=',
44
'ρσ_Exception'), output.end_statement()
45
output.indent()
46
no_default = True
47
for i, exception in enumerate(self.body):
48
if i:
49
output.print("else ")
50
51
if exception.errors.length:
52
output.print("if")
53
output.space()
54
55
def f_errors():
56
for i, err in enumerate(exception.errors):
57
if i:
58
output.newline()
59
output.indent()
60
output.print("||")
61
output.space()
62
63
output.print("ρσ_Exception")
64
output.space()
65
output.print("instanceof")
66
output.space()
67
if err.name is 'Exception':
68
output.print('Error')
69
else:
70
err.print(output)
71
72
output.with_parens(f_errors)
73
output.space()
74
else:
75
no_default = False
76
print_bracketed(exception, output, True)
77
output.space()
78
if no_default:
79
output.print("else")
80
output.space()
81
82
def f_throw():
83
output.indent()
84
output.print("throw")
85
output.space()
86
output.print("ρσ_Exception")
87
output.semicolon()
88
output.newline()
89
90
output.with_block(f_throw)
91
output.newline()
92
93
output.with_block(f_exception)
94
95
96
def print_finally(self, output, belse, else_var_name):
97
output.print("finally")
98
output.space()
99
if else_var_name:
100
101
def f_try():
102
output.indent(), output.print("try")
103
output.space()
104
output.with_block(lambda: print_else(belse, else_var_name, output))
105
print_finally(self, output)
106
107
output.with_block(f_try)
108
else:
109
print_bracketed(self, output)
110
111
112
def print_else(self, else_var_name, output):
113
output.indent(), output.spaced('if', '(' + else_var_name + ')')
114
output.space()
115
print_bracketed(self, output)
116
117