B
    Úsüd¶  ã            .   @   sž  d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl.m0Z0 ddl1m2Z2 er¦ddl%m3Z3 ej4dkrÌej5Z6ej7Z8ej7Z9ej7Z:nej;Z6ej<Z8ej=Z9ej>Z:e2d ƒ Z?ej@jA› de$› ZBdd rd!pd" ZCd#eB eC ZDG d$d%„ d%ejEjFejEjGƒZHeeI ejJejKdd&œd'd(„ZLdejKejJeeMd)œd*d+„ZNee+eejJejKf d,œd-d.„ZOd/d0„ feeeePgdf eejK d1œd2d3„ZQdxejReIeeP ee+ dd4œd5d6„ZSeTePd7œd8d9„ZUee+ eeV d:œd;d<„ZWeTePd7œd=d>„ZXeTeMd7œd?d@„ZYeeP eMePdAœdBdC„ZZeeP eeM eeP eeT ePdDœdEdF„Z[eVePePddGœdHdI„Z\eMdJœdKdL„Z]ej^dMej_dNej`dOejadPiZbejcdQejddRejedSejfdTejgdUejhdVejidWejjdXejkdYejldZejmd[ejnd\ejod]ejpd^ejqd_ejrd`ejsdaejtdbejudcejvddejwdeejxdfejydgiZzej{eej{ dhœdidj„Z|ej}dkdleIeeVePf dmœdndo„ƒZ~G dpdq„ dqejƒZ€eeMdrœdsdt„Zeeduœdvdw„Z‚dS )yz5Rewrite assertion AST to produce nice error messages.é    N)ÚPath)ÚPurePath)ÚCallable)ÚDict)ÚIO)ÚIterable)ÚIterator)ÚList)ÚOptional)ÚSequence)ÚSet)ÚTuple)ÚTYPE_CHECKING)ÚUnion)ÚDEFAULT_REPR_MAX_SIZE)Úsaferepr)Úversion)Úutil)Úformat_explanation)ÚConfig)ÚSession)Úabsolutepath)Ú
fnmatch_ex)ÚStashKey)ÚAssertionState)é   é   r   z-pytest-z.pyTÚcÚoÚ.c               @   sP  e Zd ZdZeddœdd„Zee ddœdd„Ze	j
jjZd*eeeeeef   eej ee	j
j d	œd
d„Ze	j
jeej dœdd„Zejddœdd„Zededœdd„Zeededœdd„Zededœdd„Zeddœdd„Zeddœdd „Zeeef ed!œd"d#„Zejd$krLejd%kr0d&d'l m!Z! nd&d'l"m!Z! ee!dœd(d)„Z#dS )+ÚAssertionRewritingHookz1PEP302/PEP451 import hook which rewrites asserts.N)ÚconfigÚreturnc             C   sh   || _ y| d¡| _W n tk
r4   ddg| _Y nX d | _i | _tƒ | _d| _dh| _	i | _
d| _d S )NZpython_filesz	test_*.pyz	*_test.pyFZconftest)r!   ÚgetiniÚfnpatsÚ
ValueErrorÚsessionÚ_rewritten_namesÚsetÚ_must_rewriteÚ_writing_pycÚ_basenames_to_check_rewriteÚ_marked_for_rewrite_cacheÚ_session_paths_checked)Úselfr!   © r/   úH/home/dcms/DCMS/lib/python3.7/site-packages/_pytest/assertion/rewrite.pyÚ__init__F   s    zAssertionRewritingHook.__init__)r&   r"   c             C   s   || _ d| _d S )NF)r&   r-   )r.   r&   r/   r/   r0   Úset_sessionV   s    z"AssertionRewritingHook.set_session)ÚnameÚpathÚtargetr"   c             C   s¢   | j r
d S | jjt }|  ||¡r&d S | d| ¡ |  ||¡}|d ksp|jd kspt|j	t
jjƒrptj |j¡std S |j}|  |||¡sŒd S t
jj||| |jdS )Nzfind_module called for: %s)ÚloaderÚsubmodule_search_locations)r*   r!   ÚstashÚassertstate_keyÚ_early_rewrite_bailoutÚtraceÚ
_find_specÚoriginÚ
isinstancer6   Ú	importlibÚ	machineryÚSourceFileLoaderÚosr4   ÚexistsÚ_should_rewriter   Úspec_from_file_locationr7   )r.   r3   r4   r5   ÚstateÚspecÚfnr/   r/   r0   Ú	find_spec]   s(    
z AssertionRewritingHook.find_spec)rG   r"   c             C   s   d S )Nr/   )r.   rG   r/   r/   r0   Úcreate_module…   s    z$AssertionRewritingHook.create_module)Úmoduler"   c             C   s  |j d k	st‚|j jd k	st‚t|j jƒ}| jjt }|| j|j< t	j
 }t|ƒ}|rvt|ƒ}|svd}| d|› ¡ |jd d… t }|| }t|||jƒ}	|	d krî| d|›¡ t|| jƒ\}
}	|rþd| _zt||	|
|ƒ W d d| _X n| d|› ¡ t|	|jƒ d S )NFzread only directory: éýÿÿÿz
rewriting Tzfound cached rewritten pyc for )Ú__spec__ÚAssertionErrorr=   r   r!   r8   r9   r'   Ú__name__ÚsysÚdont_write_bytecodeÚget_cache_dirÚtry_makedirsr;   r3   ÚPYC_TAILÚ	_read_pycÚ_rewrite_testr*   Ú
_write_pycÚexecÚ__dict__)r.   rK   rH   rF   ÚwriteÚ	cache_dirÚokZ
cache_nameÚpycÚcoÚsource_statr/   r/   r0   Úexec_moduleŠ   s2    

