diff mbox series

python/sepolicy: Fix get_os_version except clause

Message ID 20230619063217.3165462-1-jeffery.to@gmail.com (mailing list archive)
State Accepted
Commit 1a29c28afbb9
Delegated to: Petr Lautrbach
Headers show
Series python/sepolicy: Fix get_os_version except clause | expand

Commit Message

Jeffery To June 19, 2023, 6:32 a.m. UTC
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

Signed-off-by: Jeffery To <jeffery.to@gmail.com>
---
 python/sepolicy/sepolicy/__init__.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Petr Lautrbach June 29, 2023, 3:07 p.m. UTC | #1
Jeffery To <jeffery.to@gmail.com> writes:

> 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
>
> Signed-off-by: Jeffery To <jeffery.to@gmail.com>

Acked-by: Petr Lautrbach <lautrbach@redhat.com>

> ---
>  python/sepolicy/sepolicy/__init__.py | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py
> index c177cdfc529d..2d526c94a0c5 100644
> --- a/python/sepolicy/sepolicy/__init__.py
> +++ b/python/sepolicy/sepolicy/__init__.py
> @@ -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
> -- 
> 2.39.2
Petr Lautrbach June 30, 2023, 12:29 p.m. UTC | #2
Petr Lautrbach <lautrbach@redhat.com> writes:

> Jeffery To <jeffery.to@gmail.com> writes:
>
>> 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
>>
>> Signed-off-by: Jeffery To <jeffery.to@gmail.com>
>
> Acked-by: Petr Lautrbach <lautrbach@redhat.com>

merged, thanks!


>> ---
>>  python/sepolicy/sepolicy/__init__.py | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py
>> index c177cdfc529d..2d526c94a0c5 100644
>> --- a/python/sepolicy/sepolicy/__init__.py
>> +++ b/python/sepolicy/sepolicy/__init__.py
>> @@ -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
>> -- 
>> 2.39.2
diff mbox series

Patch

diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py
index c177cdfc529d..2d526c94a0c5 100644
--- a/python/sepolicy/sepolicy/__init__.py
+++ b/python/sepolicy/sepolicy/__init__.py
@@ -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