This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nuttx-ntfc.git


The following commit(s) were added to refs/heads/main by this push:
     new 2ecb573  coreconfig.py: fix .config parsing of quoted values 
containing hex
2ecb573 is described below

commit 2ecb573d19ba3f751e6d4e04e6aad680214b1b2a
Author: raiden00pl <[email protected]>
AuthorDate: Tue Aug 4 17:12:40 2026 +0200

    coreconfig.py: fix .config parsing of quoted values containing hex
    
    The hex branch matched any value containing "0x" before the quoted
    string branch ran, so values like CONFIG_BOARD_MEMORY_RANGE=
    "{0x40000000,...}" raised ValueError. Parse quoted strings first and
    require hex to start with "0x", falling back to the raw string.
    
    Signed-off-by: raiden00pl <[email protected]>
    Assisted-by: Claude Code
---
 src/ntfc/coreconfig.py   | 36 ++++++++++++++++++++----------------
 tests/test_coreconfig.py |  6 ++++++
 2 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/src/ntfc/coreconfig.py b/src/ntfc/coreconfig.py
index 1d54090..9f7f99d 100644
--- a/src/ntfc/coreconfig.py
+++ b/src/ntfc/coreconfig.py
@@ -46,6 +46,25 @@ class CoreConfig:
             # load ELF
             self._elf = ElfParser(elf_path)
 
+    @staticmethod
+    def _parse_config_value(val: str) -> Union[bool, str, int]:
+        """Parse a single Kconfig option value."""
+        if val == "y":
+            return True
+        if val == "n":
+            return False
+        if val.startswith('"') and val.endswith('"'):
+            # quoted strings first: they may contain hex digits
+            return val[1:-1]
+        if val.startswith("0x"):
+            try:
+                return int(val, 16)
+            except ValueError:
+                return val
+        if val.isdigit():
+            return int(val)
+        return val
+
     def _load_core_config(self) -> None:
         """Load core configuration."""
         with open(self._config["conf_path"], "r", encoding="utf-8") as f:
@@ -60,22 +79,7 @@ class CoreConfig:
                     # no '=' found — skip malformed line
                     continue
 
-                # parse option value
-                val_parsed: Union[bool, str, int]
-                if val == "y":
-                    val_parsed = True
-                elif val == "n":
-                    val_parsed = False
-                elif "0x" in val:
-                    val_parsed = int(val.rstrip(), 16)
-                elif val.isdigit():
-                    val_parsed = int(val)
-                elif val.startswith('"') and val.endswith('"'):
-                    val_parsed = val[1:-1]
-                else:
-                    val_parsed = val
-
-                self._kv_values[name] = val_parsed
+                self._kv_values[name] = self._parse_config_value(val)
 
     @property
     def uptime(self) -> Any:
diff --git a/tests/test_coreconfig.py b/tests/test_coreconfig.py
index 1fa1c38..f4e80f2 100644
--- a/tests/test_coreconfig.py
+++ b/tests/test_coreconfig.py
@@ -79,6 +79,8 @@ def test_load_core_config_value_types(tmp_path):
         "CONFIG_BOOL_N=n\n"
         'CONFIG_QUOTED="hello"\n'
         "CONFIG_UNQUOTED=plain_text\n"
+        'CONFIG_QUOTED_HEX="{0x40000000,0x100}"\n'
+        "CONFIG_BAD_HEX=0xZZ\n"
     )
     conf = {"name": "dummy", "conf_path": str(cfg_file)}
     p = CoreConfig(conf)
@@ -89,6 +91,10 @@ def test_load_core_config_value_types(tmp_path):
     assert p.kv_check("CONFIG_BOOL_N") is False
     assert p.kv_check("CONFIG_QUOTED") == "hello"
     assert p.kv_check("CONFIG_UNQUOTED") == "plain_text"
+    # quoted values containing hex digits must stay strings
+    assert p.kv_check("CONFIG_QUOTED_HEX") == "{0x40000000,0x100}"
+    # unparsable hex falls back to the raw string
+    assert p.kv_check("CONFIG_BAD_HEX") == "0xZZ"
 
 
 def test_core_config_flash_only_property():

Reply via email to