z"AssertionRewritingHook.exec_moduler   )r3   rF   r"   c             C   sÐ   | j dk	rT| jsTd| _x<| j jD ]0}t|ƒ tj¡}| j tj	 
|d ¡d ¡ q W | d¡}|d | jkrpdS t|Ž  d¡}x,| jD ]"}tj	 |¡ršdS t||ƒr†dS q†W |  ||¡r¼dS | d|› ¡ dS )	a'  A fast way to get out of rewriting modules.

        Profiling has shown that the call to PathFinder.find_spec (inside of
        the find_spec from this class) is a major slowdown, so, this method
        tries to filter what we're sure won't be rewritten before getting to
        it.
        NTéÿÿÿÿr   r   Fz.pyz early skip of rewriting module: )r&   r-   Z_initialpathsÚstrÚsplitrB   Úsepr+   Úaddr4   Úsplitextr   Úwith_suffixr$   Údirnamer   Ú_is_marked_for_rewriter;   )r.   r3   rF   Zinitial_pathÚpartsr4   Úpatr/   r/   r0   r:   ´   s$     

z-AssertionRewritingHook._early_rewrite_bailout)r3   rH   rF   r"   c             C   s”   t j |¡dkr$| d|›¡ dS | jd k	rR| j t|ƒ¡rR| d|›¡ dS t|ƒ}x,| jD ]"}t	||ƒrb| d|›¡ dS qbW |  
||¡S )Nzconftest.pyzrewriting conftest file: Tz.matched test file (was specified on cmdline): zmatched test file )rB   r4   Úbasenamer;   r&   Z
isinitpathr   r   r$   r   ri   )r.   r3   rH   rF   Zfn_pathrk   r/   r/   r0   rD   Û   s    

z&AssertionRewritingHook._should_rewritec             C   sz   y
| j | S  tk
rt   xJ| jD ]@}||ks<| |d ¡r"| d|›d|›d¡ d| j |< dS q"W d| j |< dS X d S )Nr   zmatched marked file z (from ú)TF)r,   ÚKeyErrorr)   Ú
startswithr;   )r.   r3   rF   Zmarkedr/   r/   r0   ri   ð   s    


z-AssertionRewritingHook._is_marked_for_rewrite)Únamesr"   c             G   st   t |ƒ tj¡ | j¡}x@|D ]8}tj| }t |jp8d¡st	|j
t| ƒƒs|  |¡ qW | j |¡ | j ¡  dS )zžMark import names as needing to be rewritten.

        The named module or package as well as any nested modules will
        be rewritten on import.
        Ú N)r(   ÚintersectionrP   ÚmodulesÚ
differencer'   ÚAssertionRewriterÚis_rewrite_disabledÚ__doc__r>   Ú
__loader__ÚtypeÚ_warn_already_importedr)   Úupdater,   Úclear)r.   rp   Zalready_importedr3   Úmodr/   r/   r0   Úmark_rewriteý   s    

z#AssertionRewritingHook.mark_rewrite)r3   r"   c             C   s(   ddl m} | jj|d| ƒdd d S )Nr   )ÚPytestAssertRewriteWarningz2Module already imported so cannot be rewritten: %sé   )Ú
stacklevel)Ú_pytest.warning_typesr   r!   Zissue_config_time_warning)r.   r3   r   r/   r/   r0   rz     s
    z-AssertionRewritingHook._warn_already_imported)Úpathnamer"   c          	   C   s   t |dƒ
}| ¡ S Q R X dS )zOptional PEP302 get_data API.ÚrbN)ÚopenÚread)r.   rƒ   Úfr/   r/   r0   Úget_data  s    zAssertionRewritingHook.get_data)r   é
   )r   é   r   )ÚTraversableResourcesc             C   s:   t jdk rddlm} nddlm} |tj| j| dƒS )N)r   é   r   )Ú
FileReader)r4   )rP   Úversion_infoZimportlib.readersr   Zimportlib.resources.readersÚtypesÚSimpleNamespacer'   )r.   r3   r   r/   r/   r0   Úget_resource_reader$  s
    
z*AssertionRewritingHook.get_resource_reader)NN)$rO   Ú
__module__Ú__qualname__rw   r   r1   r
   r   r2   r?   r@   Ú
