sepolgen: convert cmp functions to key functions
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 sepolgen: convert cmp functions to key functions
In python3 it is needed to pass compare function as a key argument instead of directly passing compare function to sort function
Signed-off-by Robert Kuska <[email protected]>
--- sepolgen/src/sepolgen/output.py | 4 ++-- sepolgen/src/sepolgen/util.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-)
sepolgen/src/sepolgen/output.py +2 -2
@@ -131,7 +131,7 @@ def sort_filter(module): rules = [] rules.extend(node.avrules()) rules.extend(node.interface_calls()) - rules.sort(rule_cmp) + rules.sort(key=util.cmp_to_key(rule_cmp)) cur = None sep_rules = []
@@ -155,7 +155,7 @@ def sort_filter(module): ras = [] ras.extend(node.role_types()) - ras.sort(role_type_cmp) + ras.sort(key=util.cmp_to_key(role_type_cmp)) if len(ras): comment = refpolicy.Comment() comment.lines.append("============= ROLES ==============")
sepolgen/src/sepolgen/util.py +23 -0
@@ -145,6 +145,29 @@ class Comparison(): def __ne__(self, other): return self._compare(other, lambda a, b: a != b) +if sys.version_info < (2,7): + # cmp_to_key function is missing in python2.6 + def cmp_to_key(mycmp): + 'Convert a cmp= function into a key= function' + class K: + def __init__(self, obj, *args): + self.obj = obj + def __lt__(self, other): + return mycmp(self.obj, other.obj) < 0 + def __gt__(self, other): + return mycmp(self.obj, other.obj) > 0 + def __eq__(self, other): + return mycmp(self.obj, other.obj) == 0 + def __le__(self, other): + return mycmp(self.obj, other.obj) <= 0 + def __ge__(self, other): + return mycmp(self.obj, other.obj) >= 0 + def __ne__(self, other): + return mycmp(self.obj, other.obj) != 0 + return K +else: + from functools import cmp_to_key + def cmp(first, second): return (first > second) - (second > first)
Signature · 1 line
2.4.3
Stephen Smalley
AppliedQuoted 4 lines · Robert Kuska
In python3 it is needed to pass compare function as a key argument instead of directly passing compare function to sort function
Signed-off-by Robert Kuska <[email protected]>
Thanks, applied.
Quoted 62 lines
--- sepolgen/src/sepolgen/output.py | 4 ++-- sepolgen/src/sepolgen/util.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-)sepolgen/src/sepolgen/output.py +2
-2@@ -131,7 +131,7 @@ def sort_filter(module): rules = [] rules.extend(node.avrules()) rules.extend(node.interface_calls()) - rules.sort(rule_cmp) + rules.sort(key=util.cmp_to_key(rule_cmp)) cur = None sep_rules = []@@ -155,7 +155,7 @@ def sort_filter(module): ras = [] ras.extend(node.role_types()) - ras.sort(role_type_cmp) + ras.sort(key=util.cmp_to_key(role_type_cmp)) if len(ras): comment = refpolicy.Comment() comment.lines.append("============= ROLES ==============")sepolgen/src/sepolgen/util.py +23
-0@@ -145,6 +145,29 @@ class Comparison(): def __ne__(self, other): return self._compare(other, lambda a, b: a != b) +if sys.version_info < (2,7): + # cmp_to_key function is missing in python2.6 + def cmp_to_key(mycmp): + 'Convert a cmp= function into a key= function' + class K: + def __init__(self, obj, *args): + self.obj = obj + def __lt__(self, other): + return mycmp(self.obj, other.obj) < 0 + def __gt__(self, other): + return mycmp(self.obj, other.obj) > 0 + def __eq__(self, other): + return mycmp(self.obj, other.obj) == 0 + def __le__(self, other): + return mycmp(self.obj, other.obj) <= 0 + def __ge__(self, other): + return mycmp(self.obj, other.obj) >= 0 + def __ne__(self, other): + return mycmp(self.obj, other.obj) != 0 + return K +else: + from functools import cmp_to_key + def cmp(first, second): return (first > second) - (second > first)