diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2017-11-01 23:14:15 +0100 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2017-11-01 23:14:15 +0100 |
| commit | 3ac8c0f577c79ebcac1d4cd267ee774af107c092 (patch) | |
| tree | bf4dba9550d21339f27795026a07b27a61368507 /kconfiglib.py | |
| parent | c676a03cb367c5d3e159e6cc18a83fed84cb776b (diff) | |
Emulate 'range' internals closer
Dug into the C implementation to check exactly what's going on in those
weird corner cases. Improves compatibility slightly for defaults with
invalid values (though those are pretty clearly a bug and generate a
warning anyway).
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 9039cfa..6fe9f88 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -2250,6 +2250,11 @@ class Symbol(object): self._write_to_conf = (vis != 0) if self.orig_type in (INT, HEX): + # The C implementation checks the user value against the range in a + # separate code path (post-processing after loading a .config). + # Checking all values here instead makes more sense for us. It + # requires that we check for a range first. + base = _TYPE_TO_BASE[self.orig_type] # Check if a range is in effect @@ -2257,6 +2262,8 @@ class Symbol(object): if expr_value(cond): has_active_range = True + # The zeros are from the C implementation running strtoll() + # on empty strings low = int(low_expr.str_value, base) if \ _is_base_n(low_expr.str_value, base) else 0 high = int(high_expr.str_value, base) if \ @@ -2283,36 +2290,31 @@ class Symbol(object): if expr_value(cond): self._write_to_conf = True - # Similarly to above, well-formed defaults are - # preserved as is. Defaults that do not satisfy a range - # constraints are clamped and take on a standard form. - val = val_expr.str_value if _is_base_n(val, base): val_num = int(val, base) - # TODO: move outside? - if has_active_range: - clamped_val = None - - if val_num < low: - clamped_val = low - elif val_num > high: - clamped_val = high - - if clamped_val is not None: - val = (hex(clamped_val) - if self.orig_type == HEX else - str(clamped_val)) - - break + else: + val_num = 0 # strtoll() on empty string + break else: - # No default kicked in. If there is an active range - # constraint, then the low end of the range is used, - # provided it's > 0, with "0x" prepended as appropriate. - if has_active_range and low > 0: - val = (hex(low) if self.orig_type == HEX else str(low)) + val_num = 0 # strtoll() on empty string + + # This clamping procedure runs even if there's no default + if has_active_range: + clamp = None + if val_num < low: + clamp = low + elif val_num > high: + clamp = high + + if clamp is not None: + # The value is rewritten to a standard form if it is + # clamped + val = str(clamp) \ + if self.orig_type == INT else \ + hex(clamp) elif self.orig_type == STRING: if vis and self.user_value is not None: |