PathFinderrI   r<   rb   r   r   Úbytesr   Ú
ModuleTypeÚ
ModuleSpecrJ   r`   Úboolr:   rD   ri   r~   rz   rˆ   rP   rŽ   Zimportlib.resources.abcr‹   Úimportlib.abcr‘   r/   r/   r/   r0   r    C   s*   
 $*'
r    )Úfpr_   r^   r"   c             C   s\   |   tjj¡ d}|   |¡ t|jƒd@ }|jd@ }|   t d||¡¡ |   t	 
|¡¡ d S )Ns       l   ÿÿ z<LL)rZ   r?   r   ÚMAGIC_NUMBERÚintÚst_mtimeÚst_sizeÚstructÚpackÚmarshalÚdumps)rš   r_   r^   ÚflagsÚmtimeÚsizer/   r/   r0   Ú_write_pyc_fp/  s    

r¦   )rF   r^   r_   r]   r"   c          
   C   sÂ   |› dt  ¡ › }y&t|dƒ}t|||ƒ W d Q R X W n: tk
rr } z|  d|› d|j› ¡ dS d }~X Y nX yt  ||¡ W n8 tk
r¼ } z|  d|› d|› ¡ dS d }~X Y nX dS )Nr   Úwbzerror writing pyc file at z: errno=Fz: T)rB   Úgetpidr…   r¦   ÚOSErrorr;   ÚerrnoÚreplace)rF   r^   r_   r]   Zproc_pycrš   Úer/   r/   r0   rW   A  s    rW   )rH   r!   r"   c             C   sN   t  | ¡}|  ¡ }t| ƒ}tj||d}t||||ƒ t||ddd}||fS )z1Read and rewrite *fn* and return the code object.)ÚfilenamerX   T)Údont_inherit)rB   ÚstatÚ
read_bytesrb   ÚastÚparseÚrewrite_assertsÚcompile)rH   r!   r¯   ÚsourceZstrfnÚtreer^   r/   r/   r0   rV   Z  s    
rV   c             C   s   d S )Nr/   )Úxr/   r/   r0   Ú<lambda>f  ó    r¸   )rµ   r]   r;   r"   c             C   sÄ  yt |dƒ}W n tk
r"   dS X | y(t | ¡}t|jƒ}|j}| d¡}W n6 tk
rŠ } z|d| › d|› ƒ dS d}~X Y nX t|ƒdkr¨|d|  ƒ dS |dd… t	j
jkrÌ|d|  ƒ dS |dd	… d
krì|d|  ƒ dS |d	d… }	t |	d¡|d@ kr|d|  ƒ dS |dd… }
t |
d¡|d@ krP|d|  ƒ dS yt |¡}W n8 tk
r– } z|d| › d|› ƒ dS d}~X Y nX t|tjƒs¶|d|  ƒ dS |S Q R X dS )zsPossibly read a pytest pyc containing rewritten code.

    Return rewritten code if successful or None if not.
    r„   Né   z
_read_pyc(z): OSError z&_read_pyc(%s): invalid pyc (too short)é   z-_read_pyc(%s): invalid pyc (bad magic number)r   s       z._read_pyc(%s): invalid pyc (unsupported flags)rŠ   Úlittlel   ÿÿ z_read_pyc(%s): out of datez+_read_pyc(%s): invalid pyc (incorrect size)z): marshal.load error z _read_pyc(%s): not a code object)r…   r©   rB   r¯   rœ   r   rž   r†   Úlenr?   r   r›   Ú
from_bytesr¡   ÚloadÚ	Exceptionr>   r   ÚCodeType)rµ   r]   r;   rš   Ústat_resultr¤   r¥   Údatar¬   Z
mtime_dataZ	size_datar^   r/   r/   r0   rU   e  sN    

rU   )r}   rµ   Úmodule_pathr!   r"   c             C   s   t |||ƒ | ¡ dS )z%Rewrite the assert statements in mod.N)ru   Úrun)r}   rµ   rÄ   r!   r/   r/   r0   r³   –  s    r³   )Úobjr"   c             C   s   t tjƒ}t| |d dd¡S )a§  Get a safe repr of an object for assertion error messages.

    The assertion formatting (util.format_explanation()) requires
    newlines to be escaped since they are a special character for it.
    Normally assertion.util.format_explanation() does this but for a
    custom repr it is possible to contain one of the special escape
    sequences, especially '\n{' and '\n}' are likely to be present in
    JSON reprs.
    )ÚmaxsizeÚ
z\n)Ú_get_maxsize_for_safereprr   Ú_configr   r«   )rÆ   rÇ   r/   r/   r0   Ú	_saferepr   s    

rË   )r!   r"   c             C   s6   | dk	r|   d¡nd}|dkr"dS |dkr2td S tS )zJGet `maxsize` configuration for saferepr based on the given config object.NÚverboser   é   é   r‰   )Z	getoptionr   )r!   Ú	verbosityr/   r/   r0   rÉ   ®  s    rÉ   c             C   sF   ddg}t | tƒs$t| ƒ} | d¡ x|D ]\}}|  ||¡} q*W | S )zñFormat the custom assertion message given.

    For strings this simply replaces newlines with '\n~' so that
    util.format_explanation() will preserve them instead of escaping
    newlines.  For other objects saferepr() is used first.
    )rÈ   z
~)ú%z%%)z\nz
~)r>   rb   r   Úappendr«   )rÆ   ZreplacesÚr1Úr2r/   r/   r0   Ú_format_assertmsg¸  s    

rÔ   c             C   s2   t | ƒrdS yt| dƒ S  tk
r,   dS X d S )NFrO   T)ÚcallableÚhasattrrÀ   )rÆ   r/   r/   r0   Ú_should_repr_global_nameÎ  s    r×   )ÚexplanationsÚis_orr"   c             C   s&   d|r
dpd  | ¡ d }| dd¡S )Nú(z or z and rm   rÐ   z%%)Újoinr«   )rØ   rÙ   Úexplanationr/   r/   r0   Ú_format_boolopØ  s    rÝ   )ÚopsÚresultsÚexplsÚeach_objr"   c       	   	   C   s†   xLt tt| ƒƒ||ƒD ]4\}}}y
| }W n tk
rB   d}Y nX |rP qW tjd k	r‚t | | || ||d  ¡}|d k	r‚|S |S )NTrÎ   )ÚzipÚranger½   rÀ   r   Z_reprcompare)	rÞ   rß   rà   rá   ÚiÚresÚexplÚdoneZcustomr/   r/   r0   Ú_call_reprcompareÝ  s     


rè   )ÚlinenoÚorigræ   r"   c             C   s   t jd k	rt  | ||¡ d S )N)r   Ú_assertion_pass)ré   rê   ræ   r/   r/   r0   Ú_call_assertion_passñ  s    
rì   )r"   c               C   s   t jr
dS dS )zŠCheck if any plugins implement the pytest_assertion_pass hook
    in order not to generate explanation unnecessarily (might be expensive).TF)r   rë   r/   r/   r/   r0   Ú_check_if_assertion_pass_implö  s    rí   znot %sz~%sz-%sz+%sú|ú^ú&z<<z>>ú+ú-Ú*ú/z//z%%z==z!=ú<z<=ú>z>=z**Úiszis notÚinznot inú@)Únoder"   c             c   s,   | V  x t  | ¡D ]}t|ƒE dH  qW dS )zARecursively yield node and all its children in depth-first order.N)r±   Úiter_child_nodesÚtraverse_node)rú   Úchildr/   r/   r0   rü     s    rü   rÎ   )rÇ   )Úsrcr"   c       
         s¸  i ‰d‰g ‰d‰ t ƒ ‰ddœ‡ ‡‡‡‡fdd„}t t | ¡j¡}xn|D ]d\}}\}}}}|tjkrv|dkrv|‰ qJˆ dk	rJ|tjkrš|dkršˆd7 ‰n|tjkr´|d	kr´ˆd8 ‰ˆsÖˆ ||d… ¡ ˆ |¡ qJˆdkrv|tjkrv|d
krv|ˆkr<t	ˆƒdkr<|t	ˆd ƒ t	|ƒ }	ˆd d|	… ˆd< n2|ˆkr\ˆd d|… ˆd< nˆ |d|… ¡ |ƒ  qJ|tj
tjhkr|ƒ  qJˆrJ|ˆkrJˆ |¡ ˆ |¡ qJW ˆS )z<Return a mapping from {lineno: "assertion test expression"}.r   N)r"   c                  s:   ˆ d k	st ‚d ˆ¡ ¡  d¡ˆˆ < d‰g ‰d ‰ tƒ ‰d S )Nrq   ú\r   )rN   rÛ   Úrstripr(   r/   )Úassert_linenoÚdepthÚlinesÚretÚ
seen_linesr/   r0   Ú_write_and_reset*  s    z._get_assertion_exprs.<locals>._write_and_resetÚassertz([{rÎ   z)]}ú,ra   )r(   ÚtokenizeÚioÚBytesIOÚreadlineÚNAMEÚOPrÑ   re   r½   ÚNEWLINEÚ	ENDMARKER)
rþ   r  ÚtokensÚtprµ   ré   ÚoffsetÚ_ÚlineZoffset_in_trimmedr/   )r  r  r  r  r  r0   Ú_get_assertion_exprs   s@    	
 

r  c                   s  e Zd ZdZee ee eddœ‡ fdd„Ze	j
ddœdd„Zeeed	œd
d„ƒZedœdd„Ze	je	jdœdd„Ze	je	jdœdd„Zee	je	jdœdd„Zee	jdœdd„Ze	jedœdd„Zddœdd„Ze	je	jdœdd „Ze	jee	jef d!œd"d#„Ze	jee	j d$œd%d&„Z e!ee!ef dœd'd(„Z"e	jee	jef dœd)d*„Z#e	j$ee	jef d+œd,d-„Z%e	j&ee	jef d.œd/d0„Z'e	j(ee	jef d1œd2d3„Z)e	j*ee	jef d4œd5d6„Z+e	j,ee	j,ef d7œd8d9„Z-e	jee	jef d:œd;d<„Z.e	j/ee	jef d=œd>d?„Z0‡  Z1S )@ru   aŒ
  Assertion rewriting implementation.

    The main entrypoint is to call .run() with an ast.Module instance,
    this will then find all the assert statements and rewrite them to
    provide intermediate values and a detailed assertion error.  See
    http://pybites.blogspot.be/2011/07/behind-scenes-of-pytests-new-assertion.html
    for an overview of how this works.

    The entry point here is .run() which will iterate over all the
    statements in an ast.Module and for each ast.Assert statement it
    finds call .visit() with it.  Then .visit_Assert() takes over and
    is responsible for creating new ast statements to replace the
    original assert statement: it rewrites the test of an assertion
    to provide intermediate values and replace it with an if statement
    which raises an assertion error with a detailed explanation in
    case the expression is false and calls pytest_assertion_pass hook
    if expression is true.

    For this .visit_Assert() uses the visitor pattern to visit all the
    AST nodes of the ast.Assert.test field, each visit call returning
    an AST node and the corresponding explanation string.  During this
    state is kept in several instance attributes:

    :statements: All the AST statements which will replace the assert
       statement.

    :variables: This is populated by .variable() with each variable
       used by the statements so that they can all be set to None at
       the end of the statements.

    :variable_counter: Counter to create new unique variables needed
       by statements.  Variables are created using .variable() and
       have the form of "@py_assert0".

    :expl_stmts: The AST statements which will be executed to get
       data from the assertion.  This is the code which will construct
       the detailed assertion message that is used in the AssertionError
       or for the pytest_assertion_pass hook.

    :explanation_specifiers: A dict filled by .explanation_param()
       with %-formatting placeholders and their corresponding
       expressions to use in the building of an assertion message.
       This is used by .pop_format_context() to build a message.

    :stack: A stack of the explanation_specifiers dicts maintained by
       .push_format_context() and .pop_format_context() which allows
       to build another %-formatted string while already building one.

    :variables_overwrite: A dict filled with references to variables
       that change value within an assert. This happens when a variable is
       reassigned with the walrus operator

    This state, except the variables_overwrite,  is reset on every new assert
    statement visited and used by the other visitors.
    N)rÄ   r!   rµ   r"   c                sB   t ƒ  ¡  || _|| _|d k	r,| d¡| _nd| _|| _i | _d S )NÚenable_assertion_pass_hookF)Úsuperr1   rÄ   r!   r#   r  rµ   Úvariables_overwrite)r.   rÄ   r!   rµ   )Ú	__class__r/   r0   r1     s    

zAssertionRewriter.__init__)r}   r"   c                s,  |j s
dS t|ddƒ}|dk}|dk	r4|  |¡r4dS d}d}x†|j D ]|}|r”t|tjƒr”t|jtƒr”tj	dkrx|jj}n|jj
}|  |¡rŽdS d}n$t|tjƒr¶|jdkr¶|jdkr¶nP |d7 }qDW t|tjƒrä|jrä|jd j‰ n|j‰ tj	dkrtjd	d
ˆ ddtjddˆ ddg}nt d	d
¡t dd¡g}‡ fdd„|D ƒ}||j ||…< |g}xÌ|r&| ¡ }	x¶t |	¡D ]¨\}
}t|tƒrög }xXt|ƒD ]L\}}t|tjƒrÀ| |  |¡¡ n"| |¡ t|tjƒr˜| |¡ q˜W t|	|
|ƒ n&t|tjƒrvt|tjƒsv| |¡ qvW q\W dS )z5Find all assert statements in *mod* and rewrite them.NÚ	docstringr   )r   r   FÚ
__future__rÎ   )r   r‰   Úbuiltinsz@py_builtins)ré   Ú
col_offsetz_pytest.assertion.rewritez
@pytest_arc                s   g | ]}t j|gˆ d d‘qS )r   )ré   r  )r±   ÚImport)Ú.0Úalias)ré   r/   r0   ú
<listcomp>Ú  s    z)AssertionRewriter.run.<locals>.<listcomp>)ÚbodyÚgetattrrv   r>   r±   ÚExprÚvalueÚastStrrP   rŽ   ÚsÚ
ImportFromÚlevelrK   ÚFunctionDefZdecorator_listré   r!  ÚpopÚiter_fieldsÚlistÚ	enumerateÚAssertÚextendÚvisitrÑ   ÚASTÚsetattrÚexpr)r.   r}   ÚdocZexpect_docstringÚposÚitemÚaliasesZimportsZnodesrú   r3   ÚfieldÚnewrä   rý   r/   )ré   r0   rÅ   Ÿ  sl    






zAssertionRewriter.run)r  r"   c             C   s   d| kS )NZPYTEST_DONT_REWRITEr/   )r  r/   r/   r0   rv   ö  s    z%AssertionRewriter.is_rewrite_disabled)r"   c             C   s"   dt t| jƒƒ }| j |¡ |S )zGet a new variable.z
@py_assert)rb   ÚnextÚvariable_counterÚ	variablesrÑ   )r.   r3   r/   r/   r0   Úvariableú  s    zAssertionRewriter.variable)r5  r"   c          	   C   s:   |   ¡ }| j t t |t ¡ ¡g|¡¡ t |t ¡ ¡S )zGive *expr* a name.)r?  Ú
statementsrÑ   r±   ÚAssignÚNameÚStoreÚLoad)r.   r5  r3   r/   r/   r0   Úassign  s    "zAssertionRewriter.assignc             C   s   |   d|¡S )z Call saferepr on the expression.rË   )Úhelper)r.   r5  r/   r/   r0   Údisplay  s    zAssertionRewriter.display)r3   Úargsr"   c             G   s4   t  dt  ¡ ¡}t  ||t  ¡ ¡}t  |t|ƒg ¡S )zCall a helper in this module.z
@pytest_ar)r±   rB  rD  Ú	AttributeÚCallr.  )r.   r3   rH  Zpy_nameÚattrr/   r/   r0   rF    s    zAssertionRewriter.helper)r3   r"   c             C   s"   t  dt  ¡ ¡}t  ||t  ¡ ¡S )z!Return the builtin called *name*.z@py_builtins)r±   rB  rD  rI  )r.   r3   Úbuiltin_namer/   r/   r0   Úbuiltin  s    zAssertionRewriter.builtinc             C   s(   dt t| jƒƒ }|| j|< d| d S )aJ  Return a new named %-formatting placeholder for expr.

        This creates a %-formatting placeholder for expr in the
        current formatting context, e.g. ``%(py0)s``.  The placeholder
        and expr are placed in the current format context so that it
        can be used on the next call to .pop_format_context().
        Úpyz%(z)s)rb   r<  r=  Úexplanation_specifiers)r.   r5  Ú	specifierr/   r/   r0   Úexplanation_param  s    
z#AssertionRewriter.explanation_paramc             C   s   i | _ | j | j ¡ dS )aŒ  Create a new formatting context.

        The format context is used for when an explanation wants to
        have a variable value formatted in the assertion message.  In
        this case the value required can be added using
        .explanation_param().  Finally .pop_format_context() is used
        to format a string of %-formatted values as added by
        .explanation_param().
        N)rO  ÚstackrÑ   )r.   r/   r/   r0   Úpush_format_context"  s    
z%AssertionRewriter.push_format_context)Ú	expl_exprr"   c          	   C   sª   | j  ¡ }| j r| j d | _dd„ | ¡ D ƒ}t |t| ¡ ƒ¡}t |t 	¡ |¡}dt
t| jƒƒ }| jrx| j |¡ | j t t |t ¡ ¡g|¡¡ t |t ¡ ¡S )aZ  Format the %-formatted string with current format context.

        The expl_expr should be an str ast.expr instance constructed from
        the %-placeholders created by .explanation_param().  This will
        add the required code to format said string to .expl_stmts and
        return the ast.Name instance of the formatted string.
        ra   c             S   s   g | ]}t |ƒ‘qS r/   )r'  )r   Úkeyr/   r/   r0   r"  :  s    z8AssertionRewriter.pop_format_context.<locals>.<listcomp>z
@py_format)rR  r,  rO  Úkeysr±   r   r.  ÚvaluesÚBinOpÚModrb   r<  r=  r  Úformat_variablesrÑ   Ú
expl_stmtsrA  rB  rC  rD  )r.   rT  ÚcurrentrV  Zformat_dictÚformr3   r/   r/   r0   Úpop_format_context/  s    
"z$AssertionRewriter.pop_format_context)rú   r"   c             C   s.   t |tjƒst‚|  |¡}||  |  |¡¡fS )z1Handle expressions we don't have custom code for.)r>   r±   r5  rN   rE  rQ  rG  )r.   rú   rå   r/   r/   r0   Úgeneric_visitC  s    
zAssertionRewriter.generic_visit)Úassert_r"   c             C   s.  t |jtjƒrZt|jjƒdkrZddlm} ddl}| j	dk	s@t
‚|j|dƒd| j	|jd g | _g | _t ¡ | _| jr|g | _g | _g | _|  ¡  |  |j¡\}}t t ¡ |¡}| jr|  t|ƒ¡}|jrà|  d|j¡}d}	ntd	ƒ}d
}	t t|	ƒt ¡ |¡}
t |t ¡ |
¡}t  dt !¡ ¡}|  d|¡}t "||gg ¡}t #|d¡}g }| $| j¡ | %|¡ |  d|¡}t&| j'ƒ|j }t (|  dt)|jƒt|ƒ|¡¡}t *|  d¡| j|g g ¡}|g}t *|||¡}| j %|¡ | jrÂdd„ | jD ƒ}t +|t,dƒ¡}| j %|¡ n®| j}| j %t *||g ¡¡ |jrP|  d|j¡}d| }ntd	ƒ}d
| }t |t ¡ t|ƒ¡}|  |¡}|  d|¡}t  dt !¡ ¡}t "||gg ¡}t #|d¡}| %|¡ | jrödd„ | jD ƒ}t +|t,dƒ¡}| j %|¡ x0| jD ]&}xt-|ƒD ]}t .||¡ qW qþW | jS )a.  Return the AST statements to replace the ast.Assert instance.

        This rewrites the test of an assertion to provide
        intermediate values and replace it with an if statement which
        raises an assertion error with a detailed explanation in case
        the expression is false.
        rÎ   r   )r   Nz5assertion is always true, perhaps remove parentheses?)Úcategoryr­   ré   rÔ   z	
>assert rq   zassert rN   Ú_format_explanationrì   rí   c             S   s   g | ]}t  |t  ¡ ¡‘qS r/   )r±   rB  rC  )r   r3   r/   r/   r0   r"  ›  s    z2AssertionRewriter.visit_Assert.<locals>.<listcomp>c             S   s   g | ]}t  |t  ¡ ¡‘qS r/   )r±   rB  rC  )r   r3   r/   r/   r0   r"  µ  s    )/r>   Útestr±   r   r½   Úeltsr‚   r   ÚwarningsrÄ   rN   Úwarn_explicitré   r@  r>  Ú	itertoolsÚcountr=  r  rZ  rR  r[  rS  r2  ÚUnaryOpÚNotr^  r'  ÚmsgrF  rX  ÚAddrB  rD  rJ  ÚRaiser1  rÑ   r  rµ   r%  ÚastNumÚIfrA  ÚastNameConstantrü   Úcopy_location)r.   r`  r   re  Ztop_conditionrÜ   Znegationrk  Z	assertmsgZgluestrZerr_explanationÚerr_msgZerr_nameÚfmtÚexcZraise_Zstatements_failZfmt_passrê   Zhook_call_passZhook_impl_testZstatements_passZ	main_testr>  Zclear_formatr#  Útemplater|   Ústmtrú   r/   r/   r0   Úvisit_AssertI  sš    






