From b3ccc1cac5499edeaa376bea1fa04768b45232f7 Mon Sep 17 00:00:00 2001 From: Arnaud Fontaine <arnaud.fontaine@nexedi.com> Date: Tue, 8 Mar 2011 03:38:48 +0000 Subject: [PATCH] Get properly the TALES Expression engine and CompilerError exception for isValidTALESExpression. This fixes testDynamicClassGeneration errors and failure for Zope 2.8. git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@44027 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/ERP5Type/Utils.py | 49 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/product/ERP5Type/Utils.py b/product/ERP5Type/Utils.py index 9dfdb23207..997491e167 100644 --- a/product/ERP5Type/Utils.py +++ b/product/ERP5Type/Utils.py @@ -1207,9 +1207,15 @@ def initializeProduct( context, icon = icon) ##################################################### -# Constructor initialization +# TALES Expression ##################################################### +# This gets the Engine and CompilerError classes for TALES Expression +# wherever it is defined (which is different depending on the Zope +# version) +ExpressionEngine = getEngine() +CompilerError = ExpressionEngine.getCompilerError() + def createExpressionContext(object, portal=None): """ Return a context used for evaluating a TALES expression. @@ -1276,14 +1282,10 @@ def createExpressionContext(object, portal=None): # the proper name these days 'context': object, } - ec = getEngine().getContext(data) + ec = ExpressionEngine.getContext(data) tv[cache_key] = ec return ec -# This gets the CompilerError class wherever it is defined (which is -# different depending on the Zope version) -CompilerError = getEngine().getCompilerError() - def evaluateExpressionFromString(expression_context, expression_string): """ Evaluate a TALES Expression from the given string with the given @@ -1310,6 +1312,22 @@ def evaluateExpressionFromString(expression_context, expression_string): raise ValueError("Error in TALES expression: '%s': %s" % (expression_string, str(e))) +def isValidTALESExpression(value): + """return if given value is valid TALES Expression. + This validator only validates Syntax of TALES Expression, + it does not tell that Expression is callable on given context + + - value: string we try to compile + + return tuple: (boolean result, error_message or None) + """ + try: + ExpressionEngine.compile(value) + except CompilerError, message: + return False, message + else: + return True, None + ##################################################### # More Useful methods which require Base ##################################################### @@ -1724,22 +1742,3 @@ def reencodeUrlEscapes(url): url += [_reencodeUrlEscapes_map[c] for c in part] except StopIteration: return ''.join(url) - -from zope.tales.engine import Engine -from zope.tales.tales import CompilerError - -def isValidTALESExpression(value): - """return if given value is valid TALES Expression. - This validator only validates Syntax of TALES Expression, - it does not tell that Expression is callable on given context - - - value: string we try to compile - - return tuple: (boolean result, error_message or None) - """ - try: - Engine.compile(value) - except CompilerError, message: - return False, message - else: - return True, None -- 2.30.9