B
    `b                 @   s  U 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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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/m0Z0 ddl1m2Z2 ddl3m4Z4 erddl5Z5dZ6d Z7d!Z8d"Z9d#Z:e6e7e8e9e:fZ;da<da=eed$  e>d%< e(dd&d'd(Z?dd)d*d+Z@ejAjBe,eed,  d-d.d/ZCejAjBeDd0d1d2ZEe&ejAjBeDd3d4d5ZFG d6d7 d7e ZGG d8d9 d9eHZIed: d)d;d<ZJdked$ eeD eKeDd:d>d?d@ZLG dAdB dBejMZNeeOeKf d)dCdDZPdEdF ZQdGdH ZRG dIdJ dJejSZTdKddLdMdNZUeVeDdOdPdQZWe	edR d)dSdTZXG dUdV dVejSZYeNe*dWdXdYZZed$ d)dZd[Z[d$d)d\d]Z\eKd)d^d_Z]eKd)d`daZ^eKd)dbdcZ_eOeKdddedfZ`ejadgdheeOef d)didjZbdS )lz4Discover and run doctests in modules and test files.    N)contextmanager)Any)Callable)Dict)	Generator)Iterable)List)Optional)Pattern)Sequence)Tuple)Type)TYPE_CHECKING)Union)outcomes)ExceptionInfo)ReprFileLocation)TerminalRepr)TerminalWriter)safe_getattr)Config)Parser)FixtureRequest)	Collector)OutcomeException)import_path)approx)PytestWarningnoneZcdiffZndiffudiffZonly_first_failurezdoctest.OutputCheckerCHECKER_CLASS)parserreturnc             C   s   | j ddddgd | j dddd	 | d
}|jdddddd |jdtjddtdd |jddg dddd |jdddddd |jdddd d!d d S )"Ndoctest_optionflagszoption flags for doctestsargsELLIPSIS)typedefaultdoctest_encodingzencoding used for doctest fileszutf-8)r'   collectz--doctest-modules
store_trueFzrun doctests in all .py modulesdoctestmodules)actionr'   helpdestz--doctest-reportr   z9choose another output format for diffs on doctest failuredoctestreport)r&   r'   r-   choicesr.   z--doctest-globappendpatz2doctests file matching pattern, default: test*.txtdoctestglob)r,   r'   metavarr-   r.   z--doctest-ignore-import-errorszignore doctest ImportErrorsdoctest_ignore_import_errorsz--doctest-continue-on-failurez<for a given doctest, continue to run after the first failuredoctest_continue_on_failure)ZaddiniZgetgroupZ	addoptionstrlowerDOCTEST_REPORT_CHOICES)r!   group r;   W/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/_pytest/doctest.pypytest_addoptionA   sP    

r=   )r"   c               C   s   d a d S )N)RUNNER_CLASSr;   r;   r;   r<   pytest_unconfigures   s    r?   )DoctestModuleDoctestTextfile)pathparentr"   c             C   sV   |j }| jdkr4|jjrRt| sRtj|| d}|S nt|| |rRtj|| d}|S d S )Nz.py)fspath)	configextoptionr+   _is_setup_pyr@   from_parent_is_doctestrA   )rB   rC   rE   modtxtr;   r;   r<   pytest_collect_filey   s    
rM   )rB   r"   c             C   s&   | j dkrdS |  }d|kp$d|kS )Nzsetup.pyFs
   setuptoolss	   distutils)basenameread_binary)rB   contentsr;   r;   r<   rH      s    
rH   )rE   rB   r"   c             C   sL   |j dkr|j|rdS | dp(dg}x|D ]}|j|dr0dS q0W dS )N)z.txtz.rstTr3   z	test*.txt)fnmatchF)rF   sessionZ
isinitpath	getoptioncheck)rE   rB   rC   globsglobr;   r;   r<   rJ      s    
rJ   c               @   s<   e Zd Zeeeee f  ddddZeddddZ	dS )ReprFailDoctestN)reprlocation_linesr"   c             C   s
   || _ d S )N)rX   )selfrX   r;   r;   r<   __init__   s    zReprFailDoctest.__init__)twr"   c             C   s:   x4| j D ]*\}}x|D ]}|| qW || qW d S )N)rX   line
toterminal)rY   r[   reprlocationlinesr\   r;   r;   r<   r]      s    
zReprFailDoctest.toterminal)
__name__
__module____qualname__r   r   r   r7   rZ   r   r]   r;   r;   r;   r<   rW      s   rW   c                   s(   e Zd Zed dd fddZ  ZS )MultipleDoctestFailureszdoctest.DocTestFailureN)failuresr"   c                s   t    || _d S )N)superrZ   rd   )rY   rd   )	__class__r;   r<   rZ      s    
z MultipleDoctestFailures.__init__)r`   ra   rb   r   rZ   __classcell__r;   r;   )rf   r<   rc      s   rc   zdoctest.DocTestRunnerc                 s"   dd l  G  fddd j} | S )Nr   c                   st   e Zd ZdZded ee eedd fddZd	d
edd fddZ	d	d
e
ee eejf dd fddZdS )z/_init_runner_class.<locals>.PytestDoctestRunnerzRunner to collect failures.

        Note that the out variable in this case is a list instead of a
        stdout-like object.
        Nr   Tzdoctest.OutputChecker)checkerverboseoptionflagscontinue_on_failurer"   c                s    j j| |||d || _d S )N)rh   ri   rj   )DebugRunnerrZ   rk   )rY   rh   ri   rj   rk   )doctestr;   r<   rZ      s    z8_init_runner_class.<locals>.PytestDoctestRunner.__init__zdoctest.DocTestzdoctest.Example)testexamplegotr"   c                s(     |||}| jr || n|d S )N)DocTestFailurerk   r1   )rY   outrn   ro   rp   failure)rm   r;   r<   report_failure   s    z>_init_runner_class.<locals>.PytestDoctestRunner.report_failure)rn   ro   exc_infor"   c                sX   t |d tr|d t |d tjr0td  |||}| jrP|| n|d S )N   zQuitting debugger)	
isinstancer   bdbZBdbQuitr   exitUnexpectedExceptionrk   r1   )rY   rr   rn   ro   ru   rs   )rm   r;   r<   report_unexpected_exception   s    
zK_init_runner_class.<locals>.PytestDoctestRunner.report_unexpected_exception)NNr   T)r`   ra   rb   __doc__r	   boolintrZ   r7   rt   r   r   BaseExceptiontypesTracebackTyper{   r;   )rm   r;   r<   PytestDoctestRunner   s      
r   )rm   rl   )r   r;   )rm   r<   _init_runner_class   s    -r   T)rh   ri   rj   rk   r"   c             C   s   t d krt a t | |||dS )N)rh   ri   rj   rk   )r>   r   )rh   ri   rj   rk   r;   r;   r<   _get_runner   s    r   c                   s   e Zd Zdeded ed dd fddZededdd fd	d
ZddddZddddZ	ddddZ
ee eeef d fddZdd Z  ZS )DoctestItemNz%Union[DoctestTextfile, DoctestModule]zdoctest.DocTestRunnerzdoctest.DocTest)namerC   runnerdtestr"   c                s*   t  || || _|| _d | _d | _d S )N)re   rZ   r   r   objfixture_request)rY   r   rC   r   r   )rf   r;   r<   rZ      s
    zDoctestItem.__init__)rC   r   r   r   c               s   t  j||||dS )zThe public named constructor.)r   rC   r   r   )re   rI   )clsrC   r   r   r   )rf   r;   r<   rI      s    zDoctestItem.from_parent)r"   c             C   sZ   | j d k	rVt| | _t| jjd}x$| jd D ]\}}|||< q4W | j j| d S )N)Z
getfixturedoctest_namespace)r   _setup_fixturesr   dictZgetfixturevalueitemsrU   update)rY   rU   r   valuer;   r;   r<   setup  s    

zDoctestItem.setupc             C   sT   | j d k	st| jd k	stt| j  |   g }| jj| j |d |rPt|d S )N)rr   )r   AssertionErrorr   _check_all_skipped$_disable_output_capturing_for_darwinrunrc   )rY   rd   r;   r;   r<   runtest  s    
zDoctestItem.runtestc             C   sV   t  dkrdS | jjd}|rR|jdd | \}}tj	| tj
	| dS )zFDisable output capturing. Otherwise, stdout is lost to doctest (#985).DarwinNZcapturemanagerT)Zin_)platformsystemrE   pluginmanagerZ	getpluginZsuspend_global_captureZread_global_capturesysstdoutwritestderr)rY   Zcapmanrr   errr;   r;   r<   r   #  s    z0DoctestItem._disable_output_capturing_for_darwin)excinfor"   c                s  dd l }d }t|j|j|jfr*|jg}nt|jtr>|jj}|d k	rg }x|D ]x}|j}|j  j	} j
d kr|d }n j
|j
 d }t|j}	t|||	}
t }t| jd}|d k	r$|jjd k	st|jjd} j
d k	st fddt|D }|t|j
d d|j
d  }n:dg}d	}x.|j D ] }|d
| d|  d}q:W t||jr||||j|d7 }n8t|j}|dt|j g7 }|dd tj |j D 7 }||
|f qTW t!|S t" #|S d S )Nr   rv   r/   Fc                s&   g | ]\}}d | j  d |f qS )z%03d %srv   )lineno).0ix)rn   r;   r<   
<listcomp>U  s   z,DoctestItem.repr_failure.<locals>.<listcomp>	   z?EXAMPLE LOCATION UNKNOWN, not showing all tests of that examplez>>>z???  z...
zUNEXPECTED EXCEPTION: %sc             S   s   g | ]}| d qS )r   )strip)r   r   r;   r;   r<   r   j  s   )$rm   rw   r   rq   rz   rc   rd   ro   rn   filenamer   r&   r`   r   _get_checker_get_report_choicerE   rS   Z	docstringr   
splitlines	enumeratemaxsourcer1   Zoutput_differencerp   splitr   ru   repr	tracebackformat_exceptionrW   re   repr_failure)rY   r   rm   rd   rX   rs   ro   r   r   messager^   rh   Zreport_choicer_   indentr\   Zinner_excinfo)rf   )rn   r<   r   /  sZ    





 


