B
    [uˆ\r  ã               @   sŒ   d Z ddlZddlZddlZddlZddlmZ e d¡Z	e d¡Z
e d¡Ze d¡Ze d¡Ze d	¡Ze d
¡ZG dd„ dejƒZdS )z¯
Implements a replacement for `doctest.OutputChecker` that handles certain
normalizations of Python expression output.  See the docstring on
`OutputChecker` for more details.
é    N)ÚzipÚFIXÚ	FLOAT_CMPÚIGNORE_OUTPUTÚIGNORE_OUTPUT_2ÚIGNORE_OUTPUT_3ÚALLOW_BYTESÚALLOW_UNICODEc               @   sv   e Zd ZdZejZe dej	¡Z
e dej	¡Ze dej	¡Ze dej	¡Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ ZdS )ÚOutputCheckera	  
    - Removes u'' prefixes on string literals
    - Ignores the 'L' suffix on long integers
    - In Numpy dtype strings, removes the leading pipe, i.e. '|S9' ->
      'S9'.  Numpy 1.7 no longer includes it in display.
    - Supports the FLOAT_CMP flag, which parses floating point values
      out of the output and compares their numerical values rather than their
      string representation.  This naturally supports complex numbers as well
      (simply by comparing their real and imaginary parts separately).
    z(\W|^)[uU]([rR]?[\'\"])z(([\'\"])[|<>]([biufcSaUV][0-9]+)([\'\"])z([\'\"])([iu])[48]([\'\"])z	([0-9]+)Lc             C   s‚   d}d  |¡}|d }d}|d }d  ||¡}d  |||¡}t d  ||¡¡| _d  ||¡}d  |||¡}t d  ||¡¡| _d S )	Nz(?:e[+-]?\d+)z>\s*([+-]?\d+\.\d*{0}?|[+-]?\.\d+{0}?|[+-]?\d+{0}|nan|[+-]?inf)z(\.{3})?z\s|[*+-,<=(\[]z|[>j)\]]z^{}(?={}|$)z(?<={}){}(?={}|$)z({}|{}))ÚformatÚreÚcompileÚnum_got_rgxÚnum_want_rgx)ÚselfZexpZ
got_floatsZwant_floatsZ	front_sepZback_sepZfbegZfmidend© r   ú@lib/python3.7/site-packages/pytest_doctestplus/output_checker.pyÚ__init__8   s    zOutputChecker.__init__c             C   sˆ   t  | jd|¡}t  | jd|¡}t  | jd|¡}t  | jd|¡}t  | jd|¡}t  | jd|¡}t  | jd|¡}t  | jd|¡}||fS )Nz\1\2z\1\2\3z\1)r   ÚsubÚ_str_literal_reÚ_byteorder_reÚ_fix_32bit_reÚ_long_int_re)r   ÚwantÚgotr   r   r   Údo_fixesR   s    zOutputChecker.do_fixesc       	         sr  ||krdS | j  |¡}dd„ |D ƒ}| j |¡}dd„ |D ƒ}t|ƒt|ƒkrTdS t|ƒdkrÖg ‰ xZt||ƒD ]L\}}d|krŽˆ  |¡ qpn
ˆ  |¡ tjt|ƒt|ƒ| j	| j
ddspdS qpW | j  ‡ fd	d
„|¡}|tj@ st d t tj¡¡d|¡}t dd|¡}||krdS |tj@ rPd | ¡ ¡}d | ¡ ¡}||krPdS |tj@ rnt ||¡rndS dS )aQ  
        Alternative to the built-in check_output that also handles parsing
        float values and comparing their numeric values rather than their
        string representations.

        This requires rewriting enough of the basic check_output that, when
        FLOAT_CMP is enabled, it totally takes over for check_output.
        Tc             S   s   g | ]}|  d ¡‘qS )é   )Úgroup)Ú.0Úmatchr   r   r   ú
<listcomp>r   s    z2OutputChecker.normalize_floats.<locals>.<listcomp>c             S   s   g | ]}|  d ¡‘qS )r   )r   )r   r   r   r   r   r    t   s    Fr   z...)ÚrtolÚatolZ	equal_nanc                s
   ˆ   d¡S )Nr   )Úpop)Úx)Únw_r   r   Ú<lambda>ˆ   s    z0OutputChecker.normalize_floats.<locals>.<lambda>z(?m)^{}\s*?$Ú z
(?m)^\s*?$ú )r   Úfinditerr   Úlenr   ÚappendÚnpZallcloseÚfloatr!   r"   r   ÚdoctestZDONT_ACCEPT_BLANKLINEr   r   ÚescapeZBLANKLINE_MARKERZNORMALIZE_WHITESPACEÚjoinÚsplitÚELLIPSISZ_ellipsis_match)	r   r   r   ÚflagsZmatchesZnumbers_gotZnumbers_wantZngZnwr   )r%   r   Únormalize_floats_   sD    



zOutputChecker.normalize_floatsc             C   sh   |t @ s$tjr|t@ s$tjs(|t@ r(dS |t@ r@|  ||¡\}}|t@ rV|  |||¡S | j	 
| |||¡S )NT)r   ÚsixZPY2r   r   r   r   r   r4   Ú_original_output_checkerÚcheck_output)r   r   r   r3   r   r   r   r7   ¨   s    zOutputChecker.check_outputc             C   s*   |t @ r|  ||¡\}}| j | |||¡S )N)r   r   r6   Úoutput_difference)r   r   r   r3   r   r   r   r8   ¸   s    zOutputChecker.output_differenceN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   r
   r6   r   r   ÚUNICODEr   r   r   r   r   r   r4   r7   r8   r   r   r   r   r
   !   s   




Ir
   )r<   r.   r   Znumpyr,   r5   Z	six.movesr   Zregister_optionflagr   r   r   r   r   r   r	   r
   r   r   r   r   Ú<module>   s   






