Skip to content

Commit a4f663e

Browse files
authored
[show/fgnhg] Fix crash bug (sonic-net#1272)
**- What I did** Fix crashes like the following: ``` Traceback (most recent call last): File "/usr/local/bin/show", line 10, in <module> sys.exit(cli()) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 764, in __call__ return self.main(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 717, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 956, in invoke return ctx.invoke(self.callback, **ctx.params) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 555, in invoke return callback(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/show/fgnhg.py", line 56, in active_hops click.echo(tabulate(table, header, tablefmt="grid")) UnboundLocalError: local variable 'header' referenced before assignment ``` **- How I did it** Define table header outside the loop which processes the rows
1 parent 9f276b2 commit a4f663e

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

show/fgnhg.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def active_hops(nhg):
3535
table_keys = []
3636
table_keys = state_db.keys(state_db.STATE_DB, _hash)
3737
t_dict = {}
38+
header = ["FG_NHG_PREFIX", "Active Next Hops"]
3839
table = []
3940
output_dict = {}
4041

@@ -49,7 +50,6 @@ def active_hops(nhg):
4950
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
5051

5152
nhg_prefix_report = (nhg_prefix.split("|")[1])
52-
header = ["FG_NHG_PREFIX", "Active Next Hops"]
5353
formatted_nhps = ','.replace(',', '\n').join(output_dict[nhg_prefix])
5454
table.append([nhg_prefix_report, formatted_nhps])
5555

@@ -80,7 +80,6 @@ def active_hops(nhg):
8080
nhg_prefix_report = (nhg_prefix.split("|")[1])
8181
formatted_nhps = ','.replace(',', '\n').join(output_dict[nhg_prefix])
8282
table.append([nhg_prefix_report, formatted_nhps])
83-
header = ["FG_NHG_PREFIX", "Active Next Hops"]
8483
click.echo(tabulate(table, header, tablefmt="grid"))
8584

8685

@@ -105,6 +104,7 @@ def hash_view(nhg):
105104
table_keys = []
106105
table_keys = state_db.keys(state_db.STATE_DB, _hash)
107106
t_dict = {}
107+
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
108108
table = []
109109
output_dict = {}
110110
bank_dict = {}
@@ -128,7 +128,6 @@ def hash_view(nhg):
128128

129129
bank_dict = OrderedDict(sorted(bank_dict.items()))
130130
nhg_prefix_report = (nhg_prefix.split("|")[1])
131-
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
132131

133132
for nhip, val in bank_dict.items():
134133
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])
@@ -163,7 +162,6 @@ def hash_view(nhg):
163162

164163
nhg_prefix_report = (nhg_prefix.split("|")[1])
165164
bank_dict = OrderedDict(sorted(bank_dict.items()))
166-
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
167165

168166
for nhip, val in bank_dict.items():
169167
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])

0 commit comments

Comments
 (0)