<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' ] <EOL> import re <EOL> import sys <EOL> from types import DictType , StringType , TupleType , ListType <EOL> import warnings <EOL> header_re = re . compile ( r'<STR_LIT>' ) <EOL> bad_header_value_re = re . compile ( r'<STR_LIT>' ) <EOL> class WSGIWarning ( Warning ) : <EOL> """<STR_LIT>""" <EOL> def assert_ ( cond , * args ) : <EOL> if not cond : <EOL> raise AssertionError ( * args ) <EOL> def validator ( application ) : <EOL> """<STR_LIT>""" <EOL> def lint_app ( * args , ** kw ) : <EOL> assert_ ( len ( args ) == <NUM_LIT:2> , "<STR_LIT>" ) <EOL> assert_ ( not kw , "<STR_LIT>" ) <EOL> environ , start_response = args <EOL> check_environ ( environ ) <EOL> start_response_started = [ ] <EOL> def start_response_wrapper ( * args , ** kw ) : <EOL> assert_ ( len ( args ) == <NUM_LIT:2> or len ( args ) == <NUM_LIT:3> , ( <EOL> "<STR_LIT>" % ( args , ) ) ) <EOL> assert_ ( not kw , "<STR_LIT>" ) <EOL> status = args [ <NUM_LIT:0> ] <EOL> headers = args [ <NUM_LIT:1> ] <EOL> if len ( args ) == <NUM_LIT:3> : <EOL> exc_info = args [ <NUM_LIT:2> ] <EOL> else : <EOL> exc_info = None <EOL> check_status ( status ) <EOL> check_headers ( headers ) <EOL> check_content_type ( status , headers ) <EOL> check_exc_info ( exc_info ) <EOL> start_response_started . append ( None ) <EOL> return WriteWrapper ( start_response ( * args ) ) <EOL> environ [ '<STR_LIT>' ] = InputWrapper ( environ [ '<STR_LIT>' ] ) <EOL> environ [ '<STR_LIT>' ] = ErrorWrapper ( environ [ '<STR_LIT>' ] ) <EOL> iterator = application ( environ , start_response_wrapper ) <EOL> assert_ ( iterator is not None and iterator != False , <EOL> "<STR_LIT>" ) <EOL> check_iterator ( iterator ) <EOL> return IteratorWrapper ( iterator , start_response_started ) <EOL> return lint_app <EOL> class InputWrapper : <EOL> def __init__ ( self , wsgi_input ) : <EOL> self . input = wsgi_input <EOL> def read ( self , * args ) : <EOL> assert_ ( len ( args ) <= <NUM_LIT:1> ) <EOL> v = self . input . read ( * args ) <EOL> assert_ ( type ( v ) is type ( "<STR_LIT>" ) ) <EOL> return v <EOL> def readline ( self ) : <EOL> v = self . input . readline ( ) <EOL> assert_ ( type ( v ) is type ( "<STR_LIT>" ) ) <EOL> return v <EOL> def readlines ( self , * args ) : <EOL> assert_ ( len ( args ) <= <NUM_LIT:1> ) <EOL> lines = self . input . readlines ( * args ) <EOL> assert_ ( type ( lines ) is type ( [ ] ) ) <EOL> for line in lines : <EOL> assert_ ( type ( line ) is type ( "<STR_LIT>" ) ) <EOL> return lines <EOL> def __iter__ ( self ) : <EOL> while <NUM_LIT:1> : <EOL> line = self . readline ( ) <EOL> if not line : <EOL> return <EOL> yield line <EOL> def close ( self ) : <EOL> assert_ ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> class ErrorWrapper : <EOL> def __init__ ( self , wsgi_errors ) : <EOL> self . errors = wsgi_errors <EOL> def write ( self , s ) : <EOL> assert_ ( type ( s ) is type ( "<STR_LIT>" ) ) <EOL> self . errors . write ( s ) <EOL> def flush ( self ) : <EOL> self . errors . flush ( ) <EOL> def writelines ( self , seq ) : <EOL> for line in seq : <EOL> self . write ( line ) <EOL> def close ( self ) : <EOL> assert_ ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> class WriteWrapper : <EOL> def __init__ ( self , wsgi_writer ) : <EOL> self . writer = wsgi_writer <EOL> def __call__ ( self , s ) : <EOL> assert_ ( type ( s ) is type ( "<STR_LIT>" ) ) <EOL> self . writer ( s ) <EOL> class PartialIteratorWrapper : <EOL> def __init__ ( self , wsgi_iterator ) : <EOL> self . iterator = wsgi_iterator <EOL> def __iter__ ( self ) : <EOL> return IteratorWrapper ( self . iterator , None ) <EOL> class IteratorWrapper : <EOL> def __init__ ( self , wsgi_iterator , check_start_response ) : <EOL> self . original_iterator = wsgi_iterator <EOL> self . iterator = iter ( wsgi_iterator ) <EOL> self . closed = False <EOL> self . check_start_response = check_start_response <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> assert_ ( not self . closed , <EOL> "<STR_LIT>" ) <EOL> v = self . iterator . next ( ) <EOL> if self . check_start_response is not None : <EOL> assert_ ( self . check_start_response , <EOL> "<STR_LIT>" ) <EOL> self . check_start_response = None <EOL> return v <EOL> def close ( self ) : <EOL> self . closed = True <EOL> if hasattr ( self . original_iterator , '<STR_LIT>' ) : <EOL> self . original_iterator . close ( ) <EOL> def __del__ ( self ) : <EOL> if not self . closed : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" ) <EOL> assert_ ( self . closed , <EOL> "<STR_LIT>" ) <EOL> def check_environ ( environ ) : <EOL> assert_ ( type ( environ ) is DictType , <EOL> "<STR_LIT>" <EOL> % ( type ( environ ) , environ ) ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> assert_ ( key in environ , <EOL> "<STR_LIT>" % ( key , ) ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> assert_ ( key not in environ , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( key , key [ <NUM_LIT:5> : ] ) ) <EOL> if '<STR_LIT>' not in environ : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> WSGIWarning ) <EOL> for key in environ . keys ( ) : <EOL> if '<STR_LIT:.>' in key : <EOL> continue <EOL> assert_ ( type ( environ [ key ] ) is StringType , <EOL> "<STR_LIT>" <EOL> % ( key , type ( environ [ key ] ) , environ [ key ] ) ) <EOL> assert_ ( type ( environ [ '<STR_LIT>' ] ) is TupleType , <EOL> "<STR_LIT>" % ( environ [ '<STR_LIT>' ] , ) ) <EOL> assert_ ( environ [ '<STR_LIT>' ] in ( '<STR_LIT:http>' , '<STR_LIT>' ) , <EOL> "<STR_LIT>" % environ [ '<STR_LIT>' ] ) <EOL> check_input ( environ [ '<STR_LIT>' ] ) <EOL> check_errors ( environ [ '<STR_LIT>' ] ) <EOL> if environ [ '<STR_LIT>' ] not in ( <EOL> '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" % environ [ '<STR_LIT>' ] , <EOL> WSGIWarning ) <EOL> assert_ ( not environ . get ( '<STR_LIT>' ) <EOL> or environ [ '<STR_LIT>' ] . startswith ( '<STR_LIT:/>' ) , <EOL> "<STR_LIT>" % environ [ '<STR_LIT>' ] ) <EOL> assert_ ( not environ . get ( '<STR_LIT>' ) <EOL> or environ [ '<STR_LIT>' ] . startswith ( '<STR_LIT:/>' ) , <EOL> "<STR_LIT>" % environ [ '<STR_LIT>' ] ) <EOL> if environ . get ( '<STR_LIT>' ) : <EOL> assert_ ( int ( environ [ '<STR_LIT>' ] ) >= <NUM_LIT:0> , <EOL> "<STR_LIT>" % environ [ '<STR_LIT>' ] ) <EOL> if not environ . get ( '<STR_LIT>' ) : <EOL> assert_ ( '<STR_LIT>' in environ , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> assert_ ( environ . get ( '<STR_LIT>' ) != '<STR_LIT:/>' , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def check_input ( wsgi_input ) : <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> assert_ ( hasattr ( wsgi_input , attr ) , <EOL> "<STR_LIT>" <EOL> % ( wsgi_input , attr ) ) <EOL> def check_errors ( wsgi_errors ) : <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> assert_ ( hasattr ( wsgi_errors , attr ) , <EOL> "<STR_LIT>" <EOL> % ( wsgi_errors , attr ) ) <EOL> def check_status ( status ) : <EOL> assert_ ( type ( status ) is StringType , <EOL> "<STR_LIT>" % status ) <EOL> status_code = status . split ( None , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> assert_ ( len ( status_code ) == <NUM_LIT:3> , <EOL> "<STR_LIT>" % status_code ) <EOL> status_int = int ( status_code ) <EOL> assert_ ( status_int >= <NUM_LIT:100> , "<STR_LIT>" % status_int ) <EOL> if len ( status ) < <NUM_LIT:4> or status [ <NUM_LIT:3> ] != '<STR_LIT:U+0020>' : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % status , WSGIWarning ) <EOL> def check_headers ( headers ) : <EOL> assert_ ( type ( headers ) is ListType , <EOL> "<STR_LIT>" <EOL> % ( headers , type ( headers ) ) ) <EOL> header_names = { } <EOL> for item in headers : <EOL> assert_ ( type ( item ) is TupleType , <EOL> "<STR_LIT>" <EOL> % ( item , type ( item ) ) ) <EOL> assert_ ( len ( item ) == <NUM_LIT:2> ) <EOL> name , value = item <EOL> assert_ ( name . lower ( ) != '<STR_LIT:status>' , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % value ) <EOL> header_names [ name . lower ( ) ] = None <EOL> assert_ ( '<STR_LIT:\n>' not in name and '<STR_LIT::>' not in name , <EOL> "<STR_LIT>" % name ) <EOL> assert_ ( header_re . search ( name ) , "<STR_LIT>" % name ) <EOL> assert_ ( not name . endswith ( '<STR_LIT:->' ) and not name . endswith ( '<STR_LIT:_>' ) , <EOL> "<STR_LIT>" % name ) <EOL> if bad_header_value_re . search ( value ) : <EOL> assert_ ( <NUM_LIT:0> , "<STR_LIT>" <EOL> % ( value , bad_header_value_re . search ( value ) . group ( <NUM_LIT:0> ) ) ) <EOL> def check_content_type ( status , headers ) : <EOL> code = int ( status . split ( None , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> NO_MESSAGE_BODY = ( <NUM_LIT> , <NUM_LIT> ) <EOL> for name , value in headers : <EOL> if name . lower ( ) == '<STR_LIT>' : <EOL> if code not in NO_MESSAGE_BODY : <EOL> return <EOL> assert_ ( <NUM_LIT:0> , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % code ) <EOL> if code not in NO_MESSAGE_BODY : <EOL> assert_ ( <NUM_LIT:0> , "<STR_LIT>" % headers ) <EOL> def check_exc_info ( exc_info ) : <EOL> assert_ ( exc_info is None or type ( exc_info ) is type ( ( ) ) , <EOL> "<STR_LIT>" % ( exc_info , type ( exc_info ) ) ) <EOL> def check_iterator ( iterator ) : <EOL> assert_ ( not isinstance ( iterator , str ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) </s>
