Skip to content

Commit 4be708d

Browse files
committed
Merge branch 'tools-ynl-fix-errors-reported-by-ruff'
Matthieu Baerts says: ==================== tools: ynl: fix errors reported by Ruff When looking at the YNL code to add a new feature, my text editor automatically executed 'ruff check', and found out at least one interesting error: one variable was used while not being defined. I then decided to fix this error, and all the other ones reported by Ruff. After this series, 'ruff check' reports no more errors with version 0.12.12. ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 724b22d + f6259ba commit 4be708d

File tree

5 files changed

+25
-31
lines changed

5 files changed

+25
-31
lines changed

tools/net/ynl/pyynl/ethtool.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
33

44
import argparse
5-
import json
65
import pathlib
76
import pprint
87
import sys
@@ -51,7 +50,7 @@ def print_field(reply, *desc):
5150
for spec in desc:
5251
try:
5352
field, name, tp = spec
54-
except:
53+
except ValueError:
5554
field, name = spec
5655
tp = 'int'
5756

@@ -156,7 +155,6 @@ def main():
156155
global args
157156
args = parser.parse_args()
158157

159-
script_abs_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
160158
spec = os.path.join(spec_dir(), 'ethtool.yaml')
161159
schema = os.path.join(schema_dir(), 'genetlink-legacy.yaml')
162160

@@ -255,14 +253,14 @@ def main():
255253
reply = dumpit(ynl, args, 'channels-get')
256254
print(f'Channel parameters for {args.device}:')
257255

258-
print(f'Pre-set maximums:')
256+
print('Pre-set maximums:')
259257
print_field(reply,
260258
('rx-max', 'RX'),
261259
('tx-max', 'TX'),
262260
('other-max', 'Other'),
263261
('combined-max', 'Combined'))
264262

