+ cherrypy.request.handler = MakoHandler(
+ t, cherrypy.request.handler, content_type)
+
+cherrypy.tools.mako = cherrypy.Tool('on_start_resource', MakoLoader())
+
+
+def revertStandardError():
+ """Move stderr to stdout, and return the contents of the old stderr."""
+ errio = sys.stderr
+ if not isinstance(errio, StringIO):
+ return ''
+ sys.stderr = sys.stdout
+ errio.seek(0)
+ return errio.read()
+
+
+def catchStderr():
+ old_handler = cherrypy.request.handler
+ def wrapper(*args, **kwargs):
+ sys.stderr = StringIO()
+ ret = old_handler(*args, **kwargs)
+ e = revertStandardError()
+ if e:
+ if isinstance(ret, dict):
+ ret["error_text"] = e
+ return ret
+ if old_handler:
+ cherrypy.request.handler = wrapper
+
+cherrypy.tools.catch_stderr = cherrypy.Tool('before_handler', catchStderr)