def evaluateMacro(self, expr): """evaluateMacro gets security-proxied macro programs when this is run with the zopeTraverser, and in other untrusted situations. This will cause evaluation to fail in zope.tal.talinterpreter, which knows nothing of security proxies. Therefore, this method removes any proxy from the evaluated expression. >>> from zope.pagetemplate.engine import ZopeContext >>> from zope.tales.tales import ExpressionEngine >>> from zope.security.proxy import ProxyFactory >>> output = [('version', 'xxx'), ('mode', 'html'), ('other', 'things')] >>> def expression(context): ... return ProxyFactory(output) ... >>> zc = ZopeContext(ExpressionEngine, {}) >>> out = zc.evaluateMacro(expression) >>> type(out) is list True The method does some trivial checking to make sure we are getting back a macro like we expect: it must be a sequence of sequences, in which the first sequence must start with 'version', and the second must start with 'mode'. >>> del output[0] >>> zc.evaluateMacro(expression) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: ('unexpected result from macro evaluation.', ...) >>> del output[:] >>> zc.evaluateMacro(expression) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: ('unexpected result from macro evaluation.', ...) >>> output = None >>> zc.evaluateMacro(expression) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: ('unexpected result from macro evaluation.', ...) """ macro = removeSecurityProxy(Context.evaluateMacro(self, expr)) # we'll do some basic checks that it is the sort of thing we expect problem = False try: problem = macro[0][0] != 'version' or macro[1][0] != 'mode' except (TypeError, IndexError): problem = True if problem: raise ValueError('unexpected result from macro evaluation.', macro) return macro
def evaluateMacro(self, expr): """evaluateMacro gets security-proxied macro programs when this is run with the zopeTraverser, and in other untrusted situations. This will cause evaluation to fail in zope.tal.talinterpreter, which knows nothing of security proxies. Therefore, this method removes any proxy from the evaluated expression. >>> output = [('version', 'xxx'), ('mode', 'html'), ('other', 'things')] >>> def expression(context): ... return ProxyFactory(output) ... >>> zc = ZopeContext(ExpressionEngine, {}) >>> out = zc.evaluateMacro(expression) >>> type(out) <type 'list'> The method does some trivial checking to make sure we are getting back a macro like we expect: it must be a sequence of sequences, in which the first sequence must start with 'version', and the second must start with 'mode'. >>> del output[0] >>> zc.evaluateMacro(expression) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: ('unexpected result from macro evaluation.', ...) >>> del output[:] >>> zc.evaluateMacro(expression) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: ('unexpected result from macro evaluation.', ...) >>> output = None >>> zc.evaluateMacro(expression) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: ('unexpected result from macro evaluation.', ...) """ macro = removeSecurityProxy(Context.evaluateMacro(self, expr)) # we'll do some basic checks that it is the sort of thing we expect problem = False try: problem = macro[0][0] != 'version' or macro[1][0] != 'mode' except (TypeError, IndexError): problem = True if problem: raise ValueError('unexpected result from macro evaluation.', macro) return macro
def __init__(self, *args, **kwargs): self.target_language = None if kwargs.has_key('target_language'): self.target_language = kwargs['target_language'] kwargs.pop('target_language') Context.__init__(self, *args, **kwargs)
def setUp(self): self.context = Context(Engine, {}) self.engine = Engine
def setContext(self, name, value): # Hook to allow subclasses to do things like adding security proxies Context.setContext(self, name, ProxyFactory(value))
def evaluateMacro(self, expr): macro = Context.evaluateMacro(self, expr) return macro
def setUp(self): self.c_context = c_context = Scope() c_context["__zt_context__"] = Context(None, {}) self.z_context = _C2ZContextWrapper(c_context)
def setUp(self): self.c_context = c_context = Scope() z_context = Context(None, {}) z_context.setContext("default", DEFAULT_MARKER) c_context["__zt_context__"] = wrap(z_context) self.z_context = _C2ZContextWrapper(c_context, None)