Skip to contentSkip to conversation

python/sepolicy: Fix get_os_version except clause

PATCH Applied Jeffery To 2 participants 3 messages Jun 19, 2023 – Jun 30, 2023 SELinux security module

lore
Share & tools
lore

Jeffery To

author
#0 lore raw

PATCH 1/1 python/sepolicy: Fix get_os_version except clause

This adds more exceptions to be handled by the except clause in get_os_version():

* If the distro package is not installed, then import distro raises a ModuleNotFoundError exception.

* The distro documentation[1] lists OSError and UnicodeError as exceptions that can be raised.

* Older versions of distro (<= 1.6.0) may also raise subprocessCalledProcessError[2].

[1]: https://github.com/python-distro/distro/blob/v1.8.0/src/distro/distro.py#L749-L753
[2]: https://github.com/python-distro/distro/blob/v1.6.0/distro.py#L726-L728

---
 python/sepolicy/sepolicy/__init__.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
python/sepolicy/sepolicy/__init__.py +2 -1
index c177cdfc529d..2d526c94a0c5 100644
@@ -1240,11 +1240,12 @@ def boolean_desc(boolean):
 
 
 def get_os_version():
+    import subprocess
     system_release = ""
     try:
         import distro
         system_release = distro.name(pretty=True)
-    except IOError:
+    except (ModuleNotFoundError, OSError, IOError, UnicodeError, subprocess.CalledProcessError):
         system_release = "Misc"
 
     return system_release
Signature · 1 line
2.39.2
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Jeffery To

This adds more exceptions to be handled by the except clause in get_os_version():

* If the distro package is not installed, then import distro raises a ModuleNotFoundError exception.

* The distro documentation[1] lists OSError and UnicodeError as exceptions that can be raised.

Quoted 7 lines

* Older versions of distro (<= 1.6.0) may also raise subprocessCalledProcessError[2].

[1]: https://github.com/python-distro/distro/blob/v1.8.0/src/distro/distro.py#L749-L753
[2]: https://github.com/python-distro/distro/blob/v1.6.0/distro.py#L726-L728

Quoted 24 lines
---
 python/sepolicy/sepolicy/__init__.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
python/sepolicy/sepolicy/__init__.py +2 -1
index c177cdfc529d..2d526c94a0c5 100644
@@ -1240,11 +1240,12 @@ def boolean_desc(boolean):
 
 
 def get_os_version():
+    import subprocess
     system_release = ""
     try:
         import distro
         system_release = distro.name(pretty=True)
-    except IOError:
+    except (ModuleNotFoundError, OSError, IOError, UnicodeError, subprocess.CalledProcessError):
         system_release = "Misc"
 
     return system_release
Signature · 1 line
2.39.2
#2 lore raw
Quoted 14 lines · Petr Lautrbach
Quoted 12 lines · Jeffery To

This adds more exceptions to be handled by the except clause in get_os_version():

* If the distro package is not installed, then import distro raises a ModuleNotFoundError exception.

* The distro documentation[1] lists OSError and UnicodeError as exceptions that can be raised.

* Older versions of distro (<= 1.6.0) may also raise subprocessCalledProcessError[2].

Quoted 6 lines
Quoted 4 lines

[1]: https://github.com/python-distro/distro/blob/v1.8.0/src/distro/distro.py#L749-L753
[2]: https://github.com/python-distro/distro/blob/v1.6.0/distro.py#L726-L728

merged, thanks!

Quoted 24 lines
Quoted 24 lines
---
 python/sepolicy/sepolicy/__init__.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
python/sepolicy/sepolicy/__init__.py +2 -1
index c177cdfc529d..2d526c94a0c5 100644
@@ -1240,11 +1240,12 @@ def boolean_desc(boolean):
 
 
 def get_os_version():
+    import subprocess
     system_release = ""
     try:
         import distro
         system_release = distro.name(pretty=True)
-    except IOError:
+    except (ModuleNotFoundError, OSError, IOError, UnicodeError, subprocess.CalledProcessError):
         system_release = "Misc"
 
     return system_release
Signature · 1 line
2.39.2