diff --git a/distro.py b/distro.py index a54e54a..e00832e 100644 --- a/distro.py +++ b/distro.py @@ -868,11 +868,7 @@ def version(self, pretty=False, best=False): ] if self.id() == "debian" or "debian" in self.like().split(): # On Debian-like, add debian_version file content to candidates list. - try: - with open(os.path.join(self.etc_dir, "debian_version")) as fp: - versions.append(fp.readline().rstrip()) - except IOError: - pass + versions.append(self._debian_version) version = "" if best: # This algorithm uses the last version in priority order that has @@ -1199,6 +1195,15 @@ def _uname_info(self): content = self._to_str(stdout).splitlines() return self._parse_uname_content(content) + @cached_property + def _debian_version(self): + # type: () -> str + try: + with open(os.path.join(self.etc_dir, "debian_version")) as fp: + return fp.readline().rstrip() + except (OSError, IOError): + return "" + @staticmethod def _parse_uname_content(lines): # type: (Sequence[str]) -> Dict[str, str] diff --git a/tests/test_distro.py b/tests/test_distro.py index 9ae43b6..e8e417d 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -2258,7 +2258,7 @@ def test_repr(self): repr_str = repr(distro._distro) assert "LinuxDistribution" in repr_str for attr in MODULE_DISTRO.__dict__.keys(): - if attr in ("root_dir", "etc_dir", "usr_lib_dir"): + if attr in ("root_dir", "etc_dir", "usr_lib_dir", "_debian_version"): continue assert attr + "=" in repr_str