Your IP : 216.73.216.196


Current Path : /opt/alt/python37/lib/python3.7/site-packages/webtest/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/webtest/__pycache__/lint.cpython-37.opt-1.pyc

B

���O�<�@sdZddlZddlZddlZddlmZe�d�Ze�d�ZGdd�de	�Z
d&dd	�ZGd
d�de�Z
Gdd
�d
e�ZGdd�de�ZGdd�de�ZGdd�de�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zee_d	d%gZdS)'a"
Middleware to check for obedience to the WSGI specification.

Some of the things this checks:

* Signature of the application and start_response (including that
  keyword arguments are not used).

* Environment checks:

  - Environment is a dictionary (and not a subclass).

  - That all the required keys are in the environment: REQUEST_METHOD,
    SERVER_NAME, SERVER_PORT, wsgi.version, wsgi.input, wsgi.errors,
    wsgi.multithread, wsgi.multiprocess, wsgi.run_once

  - That HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH are not in the
    environment (these headers should appear as CONTENT_LENGTH and
    CONTENT_TYPE).

  - Warns if QUERY_STRING is missing, as the cgi module acts
    unpredictably in that case.

  - That CGI-style variables (that don't contain a .) have
    (non-unicode) string values

  - That wsgi.version is a tuple

  - That wsgi.url_scheme is 'http' or 'https' (@@: is this too
    restrictive?)

  - Warns if the REQUEST_METHOD is not known (@@: probably too
    restrictive).

  - That SCRIPT_NAME and PATH_INFO are empty or start with /

  - That at least one of SCRIPT_NAME or PATH_INFO are set.

  - That CONTENT_LENGTH is a positive integer.

  - That SCRIPT_NAME is not '/' (it should be '', and PATH_INFO should
    be '/').

  - That wsgi.input has the methods read, readline, readlines, and
    __iter__

  - That wsgi.errors has the methods flush, write, writelines

* The status is a string, contains a space, starts with an integer,
  and that integer is in range (> 100).

* That the headers is a list (not a subclass, not another kind of
  sequence).

* That the items of the headers are tuples of strings.

* That there is no 'status' header (that is used in CGI, but not in
  WSGI).

* That the headers don't contain newlines or colons, end in _ or -, or
  contain characters codes below 037.

* That Content-Type is given if there is content (CGI often has a
  default content type, but WSGI does not).

* That no Content-Type is given when there is no content (@@: is this
  too restrictive?)

* That the exc_info argument to start_response is a tuple or None.

* That all calls to the writer are with strings, and no other methods
  on the writer are accessed.

* That wsgi.input is used properly:

  - .read() is called with zero or one argument

  - That it returns a string

  - That readline, readlines, and __iter__ return strings

  - That .close() is not called

  - No other methods are provided

* That wsgi.errors is used properly:

  - .write() and .writelines() is called with a string

  - That .close() is not called, and no other methods are provided.

* The response iterator:

  - That it is not a string (it should be a list of a single string; a
    string will work, but perform horribly).

  - That .next() returns a string

  - That the iterator is not iterated over until start_response has
    been called (that can signal either a server or application
    error).

  - That .close() is called (doesn't raise exception, only prints to
    sys.stderr, because we only know it isn't called when the object
    is garbage collected).
�N)�nextz^[a-zA-Z][a-zA-Z0-9\-_]*$z[\000-\037]c@seZdZdZdS)�WSGIWarningz:
    Raised in response to WSGI-spec-related warnings
    N)�__name__�
__module__�__qualname__�__doc__�rr�=/opt/alt/python37/lib/python3.7/site-packages/webtest/lint.pyr{srcs�fdd�}|S)a�
    When applied between a WSGI server and a WSGI application, this
    middleware will check for WSGI compliancy on a number of levels.
    This middleware does not modify the request or response in any
    way, but will throw an AssertionError if anything seems off
    (except for a failure to close the application iterator, which
    will be printed to stderr -- there's no way to throw an exception
    at that point).
    cs�t|�dkstd��|r td��|\}�t|�g���fdd�}t|d�|d<t|d�|d<�||�}|dk	r||dks�td	��t|�t|��S)
N�zTwo arguments requiredzNo keyword arguments allowedcs�t|�dks$t|�dks$td|��|r0td��|d}|d}t|�dkrV|d}nd}t|�t|�t||�t|���d�t�|��S)Nr
�zInvalid number of arguments: %szNo keyword arguments allowedr�)�len�AssertionError�check_status�
check_headers�check_content_type�check_exc_info�append�WriteWrapper)�args�kw�status�headers�exc_info)�start_response�start_response_startedrr	�start_response_wrapper�s



