libsemanage: improve semanage_migrate_store import failure
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): CAFftDdpNVByPh+fGgVbnd-hR_C=k7RtNM8GUVxLC2bftvfQjtg@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 43 lines · Jason Zaman
Quoted 42 lines · William Roberts
Quoted 22 lines · Yuli Khodorkovskiy
The python module import error in
semanage_migrate_storewas misleading. Before, it would print that the module is not installed, even though it is in fact on the system.Now the python module import failure is correctly reported if the module is not installed or the exact reason for failure is reported to the user.
Signed-off-by Yuli Khodorkovskiy <[email protected]>--- libsemanage/utils/semanage_migrate_store | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)libsemanage/utils/semanage_migrate_store +0
-1@@ -15,10 +15,12 @@ sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') try: import selinux import semanage -except:libsemanage/utils/semanage_migrate_store sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') · line 26
Quoted 10 lines
libsemanage/utils/semanage_migrate_store +4
-1+except ImportError: print("You must install libselinux-python and libsemanage-python before running this tool", file=sys.stderr) exit(1) - +except Exception as e: + print("Failed to import libselinux-python/libsemanage-python: %s" % str(e)) + exit(1)We should really only be handling exceptions we reasonably expect and discourage
the usage of catching raw Exception, especially considering not-catching this will
cause the runtime to print a stack trace, the error and exit non-zero.
Quoted 4 lines
Quoted 4 lines
We probably only need the except ImportError change and can drop the second hunk.
Does anyone disagree with this?
For this case, I agree that ImportError is the only thing that should be caught.
Quoted 2 lines
Agreed. catching Exception is bad cuz it also catches KeyboardInterrupt and stuff like that.
That's not correct. Catching BaseException would catch KeyboardInterrupt. Catching Exception would not. See the Python builtin exception hierarchy:
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
IMO catching Exception has valid uses.
Signature · 1 line
Chris PeBenito
A message that is not in the archive (sent off-list or not yet received): CAFftDdoSdwWwmU+8oT_ugzhSzYvAHQ6BCTzcBUr36snLbJfKpw@mail.gmail.com
Weird Gmail removed my text box for plain text mode in Gmail, re-sending since it got
filtered out of the mailing list.
Quoted 72 lines · William Roberts
Yuli,
If you respin this with just import error looks like its a go. BillQuoted 66 lines · Chris PeBenito
Quoted 43 lines · Jason Zaman
Quoted 42 lines · William Roberts
Quoted 22 lines · Yuli Khodorkovskiy
The python module import error in
semanage_migrate_storewas misleading. Before, it would print that the module is not installed, even though it is in fact on the system.Now the python module import failure is correctly reported if the module is not installed or the exact reason for failure is reported to the user.
Signed-off-by Yuli Khodorkovskiy <[email protected]>--- libsemanage/utils/semanage_migrate_store | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)libsemanage/utils/semanage_migrate_store +0
-1@@ -15,10 +15,12 @@ sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') try: import selinux import semanage -except:libsemanage/utils/semanage_migrate_store sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') · line 26
Quoted 10 lines
libsemanage/utils/semanage_migrate_store +4
-1+except ImportError: print("You must install libselinux-python and libsemanage-python before running this tool", file=sys.stderr) exit(1) - +except Exception as e: + print("Failed to import libselinux-python/libsemanage-python: %s" % str(e)) + exit(1)We should really only be handling exceptions we reasonably expect and discourage
the usage of catching raw Exception, especially considering not-catching this will
cause the runtime to print a stack trace, the error and exit non-zero.Quoted 4 lines
Quoted 4 lines
We probably only need the except ImportError change and can drop the second hunk.
Does anyone disagree with this?
For this case, I agree that ImportError is the only thing that should be caught.
Quoted 2 lines
Agreed. catching Exception is bad cuz it also catches KeyboardInterrupt and stuff like that.
That's not correct. Catching BaseException would catch KeyboardInterrupt. Catching Exception would not. See the Python builtin exception hierarchy:
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
IMO catching Exception has valid uses.
--
Chris PeBenito
Quoted 81 lines · William Roberts
Weird Gmail removed my text box for plain text mode in Gmail, re-sending since it got
filtered out of the mailing list.Quoted 72 lines · William Roberts
Yuli,
If you respin this with just import error looks like its a go. BillQuoted 66 lines · Chris PeBenito
Quoted 43 lines · Jason Zaman
Quoted 42 lines · William Roberts
Quoted 22 lines · Yuli Khodorkovskiy
The python module import error in
semanage_migrate_storewas misleading. Before, it would print that the module is not installed, even though it is in fact on the system.Now the python module import failure is correctly reported if the module is not installed or the exact reason for failure is reported to the user.
Signed-off-by Yuli Khodorkovskiy <[email protected]>--- libsemanage/utils/semanage_migrate_store | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)libsemanage/utils/semanage_migrate_store +0
-1@@ -15,10 +15,12 @@ sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') try: import selinux import semanage -except:libsemanage/utils/semanage_migrate_store sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') · line 26
Quoted 10 lines
libsemanage/utils/semanage_migrate_store +4
-1+except ImportError: print("You must install libselinux-python and libsemanage-python before running this tool", file=sys.stderr) exit(1) - +except Exception as e: + print("Failed to import libselinux-python/libsemanage-python: %s" % str(e)) + exit(1)We should really only be handling exceptions we reasonably expect and discourage
the usage of catching raw Exception, especially considering not-catching this will
cause the runtime to print a stack trace, the error and exit non-zero.Quoted 4 lines
Quoted 4 lines
We probably only need the except ImportError change and can drop the second hunk.
Does anyone disagree with this?
For this case, I agree that ImportError is the only thing that should be caught.
Quoted 2 lines
Agreed. catching Exception is bad cuz it also catches KeyboardInterrupt and stuff like that.
That's not correct. Catching BaseException would catch KeyboardInterrupt. Catching Exception would not. See the Python builtin exception hierarchy:
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
IMO catching Exception has valid uses.
--
Chris PeBenito
The reason I went the path of catching general exception was the possibility of a missing symbol from _selinux.so. For example:
Traceback (most recent call last):
File "libsemanage/utils/semanage_migrate_store", line 16, in <module>
import selinux
File "/usr/lib64/python2.7/site-packages/selinux/__init__.py", line 512, in <module>
SELABEL_CTX_ANDROID_SERVICE = _selinux.SELABEL_CTX_ANDROID_SERVICE
AttributeError: 'module' object has no attribute ‘SELABEL_CTX_ANDROID_SERVICE'
Should I just catch AttributeError as well and if more exceptions come up in the future we can catch them directly?
Quoted 93 lines · Yuli Khodorkovskiy
Quoted 82 lines · William Roberts
Weird Gmail removed my text box for plain text mode in Gmail, re-sending since it got
filtered out of the mailing list.Quoted 72 lines · William Roberts
Yuli,
If you respin this with just import error looks like its a go. BillQuoted 66 lines · Chris PeBenito
Quoted 43 lines · Jason Zaman
Quoted 42 lines · William Roberts
Quoted 22 lines · Yuli Khodorkovskiy
The python module import error in
semanage_migrate_storewas misleading. Before, it would print that the module is not installed, even though it is in fact on the system.Now the python module import failure is correctly reported if the module is not installed or the exact reason for failure is reported to the user.
Signed-off-by Yuli Khodorkovskiy <[email protected]>--- libsemanage/utils/semanage_migrate_store | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)libsemanage/utils/semanage_migrate_store +0
-1@@ -15,10 +15,12 @@ sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') try: import selinux import semanage -except:libsemanage/utils/semanage_migrate_store sepol = ctypes.cdll.LoadLibrary('libsepol.so.1') · line 26
Quoted 10 lines
libsemanage/utils/semanage_migrate_store +4
-1+except ImportError: print("You must install libselinux-python and libsemanage-python before running this tool", file=sys.stderr) exit(1) - +except Exception as e: + print("Failed to import libselinux-python/libsemanage-python: %s" % str(e)) + exit(1)We should really only be handling exceptions we reasonably expect and discourage
the usage of catching raw Exception, especially considering not-catching this will
cause the runtime to print a stack trace, the error and exit non-zero.Quoted 4 lines
Quoted 4 lines
We probably only need the except ImportError change and can drop the second hunk.
Does anyone disagree with this?
For this case, I agree that ImportError is the only thing that should be caught.
Quoted 2 lines
Agreed. catching Exception is bad cuz it also catches KeyboardInterrupt and stuff like that.
That's not correct. Catching BaseException would catch KeyboardInterrupt. Catching Exception would not. See the Python builtin exception hierarchy:
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
IMO catching Exception has valid uses.
--
Chris PeBenitoThe reason I went the path of catching general exception was the possibility of a missing symbol from _selinux.so. For example:
Traceback (most recent call last):
File "libsemanage/utils/semanage_migrate_store", line 16, in <module>
import selinux
File "/usr/lib64/python2.7/site-packages/selinux/__init__.py", line 512, in <module>
SELABEL_CTX_ANDROID_SERVICE= _selinux.SELABEL_CTX_ANDROID_SERVICE
AttributeError: 'module' object has no attribute ‘SELABEL_CTX_ANDROID_SERVICE'
Quoted 2 lines
Should I just catch AttributeError as well and if more exceptions come up in the future we can catch them directly?
Yes, especially when you're not doing anything better than the default exception mechanism
itself. In fact you kill the stack trace without providing a better diagnostic for users. So catch
Exceptions but provide something better then what the exception mechanism is doing.