From dc7e4a79d9a1884b4c5705ef3173613958204b50 Mon Sep 17 00:00:00 2001 From: Christopher Larson Date: Tue, 15 May 2012 20:26:25 -0500 Subject: oe.types: give the regex type more sane semantics Currently, if a variable is unset or has an empty value, the regex type will return a match object which always matches. Not all variable types will necessarily have the same behavior for handling defaults. I believe that returning a match object which matches nothing when a variable is unset is superior to returning one which matches anything, and the user can always explicitly request anything via '.*', if that's what they want. This constructs a null pattern object which will never match, and uses it when encountering an unset or empty variable (currently, these two things are one and the same, as maketype is handling the default. we may well want to shift that logic into the individual types, giving them more control over default behavior, but currently the behavior is at least relatively consistent -- no difference between unset and empty variables). Signed-off-by: Christopher Larson --- meta/lib/oe/types.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/meta/lib/oe/types.py b/meta/lib/oe/types.py index ea31cf421..ea53df9bf 100644 --- a/meta/lib/oe/types.py +++ b/meta/lib/oe/types.py @@ -40,6 +40,31 @@ def choice(value, choices): (value, choices)) return value +class NoMatch(object): + """Stub python regex pattern object which never matches anything""" + def findall(self, string, flags=0): + return None + + def finditer(self, string, flags=0): + return None + + def match(self, flags=0): + return None + + def search(self, string, flags=0): + return None + + def split(self, string, maxsplit=0): + return None + + def sub(pattern, repl, string, count=0): + return None + + def subn(pattern, repl, string, count=0): + return None + +NoMatch = NoMatch() + def regex(value, regexflags=None): """OpenEmbedded 'regex' type @@ -59,6 +84,12 @@ def regex(value, regexflags=None): except AttributeError: raise ValueError("Invalid regex flag '%s'" % flag) + if not value: + # Let's ensure that the default behavior for an undefined or empty + # variable is to match nothing. If the user explicitly wants to match + # anything, they can match '.*' instead. + return NoMatch + try: return re.compile(value, flagval) except re.error, exc: -- cgit v1.2.3