From 4afe236b0c036f98d97a5d4bf30b630957599ef5 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Mon, 21 Jun 2010 17:36:32 +0300 Subject: [PATCH 1/2] safe_eval: fix wrt whitespace on expressions. Regular eval() would not care about leading whitespace. But, compile() would break if lines start with space. Strip that. Also, allow some exceptions to pass through, so that we can examine the syntax errors etc. bzr revid: p_christ@hol.gr-20100621143632-0hvrck77lterxjtl --- bin/tools/safe_eval.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/tools/safe_eval.py b/bin/tools/safe_eval.py index ef26fbc4cbe..d933c53d574 100644 --- a/bin/tools/safe_eval.py +++ b/bin/tools/safe_eval.py @@ -86,8 +86,14 @@ def test_expr(expr, allowed_codes, mode="eval"): return the compiled code object. Otherwise raise a ValueError. """ try: + if mode == 'eval': + expr = expr.strip() code_obj = compile(expr, "", mode) - except: + except SyntaxError: + raise + except TypeError: + raise + except Exception, e: raise ValueError("%s is not a valid expression" % expr) for code in _get_opcodes(code_obj): if code not in allowed_codes: From 1ee1886dcdd0a9492190cbe55495c66074d32c7d Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Mon, 21 Jun 2010 17:37:01 +0300 Subject: [PATCH 2/2] rml2pdf: Log stray exceptions, use special when expected. If the eval() raises an exception, we should log that. Unless it's the one we expect to see (thus changed to a special type), where we can keep silently ignoring it. Note: the GeneratorExit requires python 2.5 . bzr revid: p_christ@hol.gr-20100621143701-oenf5fp57qs1bxz4 --- bin/report/render/rml2pdf/utils.py | 12 ++++++++++++ bin/report/report_sxw.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bin/report/render/rml2pdf/utils.py b/bin/report/render/rml2pdf/utils.py index be8695e7eb3..9c781d302be 100644 --- a/bin/report/render/rml2pdf/utils.py +++ b/bin/report/render/rml2pdf/utils.py @@ -63,7 +63,10 @@ def _child_get(node, self=None, tagname=None): if n.get('rml_except', False): try: eval(n.get('rml_except'), {}, self.localcontext) + except GeneratorExit: + continue except: + logging.getLogger('report').exception() continue if n.get('rml_tag'): try: @@ -72,7 +75,10 @@ def _child_get(node, self=None, tagname=None): n2.tag = tag n2.attrib.update(attr) yield n2 + except GeneratorExit: + yield n except: + logging.getLogger('report').exception() yield n else: yield n @@ -81,7 +87,10 @@ def _child_get(node, self=None, tagname=None): if self and self.localcontext and n.get('rml_except'): try: eval(n.get('rml_except'), {}, self.localcontext) + except GeneratorExit: + continue except: + logging.getLogger('report').exception() continue if self and self.localcontext and n.get('rml_tag'): try: @@ -91,7 +100,10 @@ def _child_get(node, self=None, tagname=None): n2.attrib.update(attr or {}) yield n2 tagname = '' + except GeneratorExit: + pass except: + logging.getLogger('report').exception() pass if (tagname is None) or (n.tag==tagname): yield n diff --git a/bin/report/report_sxw.py b/bin/report/report_sxw.py index 9d2fb97362c..32857044cdd 100644 --- a/bin/report/report_sxw.py +++ b/bin/report/report_sxw.py @@ -192,7 +192,7 @@ class rml_parse(object): return text.strip() def removeParentNode(self, tag=None): - raise Exception('Skip') + raise GeneratorExit('Skip') def set_html_image(self,id,model=None,field=None,context=None): if not id :