a
    ø£Bei.  ã                   @   sœ   d Z ddlZddlZddlZddlmZ e e¡ZG dd„ dej	ƒZ	G dd„ dej
ƒZG dd	„ d	ejƒZG d
d„ dejƒZe d¡ZG dd„ dejeƒZdS )aÜ  Nose Plugin that supports IPython doctests.

Limitations:

- When generating examples for use as doctests, make sure that you have
  pretty-printing OFF.  This can be done either by setting the
  ``PlainTextFormatter.pprint`` option in your configuration file to  False, or
  by interactively disabling it with  %Pprint.  This is required so that IPython
  output matches that of normal Python, which is used by doctest for internal
  execution.

- Do not rely on specific prompt numbers for results (such as using
  '_34==True', for example).  For IPython tests run via an external process the
  prompt numbers may be different, and IPython tests run as normal python code
  won't even have these special _NN variables set at all.
é    N)Úmodified_envc                       s   e Zd Z‡ fdd„Z‡  ZS )ÚDocTestFinderc                    sH   t ƒ  |||||¡}tt|ddƒƒrD|d urD|jD ]}d|jtj< q2|S )NZ__skip_doctest__FT)ÚsuperÚ	_get_testÚboolÚgetattrZexamplesÚoptionsÚdoctestÚSKIP)ÚselfÚobjÚnameÚmoduleÚglobsÚsource_linesÚtestZexample©Ú	__class__© úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/testing/plugin/ipdoctest.pyr   )   s
    
zDocTestFinder._get_test)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r   r   r   (   s   r   c                   @   s"   e Zd ZdZe d¡Zdd„ ZdS )ÚIPDoctestOutputCheckerzÈSecond-chance checker with support for random tests.

    If the default comparison doesn't pass, this checker looks in the expected
    output string for flags that tell us to ignore the output.
    z#\s*random\s+c                 C   s*   t j | |||¡}|s&| j |¡r&dS |S )z½Check output, accepting special markers embedded in the output.

        If the output didn't pass the default validation but the special string
        '#random' is included, we accept it.T)r	   ÚOutputCheckerÚcheck_outputÚ	random_reÚsearch)r   ÚwantÚgotZoptionflagsÚretr   r   r   r   <   s    ÿz#IPDoctestOutputChecker.check_outputN)r   r   r   Ú__doc__ÚreÚcompiler   r   r   r   r   r   r   3   s   
r   c                   @   s   e Zd ZdS )Ú	IPExampleN)r   r   r   r   r   r   r   r%   O   ó    r%   c                   @   s’   e Zd ZdZdZdZdZdZdZe	 
eeeeef e	je	jB ¡Ze	 
eeeeef e	je	jB ¡Ze	 
d¡Zdd	„ Zddd„Zddd„Zdd„ ZdS )ÚIPDocTestParserzË
    A class used to parse strings containing doctest examples.

    Note: This is a version modified to properly recognize IPython input and
    convert any IPython examples into valid Python ones.
    z>>>z\.\.\.zIn\ \[\d+\]:z\ \ \ \.\.\.+:a]  
        # Source consists of a PS1 line followed by zero or more PS2 lines.
        (?P<source>
            (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*)    # PS1 line
            (?:\n           [ ]*  (?P<ps2> %s) .*)*)  # PS2 lines
        \n? # a newline
        # Want consists of any non-blank lines that do not start with PS1.
        (?P<want> (?:(?![ ]*$)    # Not a blank line
                     (?![ ]*%s)   # Not a line starting with PS1
                     (?![ ]*%s)   # Not a line starting with PS2
                     .*$\n?       # But any other line
                  )*)
                  z#\s*all-random\s+c                 C   s.   t j |¡}t| ¡ ƒdkr&t  |¡S |S dS )z/Convert input IPython source into valid Python.é   N)Ú_ipZinput_transformer_managerZtransform_cellÚlenÚ
splitlinesZ	prefilter)r   ÚsourceÚblockr   r   r   Úip2py   s    
zIPDocTestParser.ip2pyú<string>c                    sT  |  ¡ }|  |¡‰ ˆ dkr8d ‡ fdd„| d¡D ƒ¡}g }d\}}| j |¡rVd}nd}d}t| j |¡ƒ}|rzt	j
}	nt| j |¡ƒ}t}	d	}|D ]¦}
| |||
 ¡ … ¡ || d||
 ¡ ¡7 }|  |
|||¡\}}}}||7 }|  |¡�s| |	||||ˆ t|
 d
¡ƒ |d�¡ || d|
 ¡ |
 ¡ ¡7 }|
 ¡ }q–| ||d… ¡ |S )a=  
        Divide the given string into examples and intervening text,
        and return them as a list of alternating Examples and strings.
        Line numbers for the Examples are 0-based.  The optional
        argument `name` is a name identifying this string, and is only
        used for error messages.
        r   Ú