265-
print(f'Current hardware settings:')
263+
print('Current hardware settings:')
266264
print_field(reply,
267265
('rx-count', 'RX'),
268266
('tx-count', 'TX'),
@@ -276,14 +274,14 @@ def main():
276274

277275
print(f'Ring parameters for {args.device}:')
278276

279-
print(f'Pre-set maximums:')
277+
print('Pre-set maximums:')
280278
print_field(reply,
281279
('rx-max', 'RX'),
282280
('rx-mini-max', 'RX Mini'),
283281
('rx-jumbo-max', 'RX Jumbo'),
284282
('tx-max', 'TX'))
285283

286-
print(f'Current hardware settings:')
284+
print('Current hardware settings:')
287285
print_field(reply,
288286
('rx', 'RX'),
289287
('rx-mini', 'RX Mini'),
@@ -298,7 +296,7 @@ def main():
298296
return
299297

300298
if args.statistics:
301-
print(f'NIC statistics:')
299+
print('NIC statistics:')
302300

303301
# TODO: pass id?
304302
strset = dumpit(ynl, args, 'strset-get')

tools/net/ynl/pyynl/lib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
__all__ = ["SpecAttr", "SpecAttrSet", "SpecEnumEntry", "SpecEnumSet",
1010
"SpecFamily", "SpecOperation", "SpecSubMessage", "SpecSubMessageFormat",
11-
"YnlFamily", "Netlink", "NlError"]
11+
"YnlFamily", "Netlink", "NlError", "YnlDocGenerator"]

tools/net/ynl/pyynl/lib/nlspec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def new_struct(self, elem):
501501
return SpecStruct(self, elem)
502502

503503
def new_sub_message(self, elem):
504-
return SpecSubMessage(self, elem);
504+
return SpecSubMessage(self, elem)
505505

506506
def new_operation(self, elem, req_val, rsp_val):
507507
return SpecOperation(self, elem, req_val, rsp_val)

tools/net/ynl/pyynl/lib/ynl.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import struct
1010
from struct import Struct
1111
import sys
12-
import yaml
1312
import ipaddress
1413
import uuid
1514
import queue
@@ -706,7 +705,7 @@ def _decode_unknown(self, attr):
706705
return attr.as_bin()
707706

708707
def _rsp_add(self, rsp, name, is_multi, decoded):
709-
if is_multi == None:
708+
if is_multi is None:
710709
if name in rsp and type(rsp[name]) is not list:
711710
rsp[name] = [rsp[name]]
712711
is_multi = True
@@ -739,14 +738,14 @@ def _decode_sub_msg(self, attr, attr_spec, search_attrs):
739738
decoded = {}
740739
offset = 0
741740
if msg_format.fixed_header:
742-
decoded.update(self._decode_struct(attr.raw, msg_format.fixed_header));
741+
decoded.update(self._decode_struct(attr.raw, msg_format.fixed_header))
743742
offset = self._struct_size(msg_format.fixed_header)
744743
if msg_format.attr_set:
745744
if msg_format.attr_set in self.attr_sets:
746745
subdict = self._decode(NlAttrs(attr.raw, offset), msg_format.attr_set)
747746
decoded.update(subdict)
748747
else:
749-
raise Exception(f"Unknown attribute-set '{attr_space}' when decoding '{attr_spec.name}'")
748+
raise Exception(f"Unknown attribute-set '{msg_format.attr_set}' when decoding '{attr_spec.name}'")
750749
return decoded
751750

752751
def _decode(self, attrs, space, outer_attrs = None):

tools/net/ynl/pyynl/ynl_gen_c.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
33

44
import argparse
5-
import collections
65
import filecmp
76
import pathlib
87
import os
@@ -14,7 +13,7 @@
1413

1514
sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
1615
from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, SpecEnumEntry
17-
from lib import SpecSubMessage, SpecSubMessageFormat
16+
from lib import SpecSubMessage
1817

1918

2019
def c_upper(name):
@@ -398,7 +397,7 @@ def _init_checks(self):
398397
if 'enum' in self.attr:
399398
enum = self.family.consts[self.attr['enum']]
400399
low, high = enum.value_range()
401-
if low == None and high == None:
400+
if low is None and high is None:
402401
self.checks['sparse'] = True
403402
else:
404403
if 'min' not in self.checks:
@@ -485,7 +484,7 @@ def struct_member(self, ri):
485484
ri.cw.p(f"char *{self.c_name};")
486485

487486
def _attr_typol(self):
488-
typol = f'.type = YNL_PT_NUL_STR, '
487+
typol = '.type = YNL_PT_NUL_STR, '
489488
if self.is_selector:
490489
typol += '.is_selector = 1, '
491490
return typol
@@ -539,7 +538,7 @@ def struct_member(self, ri):
539538
ri.cw.p(f"void *{self.c_name};")
540539

541540
def _attr_typol(self):
542-
return f'.type = YNL_PT_BINARY,'
541+
return '.type = YNL_PT_BINARY,'
543542

544543
def _attr_policy(self, policy):
545544
if len(self.checks) == 0:
@@ -636,10 +635,10 @@ def _complex_member_type(self, ri):
636635
return "struct nla_bitfield32"
637636

638637
def _attr_typol(self):
639-
return f'.type = YNL_PT_BITFIELD32, '
638+
return '.type = YNL_PT_BITFIELD32, '
640639

641640
def _attr_policy(self, policy):
642-
if not 'enum' in self.attr:
641+
if 'enum' not in self.attr:
643642
raise Exception('Enum required for bitfield32 attr')
644643
enum = self.family.consts[self.attr['enum']]
645644
mask = enum.get_mask(as_flags=True)
@@ -909,7 +908,7 @@ def _attr_get(self, ri, var):
909908
else:
910909
sel_var = f"{var}->{sel}"
911910
get_lines = [f'if (!{sel_var})',
912-
f'return ynl_submsg_failed(yarg, "%s", "%s");' %
911+
'return ynl_submsg_failed(yarg, "%s", "%s");' %
913912
(self.name, self['selector']),
914913
f"if ({self.nested_render_name}_parse(&parg, {sel_var}, attr))",
915914
"return YNL_PARSE_CB_ERROR;"]
@@ -1563,7 +1562,7 @@ def __init__(self, cw, family, ku_space, op, op_mode, attr_set=None):
15631562
if family.is_classic():
15641563
self.fixed_hdr_len = f"sizeof(struct {c_lower(fixed_hdr)})"
15651564
else:
1566-
raise Exception(f"Per-op fixed header not supported, yet")
1565+
raise Exception("Per-op fixed header not supported, yet")
15671566

15681567

15691568
# 'do' and 'dump' response parsing is identical
@@ -2099,7 +2098,7 @@ def _multi_parse(ri, struct, init_lines, local_vars):
20992098
if ri.family.is_classic():
21002099
iter_line = f"ynl_attr_for_each(attr, nlh, sizeof({struct.fixed_header}))"
21012100
else:
2102-
raise Exception(f"Per-op fixed header not supported, yet")
2101+
raise Exception("Per-op fixed header not supported, yet")
21032102

21042103
array_nests = set()
21052104
multi_attrs = set()
@@ -2502,7 +2501,7 @@ def print_free_prototype(ri, direction, suffix=';'):
25022501

25032502
def print_nlflags_set(ri, direction):
25042503
name = op_prefix(ri, direction)
2505-
ri.cw.write_func_prot(f'static inline void', f"{name}_set_nlflags",
2504+
ri.cw.write_func_prot('static inline void', f"{name}_set_nlflags",
25062505
[f"struct {name} *req", "__u16 nl_flags"])
25072506
ri.cw.block_start()
25082507
ri.cw.p('req->_nlmsg_flags = nl_flags;')
@@ -2533,7 +2532,7 @@ def _print_type(ri, direction, struct):
25332532
line = attr.presence_member(ri.ku_space, type_filter)
25342533
if line:
25352534
if not meta_started:
2536-
ri.cw.block_start(line=f"struct")
2535+
ri.cw.block_start(line="struct")
25372536
meta_started = True
25382537
ri.cw.p(line)
25392538
if meta_started:
@@ -2697,7 +2696,7 @@ def print_dump_type_free(ri):
26972696
ri.cw.nl()
26982697

26992698
_free_type_members(ri, 'rsp', ri.struct['reply'], ref='obj.')
2700-
ri.cw.p(f'free(rsp);')
2699+
ri.cw.p('free(rsp);')
27012700
ri.cw.block_end()
27022701
ri.cw.block_end()
27032702
ri.cw.nl()
@@ -2708,7 +2707,7 @@ def print_ntf_type_free(ri):
27082707
ri.cw.block_start()
27092708
_free_type_members_iter(ri, ri.struct['reply'])
27102709
_free_type_members(ri, 'rsp', ri.struct['reply'], ref='obj.')
2711-
ri.cw.p(f'free(rsp);')
2710+
ri.cw.p('free(rsp);')
27122711
ri.cw.block_end()
27132712
ri.cw.nl()
27142713

@@ -2803,8 +2802,6 @@ def print_kernel_policy_sparse_enum_validates(family, cw):
28032802
cw.p('/* Sparse enums validation callbacks */')
28042803
first = False
28052804

2806-
sign = '' if attr.type[0] == 'u' else '_signed'
2807-
suffix = 'ULL' if attr.type[0] == 'u' else 'LL'
28082805
cw.write_func_prot('static int', f'{c_lower(attr.enum_name)}_validate',
28092806
['const struct nlattr *attr', 'struct netlink_ext_ack *extack'])
28102807
cw.block_start()
@@ -3324,7 +3321,7 @@ def render_user_family(family, cw, prototype):
33243321
cw.block_start(f'{symbol} = ')
33253322
cw.p(f'.name\t\t= "{family.c_name}",')
33263323
if family.is_classic():
3327-
cw.p(f'.is_classic\t= true,')
3324+
cw.p('.is_classic\t= true,')
33283325
cw.p(f'.classic_id\t= {family.get("protonum")},')
33293326
if family.is_classic():
33303327
if family.fixed_header:

0 commit comments

Comments
 (0)