Example #1
0
 def __init__(self, app):
     self.app = app
     threadedprint.install(leave_stdout=True)
     threadedprint.install_stdin()
     self.counter = count()
     self.static_app = StaticURLParser(os.path.join(here, 'pdbcapture/static'))
     self.media_app = StaticURLParser(os.path.join(here, 'eval-media'))
     self.states = {}
 def __init__(self, app):
     self.app = app
     threadedprint.install(leave_stdout=True)
     threadedprint.install_stdin()
     self.counter = count()
     self.static_app = StaticURLParser(os.path.join(here, 'pdbcapture/static'))
     self.media_app = StaticURLParser(os.path.join(here, 'eval-media'))
     self.states = {}
Example #3
0
 def __call__(self, environ, start_response):
     global _threadedprint_installed
     if environ.get('paste.testing'):
         # In a testing environment this interception isn't
         # useful:
         return self.app(environ, start_response)
     if (not _threadedprint_installed
         or self._threaded_print_stdout is not sys.stdout):
         # @@: Not strictly threadsafe
         _threadedprint_installed = True
         threadedprint.install(leave_stdout=not self.replace_stdout)
         self._threaded_print_stdout = sys.stdout
     removed = []
     def remove_printdebug():
         removed.append(None)
     environ['paste.remove_printdebug'] = remove_printdebug
     logged = StringIO()
     listeners = [logged]
     environ['paste.printdebug_listeners'] = listeners
     if self.print_wsgi_errors:
         listeners.append(environ['wsgi.errors'])
     replacement_stdout = TeeFile(listeners)
     threadedprint.register(replacement_stdout)
     try:
         status, headers, body = wsgilib.intercept_output(
             environ, self.app)
         if status is None:
             # Some error occurred
             status = '500 Server Error'
             headers = [('Content-type', 'text/html')]
             start_response(status, headers)
             if not body:
                 body = 'An error occurred'
         content_type = response.header_value(headers, 'content-type')
         if (removed or
             (not self.force_content_type and
              (not content_type
               or not content_type.startswith('text/html')))):
             if replacement_stdout == logged:
                 # Then the prints will be lost, unless...
                 environ['wsgi.errors'].write(logged.getvalue())
             start_response(status, headers)
             return [body]
         response.remove_header(headers, 'content-length')
         body = self.add_log(body, logged.getvalue())
         start_response(status, headers)
         return [body]
     finally:
         threadedprint.deregister()