Opened 2 years ago

Closed 17 months ago

#5711 defect closed worksforme (worksforme)

In howto/twisted-templates.html listings/element_2.py example doesn't work with FilePath

Reported by: lewq Owned by:
Priority: normal Milestone:
Component: web Keywords: web template documentation
Cc: jknight Branch:
Author: Launchpad Bug:

Description (last modified by thijs)

from twisted.web.template import Element, renderer, XMLFile, tags
from twisted.python.filepath import FilePath

class ExampleElement(Element):
    loader = XMLFile(FilePath('template-1.xml'))

Causes:

Jumping into debugger for post-mortem of exception 'Exception while flattening:
  <ExampleElement object at 0x1b03090>
  File "/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/_flatten.py", line 218, in _flattenTree
    element = stack[-1].next()
  File "/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/_flatten.py", line 189, in _flattenElement
    result = root.render(request)
  File "/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/_element.py", line 184, in render
    return loader.load()
  File "/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/template.py", line 397, in load
    self._loadedTemplate = self._loadDoc()
  File "/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/template.py", line 388, in <lambda>
    self._loadDoc = lambda: _flatsaxParse(fobj)
  File "/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/template.py", line 335, in _flatsaxParse
    parser.parse(fl)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 102, in parse
    source = saxutils.prepare_input_source(source)
  File "/usr/lib/python2.7/xml/sax/saxutils.py", line 295, in prepare_input_source
    if source.getByteStream() is None:
AttributeError: FilePath instance has no attribute 'getByteStream'

However replacing FilePath with open so that XMLFile is just operating on a file-like object fixes it. Is this the correct fix for the docs?

Change History (6)

comment:1 Changed 2 years ago by DefaultCC Plugin

  • Cc jknight added

comment:2 Changed 2 years ago by thijs

  • Description modified (diff)
  • Keywords documentation added

Fixing ticket markup.

comment:3 Changed 17 months ago by rwall

  • Resolution set to invalid
  • Status changed from new to closed

Thanks for the bug report.

I think the problem is that you are using an older version of Twisted.

In r33940 XMLFile was updated to accept a FilePath. Passing it a file or string is now deprecated.

You can try the 12.0.0 documentation or you could update to the current Twisted release.

We should probably provide clearer links to the version specific documentation - maybe in footer of the documents themselves.

comment:4 Changed 17 months ago by exarkun

To be clear, this is a 12.1.0 feature.

comment:5 Changed 17 months ago by exarkun

  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:6 Changed 17 months ago by exarkun

  • Resolution set to worksforme
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.