zAssertionRewriter.visit_Assertc             C   s|   t  |  d¡g g ¡}|jj}t  t|ƒt  ¡ g|g¡}|  d|¡}t  	t  
¡ ||g¡}t  ||  |¡t|ƒ¡}||  |¡fS )NÚlocalsr×   )r±   rJ  rM  r5   ÚidÚComparer'  ÚInrF  ÚBoolOpÚOrÚIfExprG  rQ  )r.   r3   ÚlocsZ	target_idÚinlocsÚdoreprrc  r5  r/   r/   r0   Úvisit_NamedExpr¾  s    z!AssertionRewriter.visit_NamedExprc             C   sx   t  |  d¡g g ¡}t  t|jƒt  ¡ g|g¡}|  d|¡}t  t  	¡ ||g¡}t  
||  |¡t|jƒ¡}||  |¡fS )Nrx  r×   )r±   rJ  rM  rz  r'  ry  r{  rF  r|  r}  r~  rG  rQ  )r.   r3   r  r€  r  rc  r5  r/   r/   r0   Ú
visit_NameÊ  s    zAssertionRewriter.visit_Name)Úboolopr"   c          
   C   sü  |   ¡ }|  t g t ¡ ¡¡}t |dt ¡ ¡}tt|jtj	ƒƒ}| j
 }}| j}t|jƒd }	|  ¡  xNt|jƒD ]>\}
}|
r
g }| j t ||g ¡¡ || _t|tjƒr
t|jtƒr
|jjjdd„ |jd |
… D ƒkr
|   ¡ }|j| j|jjj< ||jj_|  ¡  |  |¡\}}| t t |t ¡ ¡g|¡¡ |  t|ƒ¡}t ||gg ¡}| j t |¡¡ |
|	k rv|}|r’t  t !¡ |¡}g }| j
 t ||g ¡¡ | | _
}qvW || _
|| _|  "d|t#|ƒ¡}|  |¡}t |t ¡ ¡|  $|¡fS )NrÑ   rÎ   c             S   s   g | ]}t |d ƒr|j‘qS )ry  )rÖ   ry  )r   Zast_exprr/   r/   r0   r"  ê  s   z2AssertionRewriter.visit_BoolOp.<locals>.<listcomp>rÝ   )%r?  rE  r±   r	   rD  rI  rœ   r>   Úopr}  r@  r[  r½   rW  rS  r/  rÑ   ro  rz  ÚleftÚ	namedExprr5   ry  r  r2  rA  rB  rC  r^  r'  rJ  r%  ri  rj  rF  rn  rQ  )r.   r„  Zres_varZ	expl_listZapprÙ   r#  ÚsaveZ	fail_saveZlevelsrä   ÚvZ
fail_innerZcondZpytest_temprå   ræ   Zexpl_formatÚcallÚinnerZexpl_templater/   r/   r0   Úvisit_BoolOpÔ  sN    

 
zAssertionRewriter.visit_BoolOp)Úunaryr"   c             C   s>   t |jj }|  |j¡\}}|  t |j|¡¡}|||f fS )N)Ú	UNARY_MAPr…  r  r2  ÚoperandrE  r±   ri  )r.   r  ÚpatternZoperand_resZoperand_explrå   r/   r/   r0   Úvisit_UnaryOp  s    zAssertionRewriter.visit_UnaryOp)Úbinopr"   c       	      C   sb   t |jj }|  |j¡\}}|  |j¡\}}d|› d|› d|› d}|  t ||j|¡¡}||fS )NrÚ   ú rm   )	Ú	BINOP_MAPr…  r  r2  r†  ÚrightrE  r±   rX  )	r.   r’  ÚsymbolZ	left_exprÚ	left_explZ
right_exprZ
right_explrÜ   rå   r/   r/   r0   Úvisit_BinOp  s    zAssertionRewriter.visit_BinOp)rŠ  r"   c             C   sT  |   |j¡\}}g }g }g }xT|jD ]J}t|tjƒrL|j| jkrL| j|j }|   |¡\}}	| |	¡ | |¡ q$W xˆ|j	D ]~}
t|
j
tjƒrª|
j
j| jkrª| j|
j
j |
_
|   |
j
¡\}}	| t |
j|¡¡ |
jrê| |
jd |	 ¡ qz| d|	 ¡ qzW d |d |¡¡}	t |||¡}|  |¡}|  |  |¡¡}|› d|› d|	› d}||fS )Nú=z**z{}({})z, z
{z = z
})r2  ÚfuncrH  r>   r±   rB  ry  r  rÑ   Úkeywordsr&  ÚkeywordÚargÚformatrÛ   rJ  rE  rQ  rG  )r.   rŠ  Únew_funcZ	func_explZ	arg_explsÚnew_argsZ
new_kwargsr  rå   ræ   rœ  Znew_callÚres_explZ
outer_explr/   r/   r0   Ú
visit_Call  s4    

