[apparmor] [patch] Adjust type(x) == str checks in the rule classes for py2

Christian Boltz apparmor at cboltz.de
Thu Dec 17 22:41:07 UTC 2015


Hello,

Am Donnerstag, 17. Dezember 2015 schrieb Tyler Hicks:
> On 2015-11-29 22:19:02, Christian Boltz wrote:
> > python 3 uses only the 'str' type, while python 2 also uses
> > 'unicode'. This patch adds a type_is_str() function to common.py -
> > depending on the python version, it checks for both. This helper
> > function is used to keep the complexity outside of the rule
> > classes.
> >
> > The rule classes get adjusted to use type_is_str() instead of
> > checking for type(x) == str, which means they support both python
> > versions.>
> >
> > Finally, add test-common.py with some tests for type_is_str().
> >
> > References: https://bugs.launchpad.net/apparmor/+bug/1513880
> >
> > I propose this patch for trunk and 2.10 (except the rule/signal.py
> > change because 2.10 doesn't contain SignalRule yet)
> 
> It looks like you may have missed two other 'type(.*== str' patterns:
> 
>   utils/apparmor/aare.py:70:        elif type(expression) == str:
>   utils/apparmor/rule/__init__.py:351:    elif type(lst) == str:
> 
> Please determine if they need the type_is_str() treatment, adjust the
> patch as needed, and then commit to both branches with:
> 
>   Acked-by: Tyler Hicks <tyhicks at canonical.com>

Actually there were two places in aare.py with a type check for str, and 
it's a good idea to allow unicode there.

rule/__init__.py should not get unicode passed in - but OTOH, being 
consistent is worth more than writing "shouldn't happen" in a bugreport, 
so I switched to type_is_str() there also.

For the records - the additional changes I included in the commit are
(trunk only - 2.10 doesn't have that code):


=== modified file 'utils/apparmor/aare.py'
--- utils/apparmor/aare.py      2015-12-09 22:19:57 +0000
+++ utils/apparmor/aare.py      2015-12-17 22:08:34 +0000
@@ -14,7 +14,7 @@
 
 import re
 
-from apparmor.common import convert_regexp, AppArmorBug, AppArmorException
+from apparmor.common import convert_regexp, type_is_str, AppArmorBug, AppArmorException
 
 class AARE(object):
     '''AARE (AppArmor Regular Expression) wrapper class'''
@@ -54,7 +54,7 @@
                 expression = expression.orig_regex
             else:
                 return self.is_equal(expression)  # better safe than sorry
-        elif type(expression) != str:
+        elif not type_is_str(expression):
             raise AppArmorBug('AARE.match() called with unknown object: %s' % str(expression))
 
         if self._regex_compiled is None:
@@ -67,7 +67,7 @@
 
         if type(expression) == AARE:
             return self.regex == expression.regex
-        elif type(expression) == str:
+        elif type_is_str(expression):
             return self.regex == expression
         else:
             raise AppArmorBug('AARE.is_equal() called with unknown object: %s' % str(expression))

=== modified file 'utils/apparmor/rule/__init__.py'
--- utils/apparmor/rule/__init__.py     2015-12-04 11:02:33 +0000
+++ utils/apparmor/rule/__init__.py     2015-12-17 22:09:47 +0000
@@ -13,7 +13,7 @@
 #
 # ----------------------------------------------------------------------
 
-from apparmor.common import AppArmorBug
+from apparmor.common import AppArmorBug, type_is_str
 
 # setup module translations
 from apparmor.translations import init_translation
@@ -348,7 +348,7 @@
 
     if lst == all_obj:
         return None, True, None
-    elif type(lst) == str:
+    elif type_is_str(lst):
         result_list = {lst}
     elif (type(lst) == list or type(lst) == tuple) and len(lst) > 0:
         result_list = set(lst)




Regards,

Christian Boltz
-- 
Der nächste DAU kommt bestimmt. Sie werden in den Kellern
von AOL gezüchtet.             [Dieter Bruegmann in dag°]




More information about the AppArmor mailing list