A valid PageTemplate fails rendering with AttributeError in zope.tal.talgenerator

Bug #1004588 reported by Evgeny Prigorodov
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Zope 2
Invalid
Low
Unassigned

Bug Description

This template fails on Zope 2.13.13 (which uses zope.tal-3.5.2 at this moment):

<html>
  <head>
    <title tal:content="template/title">The title</title>
  </head>
  <body>
    <div tal:attributes="attr0 string: value0" tal:replace="structure string: WTF?" />
  </body>
</html>

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/lib/python2.7/site-packages/Zope2-2.13.13-py2.7.egg/Shared/DC/Scripts/Bindings.py", line 322, in __call__
    return self._bindAndExec(args, kw, None)
  File "/data/lib/python2.7/site-packages/Zope2-2.13.13-py2.7.egg/Shared/DC/Scripts/Bindings.py", line 359, in _bindAndExec
    return self._exec(bound_data, args, kw)
  File "/data/lib/python2.7/site-packages/Zope2-2.13.13-py2.7.egg/Products/PageTemplates/ZopePageTemplate.py", line 334, in _exec
    result = self.pt_render(extra_context=bound_names)
  File "/data/lib/python2.7/site-packages/Zope2-2.13.13-py2.7.egg/Products/PageTemplates/ZopePageTemplate.py", line 431, in pt_render
    result = PageTemplate.pt_render(self, source, extra_context)
  File "/data/lib/python2.7/site-packages/Zope2-2.13.13-py2.7.egg/Products/PageTemplates/PageTemplate.py", line 79, in pt_render
    showtal=showtal)
  File "/data/lib/python2.7/site-packages/zope.pagetemplate-3.5.2-py2.7.egg/zope/pagetemplate/pagetemplate.py", line 113, in pt_render
    strictinsert=0, sourceAnnotations=sourceAnnotations)()
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 271, in __call__
    self.interpret(self.program)
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 343, in interpret
    handlers[opcode](self, args)
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 531, in do_optTag_tal
    self.no_tag(stuff[-2], stuff[-1])
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 513, in no_tag
    self.interpret(program)
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 343, in interpret
    handlers[opcode](self, args)
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 757, in do_insertStructure_tal
    self.insertHTMLStructure(text, repldict)
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 781, in insertHTMLStructure
    gen = AltTALGenerator(repldict, self.engine, 0)
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talinterpreter.py", line 65, in __init__
    TALGenerator.__init__(self, expressionCompiler, xml)
  File "/data/lib/python2.7/site-packages/zope.tal-3.5.2-py2.7.egg/zope/tal/talgenerator.py", line 41, in __init__
    self.CompilerError = expressionCompiler.getCompilerError()
AttributeError: 'ZopeContext' object has no attribute 'getCompilerError'

TALGenerator.__init__() expects to have in its expressionCompiler parameter an zope.tal.interfaces.ITALExpressionCompiler instance that has getCompilerError() method.

Instead of that zope.pagetemplate.pagetemplate.PageTemplate.pt_render() passes down along the chain an instance of Products.PageTemplates.Expressions.ZopeContext class, which implements ITALExpressionEngine. From zope.pagetemplate-3.5.2-py2.7.egg/zope/pagetemplate/pagetemplate.py:110

        context = self.pt_getEngineContext(namespace)
        TALInterpreter(self._v_program, self._v_macros,
                       context, output, tal=not source, showtal=showtal,
                       strictinsert=0, sourceAnnotations=sourceAnnotations)()

pdb.pm():

(Pdb) from zope.tal.interfaces import ITALExpressionCompiler, ITALExpressionEngine
(Pdb) expressionCompiler
<Products.PageTemplates.Expressions.ZopeContext object at 0xaea0b62c>
(Pdb) ITALExpressionCompiler.implementedBy(expressionCompiler.__class__)
False
(Pdb) ITALExpressionEngine.implementedBy(expressionCompiler.__class__)
True
(Pdb) expressionCompiler._engine
<Products.PageTemplates.Expressions.ZopeEngine object at 0xb702e54c>
(Pdb) ITALExpressionCompiler.implementedBy(expressionCompiler._engine.__class__)
True
(Pdb) expressionCompiler._engine.getCompilerError()
<class 'zope.tales.tales.CompilerError'>

Without this /very useful/ combination of "tal:attribuites", "tal:replace" and "structure" interpretation goes into another branch of zope.tal.interpreter.do_insertStructure_tal(), that doesn't use TALGenerator and hence doesn't fail. I thank our bright developers for revealing this.

Regards,
Evgeny

Revision history for this message
Hanno Schlichting (hannosch) wrote :

As a workaround try to remove the tal:attributes. Since tal:replace will replace the entire tag, the attributes call is a bit useless. But http://pagetemplates.org/docs/latest/reference.html#tal-attributes says the attributes should be ignored and is apparently valid syntax.

Changed in zope2:
importance: Undecided → Low
status: New → Confirmed
Revision history for this message
BenJ (ben-jolitz) wrote :

This is an easy fix, but cannot be mainlined due to some incredible disparities between zope.tal, zope.tales and zope.pagetemplates for Zope 2.x installs.

In plain english, your ZopeContext has a "_engine" property that CAN be fed into AltTALGenerator.

So replace "self.engine" with "self.engine._engine" for insertHTMLStructure, insertXMLStructure.

Here is a patch that works on my Zope 2.11 installation:
https://gist.github.com/benjolitz/6070af860e21a8a50771

It is meant to be applied within the /opt/Zope-2.11/ directory, but judicious use of patch -p# can help you.

I've investigated getting this into mainline, but I'm not interested in squaring the round circle of Zope packages.

Revision history for this message
Colin Watson (cjwatson) wrote :

The zope2 project on Launchpad has been archived at the request of the Zope developers (see https://answers.launchpad.net/launchpad/+question/683589 and https://answers.launchpad.net/launchpad/+question/685285). If this bug is still relevant, please refile it at https://github.com/zopefoundation/zope2.

Changed in zope2:
status: Confirmed → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.