zAssertionRewriter.visit_Call)Ústarredr"   c             C   s*   |   |j¡\}}t ||j¡}|d| fS )Nró   )r2  r&  r±   ÚStarredÚctx)r.   r£  rå   ræ   Znew_starredr/   r/   r0   Úvisit_Starred6  s    zAssertionRewriter.visit_Starred)rK  r"   c             C   sp   t |jtjƒs|  |¡S |  |j¡\}}|  t ||j	t ¡ ¡¡}|  
|  |¡¡}d}|||||j	f }||fS )Nz%s
{%s = %s.%s
})r>   r¥  r±   rD  r_  r2  r&  rE  rI  rK  rQ  rG  )r.   rK  r&  Z
value_explrå   r¡  rk   ræ   r/   r/   r0   Úvisit_Attribute<  s    
z!AssertionRewriter.visit_Attribute)Úcompr"   c                s`  ˆ   ¡  t|jtjƒr4|jjˆ jkr4ˆ j|jj |_t|jtƒrR|jˆ j|jjj< ˆ  	|j¡\}}t|jtj
tjfƒr‚d|› d}‡ fdd„tt|jƒƒD ƒ}dd„ |D ƒ}dd„ |D ƒ}ttt|jƒƒ|j|jƒ}g }g }	|g}
xü|D ]ô\}}}t|tƒr4t|tjƒr4|jj|jkr4ˆ  ¡ |j_|ˆ j|j< ˆ  	|¡\}}t|tj
tjfƒrbd|› d}|
 |¡ t|j }|	 t|ƒ¡ |› d|› d|› }| t|ƒ¡ t 
