parameters: (self, out, request, response, peek=None)
return_statement: return self._cast(HTTPError(500, 'Unsupported response type: %s'\
                                           % type(first)), request, response)
docstring: Try to convert the parameter into something WSGI compatible and set
          correct HTTP headers when possible.
          Support: False, str, unicode, dict, HTTPResponse, HTTPError, file-like,
          iterable of strings and iterable of unicodes
docstring_summary: Try to convert the parameter into something WSGI compatible and set
          correct HTTP headers when possible.
          Support: False, str, unicode, dict, HTTPResponse, HTTPError, file-like,
          iterable of strings and iterable of unicodes
docstring_tokens: Try to convert the parameter into something WSGI compatible and set correct HTTP headers when possible . Support : False str unicode dict HTTPResponse HTTPError file - like iterable of strings and iterable of unicodes
function: def _cast(self, out, request, response, peek=None):
          """ Try to convert the parameter into something WSGI compatible and set
          correct HTTP headers when possible.
          Support: False, str, unicode, dict, HTTPResponse, HTTPError, file-like,
          iterable of strings and iterable of unicodes
          """
          # Filtered types (recursive, because they may return anything)
          for testtype, filterfunc in self.castfilter:
              if isinstance(out, testtype):
                  return self._cast(filterfunc(out), request, response)
  
          # Empty output is done here
          if not out:
              response.headers['Content-Length'] = 0
              return []
          # Join lists of byte or unicode strings. Mixed lists are NOT supported
          if isinstance(out, (tuple, list))\
          and isinstance(out[0], (bytes, unicode)):
              out = out[0][0:0].join(out) # b'abc'[0:0] -> b''
          # Encode unicode strings
          if isinstance(out, unicode):
              out = out.encode(response.charset)
          # Byte Strings are just returned
          if isinstance(out, bytes):
              response.headers['Content-Length'] = str(len(out))
              return [out]
          # HTTPError or HTTPException (recursive, because they may wrap anything)
          if isinstance(out, HTTPError):
              out.apply(response)
              return self._cast(self.error_handler.get(out.status, repr)(out), request, response)
          if isinstance(out, HTTPResponse):
              out.apply(response)
              return self._cast(out.output, request, response)
  
          # File-like objects.
          if hasattr(out, 'read'):
              if 'wsgi.file_wrapper' in request.environ:
                  return request.environ['wsgi.file_wrapper'](out)
              elif hasattr(out, 'close') or not hasattr(out, '__iter__'):
                  return WSGIFileWrapper(out)
  
          # Handle Iterables. We peek into them to detect their inner type.
          try:
              out = iter(out)
              first = out.next()
              while not first:
                  first = out.next()
          except StopIteration:
              return self._cast('', request, response)
          except HTTPResponse, e:
              first = e
          except Exception, e:
              first = HTTPError(500, 'Unhandled exception', e, format_exc(10))
              if isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError))\
              or not self.catchall:
                  raise
          # These are the inner types allowed in iterator or generator objects.
          if isinstance(first, HTTPResponse):
              return self._cast(first, request, response)
          if isinstance(first, bytes):
              return itertools.chain([first], out)
          if isinstance(first, unicode):
              return itertools.imap(lambda x: x.encode(response.charset),
                                    itertools.chain([first], out))
          return self._cast(HTTPError(500, 'Unsupported response type: %s'\
                                           % type(first)), request, response)
function_tokens: def _cast ( self , out , request , response , peek = None ) : # Filtered types (recursive, because they may return anything) for testtype , filterfunc in self . castfilter : if isinstance ( out , testtype ) : return self . _cast ( filterfunc ( out ) , request , response ) # Empty output is done here if not out : response . headers [ 'Content-Length' ] = 0 return [ ] # Join lists of byte or unicode strings. Mixed lists are NOT supported if isinstance ( out , ( tuple , list ) ) and isinstance ( out [ 0 ] , ( bytes , unicode ) ) : out = out [ 0 ] [ 0 : 0 ] . join ( out ) # b'abc'[0:0] -> b'' # Encode unicode strings if isinstance ( out , unicode ) : out = out . encode ( response . charset ) # Byte Strings are just returned if isinstance ( out , bytes ) : response . headers [ 'Content-Length' ] = str ( len ( out ) ) return [ out ] # HTTPError or HTTPException (recursive, because they may wrap anything) if isinstance ( out , HTTPError ) : out . apply ( response ) return self . _cast ( self . error_handler . get ( out . status , repr ) ( out ) , request , response ) if isinstance ( out , HTTPResponse ) : out . apply ( response ) return self . _cast ( out . output , request , response ) # File-like objects. if hasattr ( out , 'read' ) : if 'wsgi.file_wrapper' in request . environ : return request . environ [ 'wsgi.file_wrapper' ] ( out ) elif hasattr ( out , 'close' ) or not hasattr ( out , '__iter__' ) : return WSGIFileWrapper ( out ) # Handle Iterables. We peek into them to detect their inner type. try : out = iter ( out ) first = out . next ( ) while not first : first = out . next ( ) except StopIteration : return self . _cast ( '' , request , response ) except HTTPResponse , e : first = e except Exception , e : first = HTTPError ( 500 , 'Unhandled exception' , e , format_exc ( 10 ) ) if isinstance ( e , ( KeyboardInterrupt , SystemExit , MemoryError ) ) or not self . catchall : raise # These are the inner types allowed in iterator or generator objects. if isinstance ( first , HTTPResponse ) : return self . _cast ( first , request , response ) if isinstance ( first , bytes ) : return itertools . chain ( [ first ] , out ) if isinstance ( first , unicode ) : return itertools . imap ( lambda x : x . encode ( response . charset ) , itertools . chain ( [ first ] , out ) ) return self . _cast ( HTTPError ( 500 , 'Unsupported response type: %s' % type ( first ) ) , request , response )