diff --git a/src/distro/distro.py b/src/distro/distro.py index 90ceb87..9d5a068 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: + lines.seek(0) + tokens = [] + for line in lines: + line = line.strip() + if not line or line.startswith("#"): + continue + if "=" in line: + k, v = line.split("=", 1) + clean_v = v.strip("'\"") + tokens.append(f"{k}={clean_v}") - 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 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: