From d321f04225e57c6627eebd06242a1869aea6a4f2 Mon Sep 17 00:00:00 2001 From: pro-laxmi Date: Wed, 11 Nov 2026 23:13:09 +0530 Subject: [PATCH 1/3] fix: make os-release parser resilient to unclosed quote ValueError --- src/distro/distro.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/distro/distro.py b/src/distro/distro.py index 90ceb87..d2aed75 100755 --- a/src/distro/distro.py +++ b/src/distro/distro.py @@ -1185,10 +1185,22 @@ def _parse_os_release_content(lines: TextIO) -> Dict[str, str]: A dictionary containing all information items. """ props = {} - lexer = shlex.shlex(lines, posix=True) - lexer.whitespace_split = True + try: + lexer = shlex.shlex(lines, posix=True) + lexer.whitespace_split = True + tokens = list(lexer) + except ValueError: + if hasattr(lines, "seek"): + lines.seek(os.SEEK_SET) + tokens = [] + for line in lines: + line = line.strip() + if not line or line.startswith("#"): + continue + if "=" in line: + k, v = line.split("=", 1) + tokens.append(f"{k}={v.strip('\"\'')}") - tokens = list(lexer) for token in tokens: # At this point, all shell-like parsing has been done (i.e. # comments processed, quotes and backslash escape sequences From 15e8332bb923f2e30e91c51de6873d7c241f95e3 Mon Sep 17 00:00:00 2001 From: pro-laxmi Date: Sat, 14 Nov 2026 21:07:16 +0530 Subject: [PATCH 2/3] Simplify seek call and add regression test for unclosed quotes --- src/distro/distro.py | 3 +-- tests/test_distro.py | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/distro/distro.py b/src/distro/distro.py index d2aed75..6c6f3ac 100755 --- a/src/distro/distro.py +++ b/src/distro/distro.py @@ -1190,8 +1190,7 @@ def _parse_os_release_content(lines: TextIO) -> Dict[str, str]: lexer.whitespace_split = True tokens = list(lexer) except ValueError: - if hasattr(lines, "seek"): - lines.seek(os.SEEK_SET) + lines.seek(0) tokens = [] for line in lines: line = line.strip() diff --git a/tests/test_distro.py b/tests/test_distro.py index 0131ba6..695bfc3 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -2647,6 +2647,14 @@ def test_kx_02(self) -> None: assert props.get("key1", None) is None assert props.get("key2", None) == "value 2" + def test_kx_03_unclosed_quotes(self) -> None: + props = self.distro._parse_os_release_content( + io.StringIO('NAME="Ubuntu\nID=ubuntu\nVERSION_ID="24.04"\n') + ) + assert props.get("name", None) == "Ubuntu" + assert props.get("id", None) == "ubuntu" + assert props.get("version_id", None) == "24.04" + @pytest.mark.skipif(not IS_LINUX, reason="Irrelevant on non-linux") class TestGlobal: From 74b8953c7e67bb9cdc87da229172bd6eafc659b7 Mon Sep 17 00:00:00 2001 From: pro-laxmi Date: Sun, 15 Nov 2026 10:04:39 +0530 Subject: [PATCH 3/3] fix: avoid backslash in f-string expression for Python < 3.12 compatibility --- src/distro/distro.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/distro/distro.py b/src/distro/distro.py index 6c6f3ac..9d5a068 100755 --- a/src/distro/distro.py +++ b/src/distro/distro.py @@ -1198,7 +1198,8 @@ def _parse_os_release_content(lines: TextIO) -> Dict[str, str]: continue if "=" in line: k, v = line.split("=", 1) - tokens.append(f"{k}={v.strip('\"\'')}") + clean_v = v.strip("'\"") + tokens.append(f"{k}={clean_v}") for token in tokens: # At this point, all shell-like parsing has been done (i.e.