zDoctestItem.repr_failurec             C   s$   | j d k	st| j| j jd| j fS )Nz[doctest] %s)r   r   rD   r   r   )rY   r;   r;   r<   
reportinfor  s    zDoctestItem.reportinfo)NN)r`   ra   rb   r7   r	   rZ   classmethodrI   r   r   r   r   r   r   r   r   r   rg   r;   r;   )rf   r<   r      s    
Ar   c              C   s4   dd l } t| j| j| j| j| j| jt t	 t
 d	S )Nr   )	DONT_ACCEPT_TRUE_FOR_1DONT_ACCEPT_BLANKLINENORMALIZE_WHITESPACEr%   IGNORE_EXCEPTION_DETAILCOMPARISON_FLAGSALLOW_UNICODEALLOW_BYTESNUMBER)rm   r   r   r   r   r%   r   r   _get_allow_unicode_flag_get_allow_bytes_flag_get_number_flag)rm   r;   r;   r<   _get_flag_lookupw  s    r   c             C   s4   | j d}t }d}x|D ]}||| O }qW |S )Nr#   r   )rE   getinir   )rC   Zoptionflags_strZflag_lookup_tableZflag_accflagr;   r;   r<   get_optionflags  s    
r   c             C   s    |  d}|r|  drd}|S )Nr6   ZusepdbF)getvalue)rE   rk   r;   r;   r<   _get_continue_on_failure  s
    

r   c               @   s"   e Zd ZdZee dddZdS )rA   N)r"   c             c   s   dd l }| jd}| j|}t| j}| jj}ddi}t| }td|t	 t
| jd}| }	|	||||d}
|
jrtj| |
j||
dV  d S )Nr   r(   r`   __main__F)ri   rj   rh   rk   )r   r   r   )rm   rE   r   rD   	read_textr7   rN   r   r   r   r   ZDocTestParserZget_doctestexamplesr   rI   r   )rY   rm   encodingtextr   r   rU   rj   r   r!   rn   r;   r;   r<   r)     s"    
zDoctestTextfile.collect)r`   ra   rb   r   r   r   r)   r;   r;   r;   r<   rA     s   rA   zdoctest.DocTest)rn   r"   c                s2   ddl  t fdd| jD }|r.td dS )zVRaise pytest.skip() if all examples in the given DocTest have the SKIP
    option set.r   Nc             3   s   | ]}|j  jd V  qdS )FN)optionsgetZSKIP)r   r   )rm   r;   r<   	<genexpr>  s    z%_check_all_skipped.<locals>.<genexpr>z!all tests skipped by +SKIP option)rm   allr   pytestskip)rn   Zall_skippedr;   )rm   r<   r     s    r   )r   r"   c             C   s   t | dddk	S )zmReturn if an object is possibly a mock object by checking the
    existence of a highly improbable attribute.Z1pytest_mock_example_attribute_that_shouldnt_existN)r   )r   r;   r;   r<   
_is_mocked  s    
r   )NNNc              #   sV   t j ddtdtf tttgtf  td fdd} | t _z
dV  W d t _X dS )zContext manager which replaces ``inspect.unwrap`` with a version
    that's aware of mock objects and doesn't recurse into them.N)stop.)funcr   r"   c         
      st   y6|d ks|t krt dS |  fdddS  tk
rn } ztd|f t  W d d }~X Y nX d S )N)r   c                s   t | p S )N)r   )r   )_stopr   r;   r<   <lambda>      zF_patch_unwrap_mock_aware.<locals>._mock_aware_unwrap.<locals>.<lambda>zGot %r when unwrapping %r.  This is usually caused by a violation of Python's object protocol; see e.g. https://github.com/pytest-dev/pytest/issues/5080)r   	Exceptionwarningswarnr   )r   r   e)real_unwrap)r   r   r<   _mock_aware_unwrap  s    z4_patch_unwrap_mock_aware.<locals>._mock_aware_unwrap)inspectunwrapr   r   r	   )r   r;   )r   r<   _patch_unwrap_mock_aware  s     
r   c               @   s   e Zd Zee dddZdS )r@   )r"   c             #   s   dd l  G  fddd j}| jjdkrF| jj| j| jd}nDyt| j}W n4 t	k
r   | j
drtd| j  n Y nX | }t| }td|t t| jd	}x2|||jD ] }|jrtj| |j||d
V  qW d S )Nr   c                   s.   e Zd ZdZ fddZdd fddZdS )z5DoctestModule.collect.<locals>.MockAwareDocTestFinderzA hackish doctest finder that overrides stdlib internals to fix a stdlib bug.

            https://github.com/pytest-dev/pytest/issues/3456
            https://bugs.python.org/issue25532
            c                s&   t |trt|d|} j| ||S )zDoctest code does not take into account `@property`, this
                is a hackish way to fix it.

                https://bugs.python.org/issue17446
                fget)rw   propertygetattrDocTestFinder_find_lineno)rY   r   source_lines)rm   r;   r<   r     s    
zBDoctestModule.collect.<locals>.MockAwareDocTestFinder._find_linenoN)r"   c                s<   t |rd S t    j| ||||||| W d Q R X d S )N)r   r   r   _find)rY   testsr   r   moduler   rU   seen)rm   r;   r<   r     s
    z;DoctestModule.collect.<locals>.MockAwareDocTestFinder._find)r`   ra   rb   r|   r   r   r;   )rm   r;   r<   MockAwareDocTestFinder  s   r   zconftest.pyZ
