[Twisted-web] Trivial diff -- automatically adapt renderers

Cory Dodt corydodt at twistedmatrix.com
Thu Oct 7 20:01:41 MDT 2004

When using a hand-made (doesn't inherit from rend.Page) Page that wraps real
pages, I discovered that registering an adapter for IRendererFactory doesn't
automatically allow .renderer to get called.  Attached is a diff that
automatically adapts rendererFactories to IRendererFactory in flatstan.py.

The following is a minimal app that exhibits the buggy behavior:
from nevow import inevow, rend, loaders, appserver, tags as T
from twisted.application import service, internet
from twisted.python import components

class MyWrapper:
     __implements__ = inevow.IResource,
     def __init__(self, wrapped):
       self.wrapped = inevow.IResource(wrapped)
     def renderHTTP(self, ctx):
       return self.wrapped.renderHTTP(ctx)
     def locateChild(self, ctx, segments):
       wr, segments = self.wrapped.locateChild(ctx, segments)
       return MyWrapper(wr), ()
components.registerAdapter(lambda self: self.wrapped, MyWrapper,

class DumbResource(rend.Page):
     def render_x(self, ctx, data):
         return T.body['hi']
     docFactory = loaders.xmlstr("<html
xmlns:n='http://nevow.com/ns/nevow/0.1'><a n:render='x' /></html>")

application = service.Application("brokenIRendererFactory")
svc = internet.TCPServer(8080, appserver.NevowSite(MyWrapper(DumbResource())))

# ===> MyWrapper instance has no attribute 'renderer'

(Donovan, you might remember me bringing this up on #t.web a few months ago.)

And, as an anticlimax, the patch to fix the above:

Index: nevow/flat/flatstan.py
--- nevow/flat/flatstan.py      (revision 695)
+++ nevow/flat/flatstan.py      (working copy)
@@ -204,7 +204,7 @@
      if context.precompile:
          return original

-    rendererFactory = context.locate(IRendererFactory)
+    rendererFactory = IRendererFactory(context.locate(IRendererFactory))
      renderer = rendererFactory.renderer(context, original.name)
      return serialize(renderer, context)

More information about the Twisted-web mailing list