* [PATCH] oe.types: give the regex type more sane semantics
@ 2012-05-16 1:26 Christopher Larson
2012-05-18 5:40 ` Saul Wold
0 siblings, 1 reply; 2+ messages in thread
From: Christopher Larson @ 2012-05-16 1:26 UTC (permalink / raw)
To: openembedded-core; +Cc: Christopher Larson
From: Christopher Larson <chris_larson@mentor.com>
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 <chris_larson@mentor.com>
---
meta/lib/oe/types.py | 31 +++++++++++++++++++++++++++++++
1 files changed, 31 insertions(+), 0 deletions(-)
diff --git a/meta/lib/oe/types.py b/meta/lib/oe/types.py
index ea31cf4..ea53df9 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:
--
1.7.7
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] oe.types: give the regex type more sane semantics
2012-05-16 1:26 [PATCH] oe.types: give the regex type more sane semantics Christopher Larson
@ 2012-05-18 5:40 ` Saul Wold
0 siblings, 0 replies; 2+ messages in thread
From: Saul Wold @ 2012-05-18 5:40 UTC (permalink / raw)
To: Patches and discussions about the oe-core layer; +Cc: Christopher Larson
On 05/16/2012 04:26 AM, Christopher Larson wrote:
> From: Christopher Larson<chris_larson@mentor.com>
>
> 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<chris_larson@mentor.com>
> ---
> meta/lib/oe/types.py | 31 +++++++++++++++++++++++++++++++
> 1 files changed, 31 insertions(+), 0 deletions(-)
>
> diff --git a/meta/lib/oe/types.py b/meta/lib/oe/types.py
> index ea31cf4..ea53df9 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:
Merged this series of 3 patches into OE-Core
Thanks
Sau!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-05-18 5:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-16 1:26 [PATCH] oe.types: give the regex type more sane semantics Christopher Larson
2012-05-18 5:40 ` Saul Wold
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox