diff options
| -rw-r--r-- | examples/allyesconfig.py | 12 | ||||
| -rw-r--r-- | kconfiglib.py | 132 | ||||
| -rw-r--r-- | testsuite.py | 56 |
3 files changed, 85 insertions, 115 deletions
diff --git a/examples/allyesconfig.py b/examples/allyesconfig.py index 6e2e065..efac3cf 100644 --- a/examples/allyesconfig.py +++ b/examples/allyesconfig.py @@ -91,16 +91,16 @@ while 1: selection.set_value(2) no_changes = False - # Handle a choice whose visibility only allows it to be in "m" mode. - # This might happen if a choice depends on a symbol that can only be - # "m", for example. + # Handle a choice whose visibility only allows it to be in m mode. This + # might happen if a choice depends on a symbol that can only be m, for + # example. elif choice.visibility == 1: for sym in choice.symbols: - # Does the choice have a symbol that can be "m" that we haven't - # already set to "m"? - if sym.user_tri_value != 1 and 1 in sym.assignable: + # Does the choice have a symbol that can be m that we haven't + # already set to m? + if sym.user_value != 1 and 1 in sym.assignable: # Yup, set it sym.set_value(1) diff --git a/kconfiglib.py b/kconfiglib.py index f960cf1..6b6eff6 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -665,17 +665,19 @@ class Kconfig(object): sym.name)) continue + # We represent tristate values as 0, 1, 2 + val = STR_TO_TRI[val[0]] + if sym.choice is not None: - mode = sym.choice.user_str_value + mode = sym.choice.user_value if mode is not None and mode != val: self._warn("assignment to {} changes mode of " 'containing choice from {} to {}.' - .format(name, mode, val), + .format(name, + TRI_TO_STR[mode], + TRI_TO_STR[val]), filename, linenr) - # We represent tristate values as 0, 1, 2 - val = STR_TO_TRI[val[0]] - elif sym.orig_type == STRING: string_match = _conf_string_re_match(val) if not string_match: @@ -706,17 +708,18 @@ class Kconfig(object): # Done parsing the assignment. Set the value. - if sym.user_str_value is not None: - # Make the format for the old and new value consistent in - # the warning + if sym.user_value is not None: + # Use strings for tristate values in the warning if sym.orig_type in (BOOL, TRISTATE): display_val = TRI_TO_STR[val] + display_user_val = TRI_TO_STR[sym.user_value] else: display_val = val + display_user_val = sym.user_value self._warn('{} set more than once. Old value: "{}", new ' 'value: "{}".' - .format(name, sym.user_str_value, display_val), + .format(name, display_user_val, display_val), filename, linenr) sym._set_value_no_invalidate(val, True) @@ -786,12 +789,11 @@ class Kconfig(object): for sym in self.defined_syms: # We're iterating over all (defined) symbols, so no need for # symbols to invalidate their dependent symbols - sym.user_str_value = sym.user_tri_value = None + sym.user_value = None sym._invalidate() for choice in self._choices: - choice.user_str_value = choice.user_tri_value = \ - choice.user_selection = None + choice.user_value = choice.user_selection = None choice._invalidate() def enable_warnings(self): @@ -2045,21 +2047,12 @@ class Symbol(object): The visibility of the symbol. One of 0, 1, 2, representing n, m, y. See the module documentation for an overview of symbol values and visibility. - user_str_value: - The user value of the symbol as a string, or None if no user value has - been assigned (via Kconfig.load_value() or Symbol.set_value()). See - str_value. - - Warning: Do not assign directly to this. It will break things. Use - Symbol.set_value(). - - user_tri_value: - The user value of the symbol as a tristate value (0, 1, 2), or None if no - user value has been assigned (via Kconfig.load_value() or - Symbol.set_value()). See tri_value. + user_value: + The user value of the symbol. None if no user value has been assigned + (via Kconfig.load_config() or Symbol.set_value()). - Always 0 (n) for non-bool/tristate symbols (or None if no user value has - been set). + 0, 1, or 2 for bool/tristate symbols, and a string for other symbol + types. Warning: Do not assign directly to this. It will break things. Use Symbol.set_value(). @@ -2178,8 +2171,7 @@ class Symbol(object): "ranges", "rev_dep", "selects", - "user_str_value", - "user_tri_value", + "user_value", "weak_rev_dep", ) @@ -2242,15 +2234,15 @@ class Symbol(object): else: has_active_range = False - if vis and self.user_str_value is not None and \ - _is_base_n(self.user_str_value, base) and \ + if vis and self.user_value is not None and \ + _is_base_n(self.user_value, base) and \ (not has_active_range or - low <= int(self.user_str_value, base) <= high): + low <= int(self.user_value, base) <= high): # If the user value is well-formed and satisfies range # contraints, it is stored in exactly the same form as # specified in the assignment (with or without "0x", etc.) - val = self.user_str_value + val = self.user_value else: # No user value or invalid user value. Look at defaults. @@ -2291,9 +2283,9 @@ class Symbol(object): val = (hex(low) if self.orig_type == HEX else str(low)) elif self.orig_type == STRING: - if vis and self.user_str_value is not None: + if vis and self.user_value is not None: # If the symbol is visible and has a user value, use that - val = self.user_str_value + val = self.user_value else: # Otherwise, look at defaults for val_expr, cond in self.defaults: @@ -2324,9 +2316,9 @@ class Symbol(object): if self.choice is None: self._write_to_conf = (vis != 0) - if vis and self.user_tri_value is not None: + if vis and self.user_value is not None: # If the symbol is visible and has a user value, use that - val = min(self.user_tri_value, vis) + val = min(self.user_value, vis) else: # Otherwise, look at defaults and weak reverse dependencies @@ -2368,7 +2360,7 @@ class Symbol(object): if mode == 2: val = 2 if self.choice.selection is self else 0 - elif self.user_tri_value: + elif self.user_value: # mode == 1, user value available and not 0 val = 1 @@ -2475,7 +2467,7 @@ class Symbol(object): Resets the user value of the symbol, as if the symbol had never gotten a user value via Kconfig.load_config() or Symbol.set_value(). """ - self.user_str_value = self.user_tri_value = None + self.user_value = None self._rec_invalidate() def __repr__(self): @@ -2492,13 +2484,21 @@ class Symbol(object): if node.prompt is not None: fields.append('"{}"'.format(node.prompt[0])) - fields.append('value "{}"'.format(self.str_value)) + # Only add quotes for non-bool/tristate symbols + fields.append("value " + + (self.str_value + if self.orig_type in (BOOL, TRISTATE) else + '"{}"'.format(self.str_value))) if not self.is_constant: # These aren't helpful to show for constant symbols - if self.user_str_value is not None: - fields.append('user value "{}"'.format(self.user_str_value)) + if self.user_value is not None: + # Only add quotes for non-bool/tristate symbols + fields.append("user value " + + (TRI_TO_STR[self.user_value] + if self.orig_type in (BOOL, TRISTATE) else + '"{}"'.format(self.user_value))) fields.append("visibility " + TRI_TO_STR[self.visibility]) @@ -2574,7 +2574,7 @@ class Symbol(object): self.nodes = [] - self.user_str_value = self.user_tri_value = None + self.user_value = None # Populated in Kconfig._build_dep() after parsing. Links the symbol to # the symbols that immediately depend on it (in a caching/invalidation @@ -2683,23 +2683,16 @@ class Symbol(object): "promptless symbol {} will have no effect" .format(value, self.name)) - if self.orig_type in (BOOL, TRISTATE): - self.user_str_value = TRI_TO_STR[value] - self.user_tri_value = value - else: - self.user_str_value = value - self.user_tri_value = 0 + self.user_value = value # TODO: assigning automatically changes choice yada yada if self.choice is not None: - if self.user_tri_value == 2: - self.choice.user_str_value = "y" - self.choice.user_tri_value = 2 + if self.user_value == 2: + self.choice.user_value = 2 self.choice.user_selection = self - elif self.user_tri_value == 1: - self.choice.user_str_value = "m" - self.choice.user_tri_value = 1 + elif self.user_value == 1: + self.choice.user_value = 1 def _invalidate(self): """ @@ -2844,17 +2837,10 @@ class Choice(object): The symbol that would be selected by default, had the user not selected any symbol. Can be None for the same reasons as 'selected'. - user_str_value: + user_value: The value (mode) selected by the user (through Choice.set_value() or by - assigning a value to a symbol within the choice), represented as a - string. See Symbol.user_str_value. - - Warning: Do not assign directly to this. It will break things. Use - Choice.set_value() or Symbol.set_value() instead. - - user_tri_value: - The same value as user_str_value as a tristate (0, 1, 2). See - Symbol.user_tri_value. + assigning a value to a symbol within the choice). Either 0, 1, or 2, or + None if the user hasn't selected a mode. See Symbol.user_value. Warning: Do not assign directly to this. It will break things. Use Choice.set_value() or Symbol.set_value() instead. @@ -2906,8 +2892,7 @@ class Choice(object): "orig_type", "syms", "user_selection", - "user_str_value", - "user_tri_value", + "user_value", ) # @@ -2936,8 +2921,8 @@ class Choice(object): """ See the class documentation. """ - if self.user_tri_value is not None: - val = min(self.user_tri_value, self.visibility) + if self.user_value is not None: + val = min(self.user_value, self.visibility) else: val = 0 @@ -3023,8 +3008,7 @@ class Choice(object): .format(value, _TYPENAME[self.orig_type])) return - self.user_str_value = TRI_TO_STR[value] - self.user_tri_value = value + self.user_value = value if self.syms: # Hackish way to invalidate the choice and all the choice symbols @@ -3035,7 +3019,7 @@ class Choice(object): Resets the user value (mode) and user selection of the Choice, as if the user had never touched the mode or any of the choice symbols. """ - self.user_str_value = self.user_tri_value = self.user_selection = None + self.user_value = self.user_selection = None if self.syms: # Hackish way to invalidate the choice and all the choice symbols self.syms[0]._rec_invalidate() @@ -3056,8 +3040,8 @@ class Choice(object): fields.append("mode " + self.str_value) - if self.user_str_value is not None: - fields.append('user mode {}'.format(self.user_str_value)) + if self.user_value is not None: + fields.append('user mode {}'.format(TRI_TO_STR[self.user_value])) if self.selection is not None: fields.append("{} selected".format(self.selection.name)) @@ -3109,7 +3093,7 @@ class Choice(object): self.nodes = [] - self.user_str_value = self.user_tri_value = self.user_selection = None + self.user_value = self.user_selection = None # The prompts and default values without any dependencies from # enclosing menus and ifs propagated diff --git a/testsuite.py b/testsuite.py index adde8f3..fa10a39 100644 --- a/testsuite.py +++ b/testsuite.py @@ -207,30 +207,18 @@ def run_selftests(): assign_and_verify_value(sym_name, user_val, user_val) def assign_and_verify_user_value(sym_name, val, user_val): - """Assigns a user value to the symbol and verifies the new user value. - Also checks consistency of user_str_value and user_tri_value.""" + """ + Assigns a user value to the symbol and verifies the new user value. + """ sym = c.syms[sym_name] + sym_old_user_val = sym.user_value - if sym.type in (BOOL, TRISTATE): - user_val = TRI_TO_STR[user_val] - - sym_old_user_val = sym.user_str_value sym.set_value(val) - verify(sym.user_str_value == user_val, - "{} should have the user value '{}' after being assigned " - "the user value '{}'. Instead, the new user value was '{}'. " - "The old user value was '{}'." - .format(sym_name, user_val, user_val, sym.user_str_value, - sym_old_user_val)) - - if sym.type in (BOOL, TRISTATE): - verify(sym.user_tri_value == STR_TO_TRI[sym.user_str_value], - "{} has an inconsistent user value ('{}' vs '{}')" - .format(sym_name, sym.user_tri_value, sym.user_str_value)) - else: - verify(sym.user_tri_value == 0, - "{} is non-bool/tristate and has the non-zero " - "user_tri_value {}".format(sym_name, sym.user_tri_value)) + verify(sym.user_value == user_val, + "the assigned user value '{}' wasn't reflected in user_value " + "on the symbol {}. Instead, the new user_value was '{}'. The " + "old user value was '{}'." + .format(user_val, sym.name, sym.user_value, sym_old_user_val)) # # Selftests @@ -614,15 +602,15 @@ choice c = Kconfig("Kconfiglib/tests/Krepr", warn=False) verify_repr(c.n, """ -<symbol n, tristate, value "n", constant> +<symbol n, tristate, value n, constant> """) verify_repr(c.m, """ -<symbol m, tristate, value "m", constant> +<symbol m, tristate, value m, constant> """) verify_repr(c.y, """ -<symbol y, tristate, value "y", constant> +<symbol y, tristate, value y, constant> """) verify_repr(c.syms["UNDEFINED"], """ @@ -630,17 +618,17 @@ choice """) verify_repr(c.syms["BASIC"], """ -<symbol BASIC, bool, value "y", visibility n, direct deps y, Kconfiglib/tests/Krepr:9> +<symbol BASIC, bool, value y, visibility n, direct deps y, Kconfiglib/tests/Krepr:9> """) verify_repr(c.syms["VISIBLE"], """ -<symbol VISIBLE, bool, "visible", value "n", visibility y, direct deps y, Kconfiglib/tests/Krepr:14> +<symbol VISIBLE, bool, "visible", value n, visibility y, direct deps y, Kconfiglib/tests/Krepr:14> """) c.syms["VISIBLE"].set_value(2) verify_repr(c.syms["VISIBLE"], """ -<symbol VISIBLE, bool, "visible", value "y", user value "y", visibility y, direct deps y, Kconfiglib/tests/Krepr:14> +<symbol VISIBLE, bool, "visible", value y, user value y, visibility y, direct deps y, Kconfiglib/tests/Krepr:14> """) verify_repr(c.syms["DIR_DEP_N"], """ @@ -656,11 +644,11 @@ choice """) verify_repr(c.syms["CHOICE_1"], """ -<symbol CHOICE_1, tristate, "choice sym", value "n", visibility y, choice symbol, direct deps y, Kconfiglib/tests/Krepr:33> +<symbol CHOICE_1, tristate, "choice sym", value n, visibility y, choice symbol, direct deps y, Kconfiglib/tests/Krepr:33> """) verify_repr(c.modules, """ -<symbol MODULES, bool, value "y", visibility n, is the modules symbol, direct deps y, Kconfiglib/tests/Krepr:1> +<symbol MODULES, bool, value y, visibility n, is the modules symbol, direct deps y, Kconfiglib/tests/Krepr:1> """) @@ -1267,7 +1255,7 @@ g ("BOOL", "TRISTATE", "STRING", "INT", "HEX")] for sym in syms: - verify(sym.user_str_value is None and sym.user_tri_value is None, + verify(sym.user_value is None, "{} should not have a user value to begin with") # Assign valid values for the types @@ -1297,7 +1285,7 @@ g for s in syms: s.unset_value() - verify(s.user_str_value is None and s.user_tri_value is None, + verify(s.user_value is None, "{} should not have a user value after being reset". format(s.name)) @@ -1953,8 +1941,7 @@ def test_sanity(conf, arch): sym.tri_value sym.type sym.unset_value() - sym.user_str_value - sym.user_tri_value + sym.user_value sym.visibility for sym in conf.defined_syms: @@ -2009,8 +1996,7 @@ def test_sanity(conf, arch): choice.__repr__() choice.str_value choice.tri_value - choice.user_str_value - choice.user_tri_value + choice.user_value choice.assignable choice.selection choice.default_selection |
