From f40155757fa490daf20c7dda7a38c19223ad4a6e Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Thu, 29 Jul 2021 20:02:03 +0200 Subject: [PATCH 1/5] Fix crash when `uname -rs` output is empty > closes #264 --- distro.py | 8 ++++++-- tests/resources/testdistros/distro/emptyuname/bin/uname | 2 ++ tests/test_distro.py | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/resources/testdistros/distro/emptyuname/bin/uname diff --git a/distro.py b/distro.py index a8a944e..cecb1da 100755 --- a/distro.py +++ b/distro.py @@ -1182,11 +1182,15 @@ def _uname_info(self): # type: () -> Dict[str, str] with open(os.devnull, "wb") as devnull: try: - cmd = ("uname", "-rs") - stdout = subprocess.check_output(cmd, stderr=devnull) + stdout = subprocess.check_output(("uname", "-rs"), stderr=devnull) except OSError: return {} + content = self._to_str(stdout).splitlines() + if not content: + # For some reasons, `uname -rs` output is empty: ignore it. + return {} + return self._parse_uname_content(content) @staticmethod diff --git a/tests/resources/testdistros/distro/emptyuname/bin/uname b/tests/resources/testdistros/distro/emptyuname/bin/uname new file mode 100644 index 0000000..0c16c70 --- /dev/null +++ b/tests/resources/testdistros/distro/emptyuname/bin/uname @@ -0,0 +1,2 @@ +#!/bin/sh +echo -n diff --git a/tests/test_distro.py b/tests/test_distro.py index b51ace9..ed03a41 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -712,6 +712,14 @@ def test_bad_uname(self): assert self.distro.uname_attr("name") == "" assert self.distro.uname_attr("release") == "" + def test_empty_uname(self): + self._setup_for_distro(os.path.join(TESTDISTROS, "distro", "emptyuname")) + self.distro = distro.LinuxDistribution() + + assert self.distro.uname_attr("id") == "" + assert self.distro.uname_attr("name") == "" + assert self.distro.uname_attr("release") == "" + def test_usrlibosreleaseonly(self): self._setup_for_distro( os.path.join(TESTDISTROS, "distro", "usrlibosreleaseonly") From ffec775e9569c4c3bcf749676fe47900d784b312 Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Fri, 30 Jul 2021 19:42:02 +0200 Subject: [PATCH 2/5] Switch non-empty output test to `_parse_uname_content` definition --- distro.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/distro.py b/distro.py index cecb1da..3e2465b 100755 --- a/distro.py +++ b/distro.py @@ -1185,17 +1185,14 @@ def _uname_info(self): stdout = subprocess.check_output(("uname", "-rs"), stderr=devnull) except OSError: return {} - content = self._to_str(stdout).splitlines() - if not content: - # For some reasons, `uname -rs` output is empty: ignore it. - return {} - return self._parse_uname_content(content) @staticmethod def _parse_uname_content(lines): # type: (Sequence[str]) -> Dict[str, str] + if not lines: + return {} props = {} match = re.search(r"^([^\s]+)\s+([\d\.]+)", lines[0].strip()) if match: From 78a6bc3f7e9475ae66bf5bcd298c94435e6fb652 Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sat, 31 Jul 2021 18:57:54 +0000 Subject: [PATCH 3/5] One more line feed just for consistency --- tests/resources/testdistros/distro/emptyuname/bin/uname | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/resources/testdistros/distro/emptyuname/bin/uname b/tests/resources/testdistros/distro/emptyuname/bin/uname index 0c16c70..ded5610 100644 --- a/tests/resources/testdistros/distro/emptyuname/bin/uname +++ b/tests/resources/testdistros/distro/emptyuname/bin/uname @@ -1,2 +1,3 @@ -#!/bin/sh + #!/bin/sh + echo -n From c162682f3dad265e2974f2ae22355f635bff876d Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sat, 31 Jul 2021 18:58:33 +0000 Subject: [PATCH 4/5] Woops, starting whitespaces got mixed up --- tests/resources/testdistros/distro/emptyuname/bin/uname | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/resources/testdistros/distro/emptyuname/bin/uname b/tests/resources/testdistros/distro/emptyuname/bin/uname index ded5610..58a4484 100644 --- a/tests/resources/testdistros/distro/emptyuname/bin/uname +++ b/tests/resources/testdistros/distro/emptyuname/bin/uname @@ -1,3 +1,3 @@ - #!/bin/sh +#!/bin/sh echo -n From 4b0efde644d6278b7760005f83f5b538d9f75783 Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sat, 31 Jul 2021 19:00:50 +0000 Subject: [PATCH 5/5] Rollback those changes --- distro.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/distro.py b/distro.py index dbefea4..60d92df 100755 --- a/distro.py +++ b/distro.py @@ -1123,7 +1123,8 @@ def _parse_lsb_release_content(lines: Iterable[str]) -> Dict[str, str]: @cached_property def _uname_info(self) -> Dict[str, str]: try: - stdout = subprocess.check_output(("uname", "-rs"), stderr=devnull) + cmd = ("uname", "-rs") + stdout = subprocess.check_output(cmd, stderr=subprocess.DEVNULL) except OSError: return {} content = self._to_str(stdout).splitlines()