Skip to contentSkip to conversation

python/semanage: fix Python syntax of catching several exceptions

PATCH In review Nicolas Iooss 2 participants 4 messages Aug 4, 2018 – Aug 6, 2018 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 python/semanage: fix Python syntax of catching several exceptions

"except OSError, ImportError:" does not perform what it says: it is the Python 2 syntax of catching OSError exceptions as "ImportError" (like "except OSError, e:"), and this is indeed caught by Python3:

File "./python/semanage/seobject.py", line 143
except OSError, ImportError:
^
SyntaxError: invalid syntax

The correct syntax consists in using parentheses.

---
For the record, I found this while playing with flake8 Python's linter. I am currently working on upstreaming a script which would run this on our Travis-CI environment in order to catch Python syntax errors before they are introduced.

 python/semanage/seobject.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/semanage/seobject.py +1 -1
index 78ffb091ad2f..c14671855049 100644
@@ -140,7 +140,7 @@ try:
 
             self.log_list = []
             self.log_change_list = []
-except OSError, ImportError:
+except (OSError, ImportError):
     class logger:
 
         def __init__(self):
Signature · 1 line
2.18.0
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Nicolas Iooss

"except OSError, ImportError:" does not perform what it says: it is the Python 2 syntax of catching OSError exceptions as "ImportError" (like "except OSError, e:"), and this is indeed caught by Python3:

Quoted 8 lines

File "./python/semanage/seobject.py", line 143
except OSError, ImportError:
^
SyntaxError: invalid syntax

The correct syntax consists in using parentheses.

Quoted 5 lines

---
For the record, I found this while playing with flake8 Python's linter. I am currently working on upstreaming a script which would run this on our Travis-CI environment in order to catch Python syntax errors before they are introduced.

Awesome! more linters are always good. I really do need to find the time to kill seobject more thoroughly tho, its so slow compared to direct setools. :(

-- Jason

Quoted 25 lines
 python/semanage/seobject.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/semanage/seobject.py +1 -1
index 78ffb091ad2f..c14671855049 100644
@@ -140,7 +140,7 @@ try:
 
             self.log_list = []
             self.log_change_list = []
-except OSError, ImportError:
+except (OSError, ImportError):
     class logger:
 
         def __init__(self):
Signature · 7 lines
2.18.0

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].
#2 lore raw
Quoted 10 lines · Jason Zaman
Quoted 9 lines · Nicolas Iooss

"except OSError, ImportError:" does not perform what it says: it is the Python 2 syntax of catching OSError exceptions as "ImportError" (like "except OSError, e:"), and this is indeed caught by Python3:

File "./python/semanage/seobject.py", line 143
except OSError, ImportError:
^
SyntaxError: invalid syntax

Quoted 5 lines
Quoted 3 lines

The correct syntax consists in using parentheses.

Thanks. I will wait one business day before merging this (in order to let other developers the possibility to comment, even though this change is trivial).

Quoted 9 lines
Quoted 5 lines

---
For the record, I found this while playing with flake8 Python's linter. I am currently working on upstreaming a script which would run this on our Travis-CI environment in order to catch Python syntax errors before they are introduced.

Awesome! more linters are always good. I really do need to find the time to kill seobject more thoroughly tho, its so slow compared to direct setools. :(

The more I look into flake8, the more I want to make it integrated to our review process... I will send a few patches later today in order to fix issues that it found in the Python code (for example "procotol" instead of "protocol" in sepolicy/gui.py).

Cheers,
Nicolas

#3 lore raw
Quoted 13 lines · Nicolas Iooss
Quoted 12 lines · Jason Zaman
Quoted 11 lines · Nicolas Iooss

"except OSError, ImportError:" does not perform what it says: it is the Python 2 syntax of catching OSError exceptions as "ImportError" (like "except OSError, e:"), and this is indeed caught by Python3:

File "./python/semanage/seobject.py", line 143
except OSError, ImportError:
^
SyntaxError: invalid syntax

The correct syntax consists in using parentheses.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Thanks. I will wait one business day before merging this (in order to let other developers the possibility to comment, even though this change is trivial).

I applied this patch.
Nicolas