importmoder5   zunable to import module %rF)ri   rj   rh   rk   )r   r   r   )rm   r   rD   rN   rE   r   Z_importconftestrS   r   ImportErrorr   r   r   r   r   r   r   findr`   r   r   rI   r   )rY   r   r   finderrj   r   rn   r;   )rm   r<   r)     s,     zDoctestModule.collectN)r`   ra   rb   r   r   r)   r;   r;   r;   r<   r@     s   r@   )doctest_itemr"   c             C   sH   dddd}i | _ | jj}|j| |ddd| _t| dd}|  |S )	zEUsed by DoctestTextfile and DoctestItem to setup fixture information.N)r"   c               S   s   d S )Nr;   r;   r;   r;   r<   r   .  s    z_setup_fixtures.<locals>.funcF)noder   r   funcargsT)Z	_ispytest)r   rR   Z_fixturemanagerZgetfixtureinfoZ_fixtureinfor   Z_fillfixtures)r   r   Zfmr   r;   r;   r<   r   +  s    r   c                 s,   dd l  dd lG  fddd j} | S )Nr   c                   sb   e Zd ZdjZdjZdjZe	e	e
ed fddZe	e	e	ddd	Zd
S )z2_init_checker_class.<locals>.LiteralsOutputCheckerz(\W|^)[uU]([rR]?[\'\"])z(\W|^)[bB]([rR]?[\'\"])a  
            (?P<number>
              (?P<mantissa>
                (?P<integer1> [+-]?\d*)\.(?P<fraction>\d+)
                |
                (?P<integer2> [+-]?\d+)\.
              )
              (?:
                [Ee]
                (?P<exponent1> [+-]?\d+)
              )?
              |
              (?P<integer3> [+-]?\d+)
              (?:
                [Ee]
                (?P<exponent2> [+-]?\d+)
              )
            )
            )wantrp   rj   r"   c                s    j | |||rdS |t @ }|t @ }|t @ }|sD|sD|sDdS tt ttdfdd}|rz|| j|}|| j|}|r|| j|}|| j|}|r| 	||} j | |||S )NTF)regexrL   r"   c                s     | d|S )Nz\1\2)sub)r   rL   )rer;   r<   remove_prefixesh  s    zX_init_checker_class.<locals>.LiteralsOutputChecker.check_output.<locals>.remove_prefixes)
