Issue #1237 resolved

Caching and JSON output problem

Anonymous created an issue

While using caching in the config:

tools.caching.on: True

And outputting JSON from a function:

@cherrypy.tools.json_out()

When the page is actually cached, the following error appears:

http://pastebin.com/ZEwpSngj

RAW:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
    response.body = self.handler()
  File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/jsontools.py", line 61, in json_handler
    value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
TypeError: 'NoneType' object is not callable

FIX: in file: jsontools.py, exchange function json_handler with:

def json_handler(*args, **kwargs): 
    request = cherrypy.serving.request
    if request.cached:
        return cherrypy.serving.response.body
    else:
        value = request._json_inner_handler(*args, **kwargs)
    return json_encode(value)

Comments (7)

  1. Robert Brewer

    This isn't the right way to fix this. We can be less tightly-bound to the caching tool and coincidentally faster by checking for request.handler = None before binding the json wrapper in json_out:

        request = cherrypy.serving.request
        # request.handler may be set to None by e.g. the caching tool
        # to signal to all components that a response body has already
        # been attached, in which case we don't need to wrap anything.
        if request.handler is None:
            return
    
  2. marforic

    I like the solution proposed by Robert. Though I don't know of other tools that set the handler to None. This still works. Hope it gets pushed into CherryPy.

  3. Log in to comment