sb: %if checks are numeric if the left and right values are numbers

- If the left and right values are numbers make the check numeric.

Update #4631
This commit is contained in:
Chris Johns 2022-04-26 10:12:58 +10:00
parent 990e3f05e6
commit 4c3708f127

View File

@ -68,6 +68,13 @@ def _check_nil(value):
istrue = False istrue = False
return istrue return istrue
def _check_number(value):
try:
float(value)
return True
except ValueError:
return False
class package: class package:
def __init__(self, name, arch, config): def __init__(self, name, arch, config):
@ -915,6 +922,12 @@ class file:
(self.name, self.lc, (self.name, self.lc,
self.if_depth, self.if_depth,
join_op)) join_op))
# If OR and the previous check was true short circuit the evaluation
if join_op == 'or' and cistrue:
log.trace('config: %s: %3d: _if[%i]: OR true, short circuit eval' % \
(self.name, self.lc,
self.if_depth))
break
ori = 0 ori = 0
andi = 0 andi = 0
i = len(cls) i = len(cls)
@ -935,10 +948,8 @@ class file:
i = andi i = andi
elif andi == 0: elif andi == 0:
i = ori i = ori
elif ori < andi:
i = andi
else: else:
i = andi i = min(ori, andi)
log.trace('config: %s: %3d: _if[%i]: next OP found at %i' % \ log.trace('config: %s: %3d: _if[%i]: next OP found at %i' % \
(self.name, self.lc, (self.name, self.lc,
self.if_depth, self.if_depth,
@ -996,37 +1007,27 @@ class file:
ifls = (' '.join(ifls[:op_pos]), op, ' '.join(ifls[op_pos + 1:])) ifls = (' '.join(ifls[:op_pos]), op, ' '.join(ifls[op_pos + 1:]))
break break
if len(ifls) != 3: if len(ifls) != 3:
self._error('malformed if: ' + reduce(add, ls, '')) self._error('malformed if: ' + reduce(add, ls, ''))
if ifls[1] == '==': lhs = ifls[0]
if ifls[0] == ifls[2]: operator = ifls[1]
istrue = True rhs = ifls[2]
else: if _check_number(lhs) and _check_number(rhs):
istrue = False log.trace('config: %s: %3d: _if: numeric value check' % \
elif ifls[1] == '!=' or ifls[1] == '=!': (self.name, self.lc))
if ifls[0] != ifls[2]: lhs = float(lhs)
istrue = True rhs = float(rhs)
else: if operator == '==':
istrue = False istrue = lhs == rhs
elif ifls[1] == '>': elif operator == '!=' or operator == '=!':
if ifls[0] > ifls[2]: istrue = lhs != rhs
istrue = True elif operator == '>':
else: istrue = lhs > rhs
istrue = False elif operator == '>=' or operator == '=>':
elif ifls[1] == '>=' or ifls[1] == '=>': istrue = lhs >= rhs
if ifls[0] >= ifls[2]: elif operator == '<=' or operator == '=<':
istrue = True istrue = lhs <= rhs
else: elif operator == '<':
istrue = False istrue = lhs < rhs
elif ifls[1] == '<=' or ifls[1] == '=<':
if ifls[0] <= ifls[2]:
istrue = True
else:
istrue = False
elif ifls[1] == '<':
if ifls[0] < ifls[2]:
istrue = True
else:
istrue = False
else: else:
self._error('invalid %if operator: ' + reduce(add, ls, '')) self._error('invalid %if operator: ' + reduce(add, ls, ''))