c                    s   g | ]}|ˆ d … ‘qS ©Nr   )Ú.0Úl©Z
min_indentr   r   Ú
<listcomp>–   r&   z)IPDocTestParser.parse.<locals>.<listcomp>)r   r   z	
# randomÚ FTÚindent)Úlinenor7   r   N)Ú
expandtabsZ_min_indentÚjoinÚsplitÚ_RANDOM_TESTr   ÚlistÚ_EXAMPLE_RE_PYÚfinditerr	   ÚExampleÚ_EXAMPLE_RE_IPr%   ÚappendÚstartÚcountÚ_parse_exampleZ_IS_BLANK_OR_COMMENTr*   ÚgroupÚend)r   Ústringr   ÚoutputZcharnor8   Zrandom_markerr.   Ztermsr@   Úmr,   r   r   Úexc_msgr   r4   r   Úparse‡   s@    
ÿ
ý
zIPDocTestParser.parseFc                    sT  t | d¡ƒ‰ | d¡ d¡}| d¡}| d¡}t |ƒ‰|  |ˆ ||ˆ¡ |rp|  |dd… dˆ  | ||¡ d ‡ ‡fd	d
„|D ƒ¡}|r˜|  |¡}| d¡}	|	 d¡}
t |
ƒdkrÎt d|
d ¡rÎ|
d= |  |
dˆ  ||t |ƒ ¡ t 	dd|
d ¡|
d< d ‡ fdd
„|
D ƒ¡}	| j
 |	¡}|�r6| d¡}nd}|  |||¡}|||	|fS )a7  
        Given a regular expression match from `_EXAMPLE_RE` (`m`),
        return a pair `(source, want)`, where `source` is the matched
        example's source code (with prompts and indentation stripped);
        and `want` is the example's expected output (with indentation
        stripped).

        `name` is the string's name, and `lineno` is the line number
        where the example starts; both are used for error messages.

        Optional:
        `ip2py`: if true, filter the input via IPython to convert the syntax
        into valid python.
        r7   r,   r0   Úps1Úps2r(   Nú c                    s    g | ]}|ˆ ˆ d  d… ‘qS )r(   Nr   )r2   Úsl©r7   Úps1_lenr   r   r5   ë   r&   z2IPDocTestParser._parse_example.<locals>.<listcomp>r   z *$éÿÿÿÿzOut\[\d+\]: \s*?\n?r6   r   c                    s   g | ]}|ˆ d … ‘qS r1   r   )r2   Zwl)r7   r   r   r5   þ   r&   Úmsg)r*   rF   r;   Ú_check_prompt_blankZ_check_prefixr:   r.   r#   ÚmatchÚsubZ_EXCEPTION_REZ_find_options)r   rJ   r   r8   r.   r   rM   rN   r,   r   Z
want_linesrK   r   r   rQ   r   rE   Ë   s4    

 



ÿzIPDocTestParser._parse_examplec           
      C   sb   || }|d }t |ƒD ]D\}}	t|	ƒ|kr|	| dkrtd|| d ||	||… |	f ƒ‚qdS )a¤  
        Given the lines of a source string (including prompts and
        leading indentation), check to make sure that every prompt is
        followed by a space character.  If any line is not followed by
        a space character, then raise ValueError.

        Note: IPython-modified version which takes the input prompt length as a
        parameter, so that prompts of variable length can be dealt with.
        r(   rO   z8line %r of the docstring for %s lacks blank after %s: %rN)Ú	enumerater*   Ú
ValueError)
r   Úlinesr7   r   r8   rR   Z	space_idxÚmin_lenÚiÚliner   r   r   rU     s    
ÿþz#IPDocTestParser._check_prompt_blankN)r/   )F)r   r   r   r"   Z_PS1_PYZ_PS2_PYZ_PS1_IPZ_PS2_IPZ_RE_TPLr#   r$   Ú	MULTILINEÚVERBOSEr>   rA   r<   r.   rL   rE   rU   r   r   r   r   r'   R   s"   
ÿ
ÿ

D
Ar'   r
   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚIPDocTestRunnerzKTest runner that synchronizes the IPython namespace with test globals.
    NTc                    sH   t dddœƒ�& tt| ƒ ||||¡W  d   ƒ S 1 s:0    Y  d S )NÚ80Z24)ÚCOLUMNSÚLINES)r   r   r`   Úrun)r   r   ZcompileflagsÚoutZclear_globsr   r   r   rd   '  s    ÿzIPDocTestRunner.run)NNT)r   r   r   r"   rd   r   r   r   r   r   r`   #  s   r`   )r"   r	   Úloggingr#   Ztestpathr   Ú	getLoggerr   Úlogr   r   r   r@   r%   ZDocTestParserr'   Zregister_optionflagr
   ZDocTestRunnerÚobjectr`   r   r   r   r   Ú<module>   s   
 O
