Skip to content

Commit 2b3a24e

Browse files
committed
Pylint complements: avoid no-else-raise "refactor" issues
With python3, pylint complains about `else: raise()` constructs. This rework avoids them and reduces cyclomatic complexity by using the error-out-first idiom. Signed-off-by: Yann Dirson <[email protected]>
1 parent c3e1b31 commit 2b3a24e

File tree

2 files changed

+47
-51
lines changed

2 files changed

+47
-51
lines changed

xcp/net/mac.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,25 @@ def __init__(self, addr):
6161
self.octets = []
6262
self.integer = -1
6363

64-
if isinstance(addr, six.string_types):
65-
66-
res = VALID_COLON_MAC.match(addr)
67-
if res:
68-
self._set_from_str_octets(addr.split(":"))
69-
return
64+
if not isinstance(addr, six.string_types):
65+
raise TypeError("String expected")
7066

71-
res = VALID_DASH_MAC.match(addr)
72-
if res:
73-
self._set_from_str_octets(addr.split("-"))
74-
return
67+
res = VALID_COLON_MAC.match(addr)
68+
if res:
69+
self._set_from_str_octets(addr.split(":"))
70+
return
7571

76-
res = VALID_DOTQUAD_MAC.match(addr)
77-
if res:
78-
self._set_from_str_quads(addr.split("."))
79-
return
72+
res = VALID_DASH_MAC.match(addr)
73+
if res:
74+
self._set_from_str_octets(addr.split("-"))
75+
return
8076

81-
raise ValueError("Unrecognised MAC address '%s'" % addr)
77+
res = VALID_DOTQUAD_MAC.match(addr)
78+
if res:
79+
self._set_from_str_quads(addr.split("."))
80+
return
8281

83-
else:
84-
raise TypeError("String expected")
82+
raise ValueError("Unrecognised MAC address '%s'" % addr)
8583

8684

8785
def _set_from_str_octets(self, octets):

xcp/pci.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,48 +67,46 @@ def __init__(self, addr):
6767
self.function = -1
6868
self.index = -1
6969

70-
if isinstance(addr, six.string_types):
71-
72-
res = VALID_SBDFI.match(addr)
73-
if res:
74-
groups = res.groupdict()
70+
if not isinstance(addr, six.string_types):
71+
raise TypeError("String expected")
7572

76-
if "segment" in groups and groups["segment"] is not None:
77-
self.segment = int(groups["segment"], 16)
78-
else:
79-
self.segment = 0
73+
res = VALID_SBDFI.match(addr)
74+
if res:
75+
groups = res.groupdict()
8076

81-
self.bus = int(groups["bus"], 16)
82-
if not ( 0 <= self.bus < 2**8 ):
83-
raise ValueError("Bus '%d' out of range 0 <= bus < 256"
84-
% (self.bus,))
77+
if "segment" in groups and groups["segment"] is not None:
78+
self.segment = int(groups["segment"], 16)
79+
else:
80+
self.segment = 0
8581

86-
self.device = int(groups["device"], 16)
87-
if not ( 0 <= self.device < 2**5):
88-
raise ValueError("Device '%d' out of range 0 <= device < 32"
89-
% (self.device,))
82+
self.bus = int(groups["bus"], 16)
83+
if not ( 0 <= self.bus < 2**8 ):
84+
raise ValueError("Bus '%d' out of range 0 <= bus < 256"
85+
% (self.bus,))
9086

91-
self.function = int(groups["function"], 16)
92-
if not ( 0 <= self.function < 2**3):
93-
raise ValueError("Function '%d' out of range 0 <= device "
94-
"< 8" % (self.function,))
87+
self.device = int(groups["device"], 16)
88+
if not ( 0 <= self.device < 2**5):
89+
raise ValueError("Device '%d' out of range 0 <= device < 32"
90+
% (self.device,))
9591

96-
if "index" in groups and groups["index"] is not None:
97-
self.index = int(groups["index"])
98-
else:
99-
self.index = 0
92+
self.function = int(groups["function"], 16)
93+
if not ( 0 <= self.function < 2**3):
94+
raise ValueError("Function '%d' out of range 0 <= device "
95+
"< 8" % (self.function,))
10096

101-
self.integer = (int(self.segment << 16 |
102-
self.bus << 8 |
103-
self.device << 3 |
104-
self.function) << 8 |
105-
self.index)
106-
return
97+
if "index" in groups and groups["index"] is not None:
98+
self.index = int(groups["index"])
99+
else:
100+
self.index = 0
107101

108-
raise ValueError("Unrecognised PCI address '%s'" % addr)
102+
self.integer = (int(self.segment << 16 |
103+
self.bus << 8 |
104+
self.device << 3 |
105+
self.function) << 8 |
106+
self.index)
107+
return
109108

110-
else:
111-
raise TypeError("String expected")
109+
raise ValueError("Unrecognised PCI address '%s'" % addr)
112110

113111

114112
def __str__(self):

0 commit comments

Comments
 (0)