z<middleware.<locals>.lint_app.<locals>.start_response_wrapperz
wsgi.inputzwsgi.errorsFz>The application must return an iterator, if only an empty list)r
r�
check_environ�InputWrapper�ErrorWrapper�check_iterator�IteratorWrapper)rr�environr�iterator)�application)rrr	�lint_app�s
zmiddleware.<locals>.lint_appr)r$�global_confr%r)r$r	�
middleware�s)r'c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)rcCs
||_dS)N)�input)�self�
wsgi_inputrrr	�__init__�szInputWrapper.__init__cGs4t|�dkst�|jj|�}t|�td�ks0t�|S)Nr�)r
rr(�read�type)r)r�vrrr	r-�szInputWrapper.readcGs$|jj|�}t|�td�ks t�|S)Nr,)r(�readliner.r)r)rr/rrr	r0�szInputWrapper.readlinecGsVt|�dkst�|jj|�}t|�tg�ks0t�x |D]}t|�td�ks6t�q6W|S)Nrr,)r
rr(�	readlinesr.)r)r�lines�linerrr	r1�s
zInputWrapper.readlinesccs x|��}|sdS|VqWdS)N)r0)r)r3rrr	�__iter__�s
zInputWrapper.__iter__cCsdstd��dS)Nrz input.close() must not be called)r)r)rrr	�close�szInputWrapper.closeN)	rrrr+r-r0r1r4r5rrrr	r�src@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)rcCs
||_dS)N)�errors)r)�wsgi_errorsrrr	r+�szErrorWrapper.__init__cCs$t|�td�kst�|j�|�dS)Nr,)r.rr6�write)r)�srrr	r8�szErrorWrapper.writecCs|j��dS)N)r6�flush)r)rrr	r:�szErrorWrapper.flushcCsx|D]}|�|�qWdS)N)r8)r)�seqr3rrr	�
writelines�s
zErrorWrapper.writelinescCsdstd��dS)Nrz!errors.close() must not be called)r)r)rrr	r5�szErrorWrapper.closeN)rrrr+r8r:r<r5rrrr	r�s
rc@seZdZdd�Zdd�ZdS)rcCs
||_dS)N)�writer)r)Zwsgi_writerrrr	r+�szWriteWrapper.__init__cCs"t|�td�kst�|�|�dS)Nr,)r.rr=)r)r9rrr	�__call__�szWriteWrapper.__call__N)rrrr+r>rrrr	r�src@seZdZdd�Zdd�ZdS)�PartialIteratorWrappercCs
||_dS)N)r#)r)�
wsgi_iteratorrrr	r+�szPartialIteratorWrapper.__init__cCs
t|j�S)N)r!r#)r)rrr	r4�szPartialIteratorWrapper.__iter__N)rrrr+r4rrrr	r?�sr?c@s8eZdZdd�Zdd�Zdd�ZeZdd�Zd	d
�ZdS)r!cCs ||_t|�|_d|_||_dS)NF)�original_iterator�iterr#�closed�check_start_response)r)r@rDrrr	r+s
zIteratorWrapper.__init__cCs|S)Nr)r)rrr	r4szIteratorWrapper.__iter__cCsV|jrtd��t|j�}|jdk	r6|js0td��d|_t|t�sRtd|j|f��|S)NzIterator read after closedzjThe application returns and we started iterating over its body, but start_response has not yet been calledz)Iterator %r returned a non-str object: %r)rCrrr#rD�
isinstance�str)r)r/rrr	rs

