Fix some issues found by flake8
cover Fix some issues found by flake8
Hi,
I have been working on a script which uses flake8 to discover issues in Python code. This led me to discover several issues which are fixed by these patches. Distribution maintainers might be interested in backporting some of them (at least patches 5 and 10 and probably the ones which fix usage of undefined variables).
As Travis-CI is experiencing issues today (it fails to launch new builds), I have not been able to test the integration of my script with Travis-CI yet. Once it works again I will submit this script too.
Thanks,
Nicolas
---
Here comes the description generated by "git format-patch --cover":
Nicolas Iooss (13):
libselinux: fix flake8 warnings in SWIG-generated code python/sepolgen: do not import twice the modules python/sepolgen: return NotImplemented instead of raising it python/sepolicy: drop unused CheckPolicyType python/sepolicy: use lowercase variable name python/sepolgen: fix refpolicy parsing of "permissive" python/sepolgen: silence linter warning about has_key python/sepolgen: comment buggy code
python/sepolgen: use self when accessing members in FilesystemUse python/sepolicy: fix "procotol" misspelling python/sepolicy: use variables which exists in the gui.py python/sepolicy: do not import sepolicy.generate.DAEMON twice python/sepolicy: do not import types
libselinux/src/selinuxswig_python.i | 5 +++-- python/sepolgen/src/sepolgen/interfaces.py | 10 +++++----- python/sepolgen/src/sepolgen/refparser.py | 4 ++-- python/sepolgen/src/sepolgen/refpolicy.py | 6 +++--- python/sepolgen/src/sepolgen/util.py | 6 +----- python/sepolicy/sepolicy.py | 12 +----------- python/sepolicy/sepolicy/generate.py | 1 - python/sepolicy/sepolicy/gui.py | 12 ++++++------ 8 files changed, 21 insertions(+), 35 deletions(-)
Signature · 1 line
2.18.0
PATCH 1/13 libselinux: fix flake8 warnings in SWIG-generated code
flake8, a Python linter, complains:
libselinux/src/selinux.py:100:1: F401 'errno' imported but unused libselinux/src/selinux.py:100:1: F401 'stat' imported but unused libselinux/src/selinux.py:100:14: E401 multiple imports on one line libselinux/src/selinux.py:130:16: E111 indentation is not a multiple of four libselinux/src/selinux.py:130:45: E231 missing whitespace after ','
--- libselinux/src/selinuxswig_python.i | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/src/selinuxswig_python.i +3 -2
@@ -13,7 +13,8 @@ %pythoncode %{ -import shutil, os, errno, stat +import shutil +import os DISABLED = -1 PERMISSIVE = 0
@@ -43,7 +44,7 @@ def chcon(path, context, recursive=False): if recursive: for root, dirs, files in os.walk(path): for name in files + dirs: - lsetfilecon(os.path.join(root,name), context) + lsetfilecon(os.path.join(root, name), context) def copytree(src, dest): """ An SELinux-friendly shutil.copytree method """
Signature · 1 line
2.18.0
PATCH 2/13 python/sepolgen: do not import twice the modules
In reply to Nicolas Ioossutil.py starts by importing "locale" and "sys", so there is no need to import these modules again in the functions.
--- python/sepolgen/src/sepolgen/util.py | 4 ---- 1 file changed, 4 deletions(-)
python/sepolgen/src/sepolgen/util.py +0 -4
@@ -90,7 +90,6 @@ def first(s, sorted=False): return x def encode_input(text): - import locale """Encode given text via preferred system encoding""" # locale will often find out the correct encoding encoding = locale.getpreferredencoding()
@@ -104,7 +103,6 @@ def encode_input(text): return encoded_text def decode_input(text): - import locale """Decode given text via preferred system encoding""" # locale will often find out the correct encoding encoding = locale.getpreferredencoding()
@@ -172,11 +170,9 @@ def cmp(first, second): return (first > second) - (second > first) if __name__ == "__main__": - import sys import time p = ConsoleProgressBar(sys.stdout, steps=999) p.start("computing pi") for i in range(999): p.step() time.sleep(0.001) -
Signature · 1 line
2.18.0
PATCH 3/13 python/sepolgen: return NotImplemented instead of raising it
In reply to Nicolas Ioosssepolgen uses "return NotImplemented" (in access.py and matching.py) in order to make Python's sorting function use an other call to compare objects. For this to work, "NotImplemented" needs to be returned, not raised like _compare's default implementation does.
This issue has been found using flake8. This Python linter reported:
python/sepolgen/src/sepolgen/util.py:128:9: F901 'raise NotImplemented' should be 'raise NotImplementedError'
--- python/sepolgen/src/sepolgen/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/util.py +1 -1
@@ -123,7 +123,7 @@ class Comparison(): _compare function within your class.""" def _compare(self, other, method): - raise NotImplemented + return NotImplemented def __eq__(self, other): return self._compare(other, lambda a, b: a == b)
Signature · 1 line
2.18.0
PATCH 4/13 python/sepolicy: drop unused CheckPolicyType
In reply to Nicolas IoossCheckPolicyType class is not used in sepolicy and has been written from an buggy copy-paste: "newval.append(v)" does not make any sense in CheckPolicyType.__call__, as reported by flake8:
python/sepolicy/sepolicy.py:190:13: F821 undefined name 'newval' python/sepolicy/sepolicy.py:190:27: F821 undefined name 'v'
--- python/sepolicy/sepolicy.py | 10 ---------- 1 file changed, 10 deletions(-)
python/sepolicy/sepolicy.py +0 -10
@@ -181,16 +181,6 @@ class LoadPolicy(argparse.Action): setattr(namespace, self.dest, values) -class CheckPolicyType(argparse.Action): - - def __call__(self, parser, namespace, values, option_string=None): - from sepolicy.generate import get_poltype_desc, poltype - if values not in poltype.keys(): - raise ValueError("%s invalid SELinux policy type\n%s" % (values, get_poltype_desc())) - newval.append(v) - setattr(namespace, self.dest, values) - - class CheckUser(argparse.Action): def __call__(self, parser, namespace, value, option_string=None):
Signature · 1 line
2.18.0
PATCH 5/13 python/sepolicy: use lowercase variable name
In reply to Nicolas IoossSELinuxGui.files_initial_data_insert() uses both "seLinux_label" and "selinux_label", which leads to using an undefined variable, as reported by flake8:
python/sepolicy/sepolicy/gui.py:1191:42: F821 undefined name 'selinux_label'
--- python/sepolicy/sepolicy/gui.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
python/sepolicy/sepolicy/gui.py +2 -2
@@ -1175,7 +1175,7 @@ class SELinuxGui(): continue self.files_initial_data_insert(self.writable_files_liststore, path, write, file_class) - def files_initial_data_insert(self, liststore, path, seLinux_label, file_class): + def files_initial_data_insert(self, liststore, path, selinux_label, file_class): iter = liststore.append(None) if path is None: path = _("MISSING FILE PATH")
@@ -1191,7 +1191,7 @@ class SELinuxGui(): file_class = self.markup(selinux_label) file_class = self.markup(file_class) liststore.set_value(iter, 0, path) - liststore.set_value(iter, 1, seLinux_label) + liststore.set_value(iter, 1, selinux_label) liststore.set_value(iter, 2, file_class) liststore.set_value(iter, 7, modify)
Signature · 1 line
2.18.0
PATCH 6/13 python/sepolgen: fix refpolicy parsing of "permissive"
In reply to Nicolas Ioossp_permissive() uses an undefined variable t, which is buggy. In order to ignore permissive statements, the function only needs to "pass".
flake8 reported the following error:
python/sepolgen/src/sepolgen/refparser.py:789:5: F821 undefined name 't'
--- python/sepolgen/src/sepolgen/refparser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/refparser.py +1 -1
@@ -786,7 +786,7 @@ def p_role_allow(p): def p_permissive(p): 'permissive : PERMISSIVE names SEMI' - t.skip(1) + pass def p_avrule_def(p): '''avrule_def : ALLOW names names COLON names names SEMI
Signature · 1 line
2.18.0
PATCH 7/13 python/sepolgen: silence linter warning about has_key
In reply to Nicolas Ioossflake8 warns about using has_key():
python/sepolgen/src/sepolgen/refparser.py:315:15: W601 .has_key() is deprecated, use 'in'
However "spt.has_key(id)" uses function SupportMacros.has_key(), which is not the same as the "in" operator. Silence this warning by using "# noqa".
--- python/sepolgen/src/sepolgen/refparser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/refparser.py +1 -1
@@ -312,7 +312,7 @@ def collect(stmts, parent, val=None): def expand(ids, s): for id in ids: - if spt.has_key(id): + if spt.has_key(id): # noqa s.update(spt.by_name(id)) else: s.add(id)
Signature · 1 line
2.18.0
PATCH 8/13 python/sepolgen: comment buggy code
In reply to Nicolas Ioossav_extract_params() may call __param_insert() with only 2 parameters instead of 4, which has no chance to work fine. Moreover it uses "PERM", which is undefined. As nobody complained about this code, it seems to be dead, so comment it.
This issue has been found using flake8. This Python linter reported:
python/sepolgen/src/sepolgen/interfaces.py:158:37: F821 undefined name 'PERM'
While at it, fix a typo in a comment.
--- python/sepolgen/src/sepolgen/interfaces.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
python/sepolgen/src/sepolgen/interfaces.py +5 -5
@@ -112,7 +112,7 @@ def av_extract_params(av, params): entries in the dict, but if an unresolvable conflict is found it is reported to the caller. - The goal here is to figure out how interface paramaters are + The goal here is to figure out how interface parameters are actually used in the interface - e.g., that $1 is a domain used as a SRC_TYPE. In general an interface will look like this:
@@ -153,10 +153,10 @@ def av_extract_params(av, params): if __param_insert(av.obj_class, refpolicy.OBJ_CLASS, av, params) == 1: ret = 1 - for perm in av.perms: - if access.is_idparam(perm): - if __param_insert(perm, PERM) == 1: - ret = 1 + #for perm in av.perms: + # if access.is_idparam(perm): + # if __param_insert(perm, PERM) == 1: + # ret = 1 return ret
Signature · 1 line
2.18.0
PATCH 9/13 python/sepolgen: use self when accessing members in FilesystemUse
In reply to Nicolas IoossThis silences the following flake8 errors:
python/sepolgen/src/sepolgen/refpolicy.py:758:25: F821 undefined name 'XATTR' python/sepolgen/src/sepolgen/refpolicy.py:760:27: F821 undefined name 'TRANS' python/sepolgen/src/sepolgen/refpolicy.py:762:27: F821 undefined name 'TASK'
--- python/sepolgen/src/sepolgen/refpolicy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
python/sepolgen/src/sepolgen/refpolicy.py +3 -3
@@ -755,11 +755,11 @@ class FilesystemUse(Leaf): def to_string(self): s = "" - if self.type == XATTR: + if self.type == self.XATTR: s = "fs_use_xattr " - elif self.type == TRANS: + elif self.type == self.TRANS: s = "fs_use_trans " - elif self.type == TASK: + elif self.type == self.TASK: s = "fs_use_task " return "%s %s %s;" % (s, self.filesystem, str(self.context))
Signature · 1 line
2.18.0
PATCH 10/13 python/sepolicy: fix "procotol" misspelling
In reply to Nicolas Ioossprocotol -> protocol
This issue has been found using flake8. This Python linter reported:
python/sepolicy/sepolicy/gui.py:2525:132: F821 undefined name 'procotol'
--- python/sepolicy/sepolicy/gui.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolicy/sepolicy/gui.py +1 -1
@@ -2522,7 +2522,7 @@ class SELinuxGui(): if self.cur_dict[k][(port, protocol)]["action"] == "-d": update_buffer += "port -d -p %s %s\n" % (protocol, port) else: - update_buffer += "port %s -t %s -p %s %s\n" % (self.cur_dict[k][f]["action"], self.cur_dict[k][f]["type"], procotol, port) + update_buffer += "port %s -t %s -p %s %s\n" % (self.cur_dict[k][f]["action"], self.cur_dict[k][f]["type"], protocol, port) return update_buffer
Signature · 1 line
2.18.0
PATCH 11/13 python/sepolicy: use variables which exists in the gui.py
In reply to Nicolas IoossSome function used variables which were not defined when populating self.cur_dict with an operation. Fix this, even though the old values do not seem to be used.
This issue has been found using flake8. This Python linter reported:
python/sepolicy/sepolicy/gui.py:2020:101: F821 undefined name 'oldsetype'
python/sepolicy/sepolicy/gui.py:2020:122: F821 undefined name 'oldmls'
python/sepolicy/sepolicy/gui.py:2020:142: F821 undefined name 'oldclass'
python/sepolicy/sepolicy/gui.py:2050:133: F821 undefined name 'oldmls'--- python/sepolicy/sepolicy/gui.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
python/sepolicy/sepolicy/gui.py +3 -3
@@ -2015,9 +2015,9 @@ class SELinuxGui(): if self.modify: iter = self.get_selected_iter() oldpath = self.unmark(self.liststore.get_value(iter, 0)) - setype = self.unmark(self.liststore.set_value(iter, 1)) + oldsetype = self.unmark(self.liststore.set_value(iter, 1)) oldtclass = self.liststore.get_value(iter, 2) - self.cur_dict["fcontext"][(path, tclass)] = {"action": "-m", "type": setype, "oldtype": oldsetype, "oldmls": oldmls, "oldclass": oldclass} + self.cur_dict["fcontext"][(path, tclass)] = {"action": "-m", "type": setype, "oldtype": oldsetype, "oldpath": oldpath, "oldclass": oldtclass} else: iter = self.liststore.append(None) self.cur_dict["fcontext"][(path, tclass)] = {"action": "-a", "type": setype}
@@ -2047,7 +2047,7 @@ class SELinuxGui(): oldports = self.unmark(self.liststore.get_value(iter, 0)) oldprotocol = self.unmark(self.liststore.get_value(iter, 1)) oldsetype = self.unmark(self.liststore.set_value(iter, 2)) - self.cur_dict["port"][(ports, protocol)] = {"action": "-m", "type": setype, "mls": mls, "oldtype": oldsetype, "oldmls": oldmls, "oldprotocol": oldprotocol, "oldports": oldports} + self.cur_dict["port"][(ports, protocol)] = {"action": "-m", "type": setype, "mls": mls, "oldtype": oldsetype, "oldprotocol": oldprotocol, "oldports": oldports} else: iter = self.liststore.append(None) self.cur_dict["port"][(ports, protocol)] = {"action": "-a", "type": setype, "mls": mls}
Signature · 1 line
2.18.0
PATCH 12/13 python/sepolicy: do not import sepolicy.generate.DAEMON twice
In reply to Nicolas IoossThis issue has been found using flake8. This Python linter reported:
python/sepolicy/sepolicy.py:582:5: F811 redefinition of unused 'DAEMON' from line 582
--- python/sepolicy/sepolicy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolicy/sepolicy.py +1 -1
@@ -579,7 +579,7 @@ def gen_interface_args(parser): def gen_generate_args(parser): - from sepolicy.generate import DAEMON, get_poltype_desc, poltype, DAEMON, DBUS, INETD, CGI, SANDBOX, USER, EUSER, TUSER, XUSER, LUSER, AUSER, RUSER, NEWTYPE + from sepolicy.generate import get_poltype_desc, poltype, DAEMON, DBUS, INETD, CGI, SANDBOX, USER, EUSER, TUSER, XUSER, LUSER, AUSER, RUSER, NEWTYPE generate_usage = generate_custom_usage(usage, usage_dict)
Signature · 1 line
2.18.0
PATCH 13/13 python/sepolicy: do not import types
In reply to Nicolas IoossThis module is not used and its name conflicts with variable "types".
This issue has been found using flake8. This Python linter reported:
python/sepolicy/sepolicy/generate.py:657:5: F811 redefinition of unused 'types' from line 31
--- python/sepolicy/sepolicy/generate.py | 1 - 1 file changed, 1 deletion(-)
python/sepolicy/sepolicy/generate.py +0 -1
@@ -28,7 +28,6 @@ import re import sepolicy from sepolicy import get_all_types, get_all_attributes, get_all_roles import time -import types import platform from .templates import executable
Signature · 1 line
2.18.0
Quoted 18 lines · Nicolas Iooss
Hi,
I have been working on a script which uses flake8 to discover issues in Python code. This led me to discover several issues which are fixed by these patches. Distribution maintainers might be interested in backporting some of them (at least patches 5 and 10 and probably the ones which fix usage of undefined variables).
As Travis-CI is experiencing issues today (it fails to launch new builds), I have not been able to test the integration of my script with Travis-CI yet. Once it works again I will submit this script too.
Thanks,
Nicolas---
Here comes the description generated by "git format-patch --cover":
Quoted 8 lines
Nicolas Iooss (13):
libselinux: fix flake8 warnings in SWIG-generated code python/sepolgen: do not import twice the modules python/sepolgen: return NotImplemented instead of raising it python/sepolicy: drop unused CheckPolicyType python/sepolicy: use lowercase variable name python/sepolgen: fix refpolicy parsing of "permissive"
python/sepolgen: silence linter warning about has_key
Quoted 1 line
This is the only one I don't particularly like:
Quoted 2 lines
python/sepolgen: comment buggy code
If we choose to comment out it out, a block comment explaining why would be helpful or just delete it if it is dead code.
python/sepolgen: use self when accessing members in FilesystemUse
Quoted 25 lines
python/sepolicy: fix "procotol" misspelling python/sepolicy: use variables which exists in the
gui.pypython/sepolicy: do not import sepolicy.generate.DAEMON twice python/sepolicy: do not import typeslibselinux/src/selinuxswig_python.i | 5 +++-- python/sepolgen/src/sepolgen/interfaces.py | 10 +++++----- python/sepolgen/src/sepolgen/refparser.py | 4 ++-- python/sepolgen/src/sepolgen/refpolicy.py | 6 +++--- python/sepolgen/src/sepolgen/util.py | 6 +----- python/sepolicy/sepolicy.py | 12 +----------- python/sepolicy/sepolicy/generate.py | 1 - python/sepolicy/sepolicy/gui.py | 12 ++++++------ 8 files changed, 21 insertions(+), 35 deletions(-)--
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].
Quoted 33 lines · William Roberts
Quoted 27 lines · Nicolas Iooss
Hi,
I have been working on a script which uses flake8 to discover issues in Python code. This led me to discover several issues which are fixed by these patches. Distribution maintainers might be interested in backporting some of them (at least patches 5 and 10 and probably the ones which fix usage of undefined variables).
As Travis-CI is experiencing issues today (it fails to launch new builds), I have not been able to test the integration of my script with Travis-CI yet. Once it works again I will submit this script too.
Thanks,
Nicolas---
Here comes the description generated by "git format-patch --cover":Nicolas Iooss (13):
libselinux: fix flake8 warnings in SWIG-generated code python/sepolgen: do not import twice the modules python/sepolgen: return NotImplemented instead of raising it python/sepolicy: drop unused CheckPolicyType python/sepolicy: use lowercase variable name python/sepolgen: fix refpolicy parsing of "permissive"Quoted 1 line
python/sepolgen: silence linter warning about
has_key
Quoted 7 lines
This is the only one I don't particularly like:
Quoted 2 lines
python/sepolgen: comment buggy code
If we choose to comment out it out, a block comment explaining why would be helpful or just delete it if it is dead code.
It feels like this code could be useful if an interface parameter ("$1") is used as a permission, which is why I have not deleted it. I suggest adding the following comment:
# This function currently ignores parameters which are used in permission. # The following code has been present for a long time and contains two # syntax errors (__param_insert takes 4 arguments and PERM is not defined) # which proves that it is actually dead code.
#for perm in av.perms:
# if access.is_idparam(perm):
# if __param_insert(perm, PERM) == 1:
# ret = 1
Does this suit you? By the way, I do not have a strong opinion about commenting or deleting it, so I am fine if we choose to remove this code instead.
Thanks for your review,
Nicolas
Quoted 59 lines · Nicolas Iooss
Quoted 33 lines · William Roberts
Quoted 27 lines · Nicolas Iooss
Hi,
I have been working on a script which uses flake8 to discover issues in Python code. This led me to discover several issues which are fixed by these patches. Distribution maintainers might be interested in backporting some of them (at least patches 5 and 10 and probably the ones which fix usage of undefined variables).
As Travis-CI is experiencing issues today (it fails to launch new builds), I have not been able to test the integration of my script with Travis-CI yet. Once it works again I will submit this script too.
Thanks,
Nicolas---
Here comes the description generated by "git format-patch --cover":Nicolas Iooss (13):
libselinux: fix flake8 warnings in SWIG-generated code python/sepolgen: do not import twice the modules python/sepolgen: return NotImplemented instead of raising it python/sepolicy: drop unused CheckPolicyType python/sepolicy: use lowercase variable name python/sepolgen: fix refpolicy parsing of "permissive"Quoted 1 line
python/sepolgen: silence linter warning about
has_keyQuoted 6 lines
This is the only one I don't particularly like:
Quoted 2 lines
python/sepolgen: comment buggy code
If we choose to comment out it out, a block comment explaining why would
be
Quoted 1 line
helpful or just delete it if it is dead code.
It feels like this code could be useful if an interface parameter ("$1") is used as a permission, which is why I have not deleted it. I suggest adding the following comment:
# This function currently ignores parameters which are used in
permission.
# The following code has been present for a long time and contains two # syntax errors (__param_inserttakes 4 arguments and PERM is not
defined)# which proves that it is actually dead code. #for perm in av.perms: # if access.is_idparam(perm): # if __param_insert(perm, PERM) == 1: # ret = 1
Quoted 4 lines
Does this suit you? By the way, I do not have a strong opinion about commenting or deleting it, so I am fine if we choose to remove this code instead.
I'm all for deleting it. I think we should just do that.
Quoted 5 lines
Thanks for your review,
Nicolas