Ticket #5711 defect closed worksforme

Opened 11 months ago

Last modified 4 weeks ago

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) (diff)

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

1

Changed 11 months ago by DefaultCC Plugin

  • cc jknight added

2

Changed 11 months ago by thijs

  • keywords documentation added
  • description modified (diff)

Fixing ticket markup.

3

Changed 4 weeks ago by rwall

  • status changed from new to closed
  • resolution set to invalid

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.

4

Changed 4 weeks ago by exarkun

To be clear, this is a 12.1.0 feature.

5

Changed 4 weeks ago by exarkun

  • status changed from closed to reopened
  • resolution invalid deleted

6

Changed 4 weeks ago by exarkun

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