zIteratorWrapper.nextcCs d|_t|jd�r|j��dS)NTr5)rC�hasattrrAr5)r)rrr	r5szIteratorWrapper.closecCs$|jstj�d�|js td��dS)Nz/Iterator garbage collected without being closed)rC�sys�stderrr8r)r)rrr	�__del__$s
zIteratorWrapper.__del__N)	rrrr+r4r�__next__r5rJrrrr	r!sr!cCst|�tks tdt|�|f��x dD]}||ks&td|��q&Wx,dD]$}||ksHtd||dd�f��qHWd|kr�t�dt�xD|D]<}d	|kr�q�t||�tks�td
|t||�||f��q�Wt|d�tks�td|d��|d
dk�std|d
��t|d�t	|d�|ddk�rBt�d|dt�|�
d��rn|d�d��sntd|d��|�
d��r�|d�d��s�td|d��|�
d��r�t|d�dk�s�td|d��|�
d��s�d|k�s�td��|�
d�dk�s�td��dS)Nz:Environment is not of the right type: %r (environment: %r))	�REQUEST_METHODZSERVER_NAMEZSERVER_PORTzwsgi.versionz
wsgi.inputzwsgi.errorszwsgi.multithreadzwsgi.multiprocessz
wsgi.run_oncez$Environment missing required key: %r)ZHTTP_CONTENT_TYPEZHTTP_CONTENT_LENGTHz8Environment should not have the key: %s (use %s instead)�ZQUERY_STRINGz�QUERY_STRING is not in the WSGI environment; the cgi module will use sys.argv when this variable is missing, so application errors are more likely�.z9Environmental variable %s is not a string: %r (value: %r)zwsgi.versionz#wsgi.version should be a tuple (%r)zwsgi.url_scheme)ZhttpZhttpszwsgi.url_scheme unknown: %rz
wsgi.inputzwsgi.errorsrL)ZGETZHEADZPOSTZOPTIONSZPUTZDELETEZTRACEzUnknown REQUEST_METHOD: %rZSCRIPT_NAME�/z$SCRIPT_NAME doesn't start with /: %rZ	PATH_INFOz"PATH_INFO doesn't start with /: %rZCONTENT_LENGTHrzInvalid CONTENT_LENGTH: %rzgOne of SCRIPT_NAME or PATH_INFO are required (PATH_INFO should at least be '/' if SCRIPT_NAME is empty)zOSCRIPT_NAME cannot be '/'; it should instead be '', and PATH_INFO should be '/')
r.�dictr�warnings�warnrrF�tuple�check_input�check_errors�get�
startswith�int)r"�keyrrr	r,sZ





rcCs,x&dD]}t||�std||f��qWdS)N)r-r0r1r4z-wsgi.input (%r) doesn't have the attribute %s)rGr)r*�attrrrr	rTns
rTcCs,x&dD]}t||�std||f��qWdS)N)r:r8r<z.wsgi.errors (%r) doesn't have the attribute %s)rGr)r7rZrrr	rUus
rUcCs�t|�tkstd|��|�dd�d}t|�dks@td|��t|�}|dks\td|��t|�dkst|dd	kr�t�d
|t�dS)Nz Status must be a string (not %r)rrrz)Status codes must be three characters: %r�dzStatus code is invalid: %r�� zjThe status string (%r) should be a three-digit integer followed by a single space and a status explanation)	r.rFr�splitr
rXrQrRr)rZstatus_codeZ
status_intrrr	r|s

rcCs
t|�tks td|t|�f��i}x�|D]�}t|�tksNtd|t|�f��t|�dks^t�|\}}|��dks~td|��d||��<d|kr�d|ks�td|��t�|�s�td	|��|�d
�s�|�d�r�td|��t	�|�r*td
|t	�|��
d�f��q*WdS)Nz%Headers (%r) must be of type list: %rz1Individual headers (%r) must be of type tuple: %rr
rzyThe Status header cannot be used; it conflicts with CGI script, and HTTP status is not given through headers (value: %r).�
�:z,Header names may not contain ':' or '\n': %rzBad header name: %r�-�_z#Names may not end in '-' or '_': %rz#Bad header value: %r (bad char: %r)r)r.�listrrSr
�lower�	header_re�search�endswith�bad_header_value_re�group)rZheader_names�item�name�valuerrr	r�s,


rcCs�t|�dd�d�}d}d}d}x,|D]$\}}|��dkr&|��r&t|�}q&WxT|D]L\}}|��dkrT||krtdS|dkr�t�d|t�dSdsTtd|��qTW||kr�|dk	r�|dkr�ds�td	|��dS)
Nrr)����i0)rni0zcontent-lengthzcontent-typezEContent-Type header found in a %s response, which not return content.zJContent-Type header found in a %s response, which must not return content.z,No Content-Type header found in headers (%s))rXr^rd�isdigitrQrRrr)rr�codeZNO_MESSAGE_BODYZNO_MESSAGE_TYPE�lengthrkrlrrr	r�s(rcCs0|dks,t|�td�ks,td|t|�f��dS)Nrz exc_info (%r) is not a tuple: %r)r.r)rrrr	r�srcCst|t�rtd��dS)NzvYou should not return a string as your application iterator, instead return a single-item list containing that string.)rErFr)r#rrr	r �sr cCst|�S)N)r')r$r&rrr	�make_middleware�srr)N)r�rerHrQZwebtest.compatr�compilererh�Warningrr'�objectrrrr?r!rrTrUrrrrr rr�__all__rrrr	�<module>ps0


8#

(B