||g|g¡}ˆ j t || g|¡¡ || }}qèW ˆ  dt |	t ¡ ¡t |t ¡ ¡t |t ¡ ¡t |
t ¡ ¡¡}t|jƒd	krDt t ¡ |¡}n|d
 }|ˆ  ˆ  |¡¡fS )NrÚ   rm   c                s   g | ]}ˆ   ¡ ‘qS r/   )r?  )r   rä   )r.   r/   r0   r"  T  s    z3AssertionRewriter.visit_Compare.<locals>.<listcomp>c             S   s   g | ]}t  |t  ¡ ¡‘qS r/   )r±   rB  rD  )r   r‰  r/   r/   r0   r"  U  s    c             S   s   g | ]}t  |t  ¡ ¡‘qS r/   )r±   rB  rC  )r   r‰  r/   r/   r0   r"  V  s    r“  rè   rÎ   r   )rS  r>   r†  r±   rB  ry  r  r‡  r5   r2  rz  r|  rã   r½   rÞ   râ   Zcomparatorsr?  rÑ   r”  r  r'  r@  rA  rF  r   rD  ÚAndrQ  r^  )r.   r¨  Zleft_resr—  Zres_variablesZ
load_namesZstore_namesÚitrà   Zsymsrß   rä   r…  Znext_operandZnext_resZ	next_explÚsymræ   Zres_exprZ	expl_callrå   r/   )r.   r0   Úvisit_CompareF  sV    

