From d115b5ae707d00dd09bebc7f5e63d4e25cdb1a04 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 14 Sep 2012 09:41:01 +1200 Subject: [PATCH] Expand Flow.match to accept either a string or a compiled filter expression. --- libmproxy/flow.py | 7 +++++++ test/test_flow.py | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libmproxy/flow.py b/libmproxy/flow.py index 44bb10186..8e4c2117f 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -1092,7 +1092,14 @@ class Flow: """ Match this flow against a compiled filter expression. Returns True if matched, False if not. + + If f is a string, it will be compiled as a filter expression. If + the expression is invalid, ValueError is raised. """ + if isinstance(f, basestring): + f = filt.parse(f) + if not f: + raise ValueError("Invalid filter expression.") if f: return f(self) return True diff --git a/test/test_flow.py b/test/test_flow.py index 53d92f259..277d2407a 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -171,12 +171,15 @@ class TestFlow: f = tutils.tflow() f.response = tutils.tresp() f.request = f.response.request - assert not f.match(filt.parse("~b test")) + assert not f.match("~b test") assert f.match(None) - assert not f.match(filt.parse("~b test")) + assert not f.match("~b test") f = tutils.tflow_err() - assert f.match(filt.parse("~e")) + assert f.match("~e") + + tutils.raises(ValueError, f.match, "~") + def test_backup(self): f = tutils.tflow()