OutputCheckercheck_outputr   r   r   r
   r7   _unicode_literal_re_bytes_literal_re_remove_unwanted_precision)rY   r   rp   rj   Zallow_unicodeZallow_bytesZallow_numberr   )rm   r   r;   r<   r   ]  s"    


z?_init_checker_class.<locals>.LiteralsOutputChecker.check_output)r   rp   r"   c             S   s  t | j|}t | j|}t|t|kr4|S d}xt||D ]\}}|d}|d}	|	d krr|d}	|d krd}
nt|}
|	d k	r|
t|	8 }
t| tt| d|
  dkrD|d |	 |  |  ||
 | d   }||
 |	  |
 |	   7 }qDW |S )Nr   fractionZ	exponent1Z	exponent2
   )abs)list
_number_refinditerlenzipr:   r~   floatr   startend)rY   r   rp   Zwantsgotsoffsetwgr  exponentZ	precisionr;   r;   r<   r   x  s&    


&0(zM_init_checker_class.<locals>.LiteralsOutputChecker._remove_unwanted_precisionN)r`   ra   rb   compileUNICODEr   r   VERBOSEr  r7   r~   r}   r   r   r;   )rm   r   r;   r<   LiteralsOutputChecker?  s   r  )rm   r   r   )r  r;   )rm   r   r<   _init_checker_class;  s    Tr  c               C   s   t dkrt a t  S )a  Return a doctest.OutputChecker subclass that supports some
    additional options:

    * ALLOW_UNICODE and ALLOW_BYTES options to ignore u'' and b''
      prefixes (respectively) in string literals. Useful when the same
      doctest should run in Python 2 and Python 3.

    * NUMBER to ignore floating-point differences smaller than the
      precision of the literal number in the doctest.

    An inner class is used to avoid importing "doctest" at the module
    level.
    N)r    r  r;   r;   r;   r<   r     s    r   c              C   s   ddl } | dS )z+Register and return the ALLOW_UNICODE flag.r   Nr   )rm   register_optionflag)rm   r;   r;   r<   r     s    r   c              C   s   ddl } | dS )z)Register and return the ALLOW_BYTES flag.r   Nr   )rm   r  )rm   r;   r;   r<   r     s    r   c              C   s   ddl } | dS )z$Register and return the NUMBER flag.r   Nr   )rm   r  )rm   r;   r;   r<   r     s    r   )keyr"   c          
   C   s,   ddl }t|jt|jt|jt|jt	di|  S )zReturn the actual `doctest` module flag value.

    We want to do it as late as possible to avoid importing `doctest` and all
    its dependencies when parsing options, as it adds overhead and breaks tests.
    r   N)
rm   DOCTEST_REPORT_CHOICE_UDIFFZREPORT_UDIFFDOCTEST_REPORT_CHOICE_CDIFFZREPORT_CDIFFDOCTEST_REPORT_CHOICE_NDIFFZREPORT_NDIFF(DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILUREZREPORT_ONLY_FIRST_FAILUREDOCTEST_REPORT_CHOICE_NONE)r  rm   r;   r;   r<   r     s    r   rR   )Zscopec               C   s   t  S )zaFixture that returns a :py:class:`dict` that will be injected into the
    namespace of doctests.)r   r;   r;   r;   r<   r     s    r   )NNr   T)cr|   rx   r   r   r   r   r   r   
contextlibr   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   Zpy.pathpyr   Z_pytestr   Z_pytest._code.coder   r   r   Z_pytest._ior   Z_pytest.compatr   Z_pytest.configr   Z_pytest.config.argparsingr   Z_pytest.fixturesr   Z_pytest.nodesr   Z_pytest.outcomesr   Z_pytest.pathlibr   Z_pytest.python_apir   Z_pytest.warning_typesr   rm   r  r  r  r  r  r9   r>   r    __annotations__r=   r?   rB   localrM   r}   rH   rJ   rW   r   rc   r   r~   r   ZItemr   r7   r   r   r   ModulerA   r   objectr   r   r@   r   r  r   r   r   r   r   Zfixturer   r;   r;   r;   r<   <module>   s   2
4    	

	A[