zAssertionRewriter.visit_Compare)2rO   r’   r“   rw   r
   rb   r   r•   r1   r±   ÚModulerÅ   Ústaticmethodr˜   rv   r?  r5  rB  rE  rG  rF  rI  rM  rQ  rS  r^  r3  r   r_  r0  r	   rv  rw  r‡  r‚  rƒ  r|  rŒ  ri  r‘  rX  r˜  rJ  r¢  r¤  r¦  r§  rz  r¬  Ú__classcell__r/   r/   )r  r0   ru   W  s2   7Wu
3!
ru   )r[   r"   c          
   C   sv   yt j| dd W n^ tttfk
r,   dS  tk
r>   dS  tk
rp } z|jtjkr^dS ‚ W dd}~X Y nX dS )z~Attempt to create the given directory and sub-directories exist.

    Returns True if successful or if it already exists.
    T)Úexist_okFN)	rB   ÚmakedirsÚFileNotFoundErrorÚNotADirectoryErrorÚFileExistsErrorÚPermissionErrorr©   rª   ZEROFS)r[   r¬   r/   r/   r0   rS   €  s    rS   )Ú	file_pathr"   c             C   s:   t jdkr,t jr,tt jƒt| jdd… Ž  S | jd S dS )zKReturn the cache directory to write .pyc files for the given .py file path.)r   r   rÎ   ra   Ú__pycache__N)rP   rŽ   Zpycache_prefixr   rj   Úparent)r¶  r/   r/   r0   rR   –  s    rR   )NN)ƒrw   r±   rª   Ú	functoolsr™   r?   Úimportlib.machineryÚimportlib.utilr
  rg  r¡   rB   rŸ   rP   r	  r   Úpathlibr   r   Útypingr   r   r   r   r   r	   r
   r   r   r   r   r   Z_pytest._io.safereprr   r   Z_pytest._versionr   Z_pytest.assertionr   Z_pytest.assertion.utilr   rb  Z_pytest.configr   Z_pytest.mainr   Z_pytest.pathlibr   r   Z_pytest.stashr   r   rŽ   Z	NamedExprr‡  ÚConstantrp  r'  rn  r%  ÚNameConstantÚStrÚNumr9   ÚimplementationÚ	cache_tagZ
PYTEST_TAGZPYC_EXTrT   ÚabcÚMetaPathFinderÚLoaderr    r•   rÂ   rÁ   r¦   r˜   rW   rV   rb   rU   r­  r³   ÚobjectrË   rœ   rÉ   rÔ   r×   rÝ   rè   rì   rí   rj  ÚInvertÚUSubÚUAddrŽ  ÚBitOrÚBitXorÚBitAndÚLShiftÚRShiftrl  ÚSubÚMultÚDivÚFloorDivrY  ÚEqÚNotEqÚLtÚLtEÚGtÚGtEÚPowÚIsÚIsNotr{  ÚNotInÚMatMultr”  r3  rü   Ú	lru_cacher  ÚNodeVisitorru   rS   rR   r/   r/   r/   r0   Ú<module>   sä   
 n2 



6    -