def call_app(self, req, state): event = state['event'] stream_handler = StreamHandler(stdin=state['stdin'], stdin_event=state['stdin_event'], stdout=state['stdout'], signal_event=state['event']) threadedprint.register(stream_handler) threadedprint.register_stdin(stream_handler) try: resp = req.get_response(self.app) state['response'] = resp except: state['exc_info'] = sys.exc_info() event.set()
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()