a
    !fh                     @   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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(m)Z) ddl*m+Z+ ddl*m,Z, ddl-m.Z. ddl-m/Z/ ddl0m1Z1 ddl0m2Z2 dd l3m4Z4 dd!l3m5Z5 dd"l6m7Z7 dd#l8m9Z9 dd$l:m;Z; erddl<Z<d%Z=d&Z>d'Z?d(Z@d)ZAe=e>e?e@eAfZBdaCdaDeed*  eEd+< e)dd,d-d.ZFdd/d0d1ZGee.eed2  d3d4d5ZHeeId6d7d8ZJe'ee.eId9d:d;ZKeeId6d<d=ZLG d>d? d?e!ZMG d@dA dAeNZOedB d/dCdDZPdsed* eeI eQeIdBdFdGdHZRG dIdJ dJe/ZSeeTeQf d/dKdLZUdMdN ZVdOdP ZWG dQdR dRe7ZXdSddTdUdVZYeZeIdWdXdYZ[eedZ d/d[d\Z\G d]d^ d^e7Z]eSe,d_d`daZ^ed* d/dbdcZ_d*d/dddeZ`eQd/dfdgZaeQd/dhdiZbeQd/djdkZceTeQdldmdnZde+dodpeeTef d/dqdrZedS )tz4Discover and run doctests in modules and test files.    N)contextmanager)Path)Any)Callable)Dict)	Generator)Iterable)List)Optional)Pattern)Sequence)Tuple)Type)TYPE_CHECKING)Union)outcomes)ExceptionInfo)ReprFileLocation)TerminalRepr)TerminalWritersafe_getattr)Config)Parser)fixture)FixtureRequest)	Collector)Item)OutcomeException)skip
fnmatch_ex)import_path)Module)approx)PytestWarningnoneZcdiffZndiffudiffZonly_first_failure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)r0   collectz--doctest-modules
store_trueFzRun doctests in all .py modulesdoctestmodules)actionr0   helpdestz--doctest-reportr'   z9Choose another output format for diffs on doctest failuredoctestreport)r/   r0   r6   choicesr7   z--doctest-globappendpatz2Doctests file matching pattern, default: test*.txtdoctestglob)r5   r0   metavarr6   r7   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 rD   P/var/www/html/python-backend/venv/lib/python3.9/site-packages/_pytest/doctest.pypytest_addoptionF   s^    
rF   r+   c                   C   s   d a d S N)RUNNER_CLASSrD   rD   rD   rE   pytest_unconfigurex   s    rJ   )DoctestModuleDoctestTextfile)	file_pathparentr+   c                 C   sb   |j }| jdkr@|jjr^tt| t| fs^tj|| d}|S nt	|| |r^t
j|| d}|S d S )Nz.pypath)configsuffixoptionr4   any_is_setup_py_is_main_pyrK   from_parent_is_doctestrL   )rM   rN   rQ   modtxtrD   rD   rE   pytest_collect_file~   s    

r[   )rP   r+   c                 C   s&   | j dkrdS |  }d|v p$d|v S )Nzsetup.pyFs
   setuptoolss	   distutils)name
read_bytes)rP   contentsrD   rD   rE   rU      s    
rU   )rQ   rP   rN   r+   c                    s@    j dv r|j rdS | dp(dg}t fdd|D S )N)z.txtz.rstTr<   z	test*.txtc                 3   s   | ]}t | V  qd S rH   r    ).0globrO   rD   rE   	<genexpr>       z_is_doctest.<locals>.<genexpr>)rR   sessionZ
isinitpath	getoptionrT   )rQ   rP   rN   globsrD   rO   rE   rX      s    rX   c                 C   s
   | j dkS )Nz__main__.py)r\   rO   rD   rD   rE   rV      s    rV   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 rH   )rg   )selfrg   rD   rD   rE   __init__   s    zReprFailDoctest.__init__)twr+   c                 C   s2   | j D ]&\}}|D ]}|| q|| qd S rH   )rg   line
toterminal)rh   rj   reprlocationlinesrk   rD   rD   rE   rl      s    zReprFailDoctest.toterminal)
__name__
__module____qualname__r   r   r   r@   ri   r   rl   rD   rD   rD   rE   rf      s   rf   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 rH   )superri   rs   rh   rs   	__class__rD   rE   ri      s    
z MultipleDoctestFailures.__init__)ro   rp   rq   r   ri   __classcell__rD   rD   rv   rE   rr      s   rr   doctest.DocTestRunnerc                     s"   dd l  G  fddd j} | S )Nr   c                       sx   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  Z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   Tr(   checkerverboseoptionflagscontinue_on_failurer+   c                    s   t  j|||d || _d S )N)r{   r|   r}   )rt   ri   r~   )rh   r{   r|   r}   r~   rv   rD   rE   ri      s    z8_init_runner_class.<locals>.PytestDoctestRunner.__init__doctest.DocTestzdoctest.Example)testexamplegotr+   c                    s(     |||}| jr || n|d S rH   )DocTestFailurer~   r:   )rh   outr   r   r   failuredoctestrD   rE   report_failure   s    z>_init_runner_class.<locals>.PytestDoctestRunner.report_failure)r   r   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UnexpectedExceptionr~   r:   )rh   r   r   r   r   r   r   rD   rE   report_unexpected_exception   s    
zK_init_runner_class.<locals>.PytestDoctestRunner.report_unexpected_exception)NNr   T)ro   rp   rq   __doc__r
   boolintri   r@   r   r   r   BaseExceptiontypesTracebackTyper   rx   rD   r   rv   rE   PytestDoctestRunner   s,       r   )r   ZDebugRunner)r   rD   r   rE   _init_runner_class   s    /r   Trz   c                 C   s   t d u rt a t | |||dS )Nr{   r|   r}   r~   )rI   r   r   rD   rD   rE   _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eedef ee ef dddZ  ZS )DoctestItemNz%Union[DoctestTextfile, DoctestModule]ry   r   )r\   rN   runnerdtestr+   c                    s*   t  || || _|| _d | _d | _d S rH   )rt   ri   r   r   objfixture_request)rh   r\   rN   r   r   rv   rD   rE   ri      s
    zDoctestItem.__init__)rN   r\   r   r   c                   s   t  j||||dS )zThe public named constructor.)r\   rN   r   r   )rt   rW   )clsrN   r\   r   r   rv   rD   rE   rW     s    zDoctestItem.from_parentrG   c                 C   sV   | j d urRt| | _t| jjd}| jd D ]\}}|||< q2| j j| d S )N)Z
getfixturedoctest_namespace)r   _setup_fixturesr   dictZgetfixturevalueitemsre   update)rh   re   r\   valuerD   rD   rE   setup  s    


zDoctestItem.setupc                 C   sT   | j d usJ | jd usJ t| j  |   g }| jj| j |d |rPt|d S )N)r   )r   r   _check_all_skipped$_disable_output_capturing_for_darwinrunrr   ru   rD   rD   rE   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systemrQ   pluginmanagerZ	getpluginZsuspend_global_captureZread_global_capturesysstdoutwritestderr)rh   Zcapmanr   errrD   rD   rE   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 u rRt |S g }|D ]v}|j	}|j
  j} jd u rd }n j|j d }t|j}	t|||	}
t }t| jd}|d ur*|j
jd usJ |j
jd} jd usJ  fddt|D }|t|jd d|jd  }n6dg}d	}|j D ] }|d
| d|  d}q>t||jr||||j|d7 }n:t|j}|dt |j g7 }|dd t!j"|j D 7 }||
|f qZt#|S )Nr   r   r8   Fc                    s&   g | ]\}}d | j  d |f qS )z%03d %sr   )lineno)r_   ixr   rD   rE   
<listcomp>`  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   rD   rD   rE   r   t  s   )$r   r   r   r   r   rr   rs   rt   repr_failurer   r   filenamer   r/   ro   r   _get_checker_get_report_choicerQ   rd   	docstring
splitlines	enumeratemaxsourcer:   Zoutput_differencer   splitr   Zfrom_exc_infor   repr	tracebackformat_exceptionrf   )rh   r   r   rs   rg   r   r   r   r   messagerm   r{   Zreport_choicern   indentrk   Zinner_excinforv   r   rE   r   :  sd    





 
zDoctestItem.repr_failurezos.PathLike[str]c                 C   s$   | j d usJ | j| j jd| j fS )Nz[doctest] %s)r   rP   r   r\   )rh   rD   rD   rE   
reportinfoz  s    zDoctestItem.reportinfo)NN)ro   rp   rq   r@   r
   ri   classmethodrW   r   r   r   r   r   r   r   r   r   r   r   rx   rD   rD   rv   rE   r      s,     

@r   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)r   r   r   r   r   r.   r   r   _get_allow_unicode_flag_get_allow_bytes_flag_get_number_flagr   rD   rD   rE   _get_flag_lookup  s    r   c                 C   s0   | j d}t }d}|D ]}||| O }q|S )Nr,   r   )rQ   getinir   )rN   Zoptionflags_strZflag_lookup_tableZflag_accflagrD   rD   rE   get_optionflags  s    r   c                 C   s    |  d}|r|  drd}|S )Nr?   ZusepdbF)getvalue)rQ   r~   rD   rD   rE   _get_continue_on_failure  s
    

r   c                   @   s"   e Zd ZdZee dddZdS )rL   NrG   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   r1   ro   __main__Fr|   r}   r{   r~   r\   r   r   )r   rQ   r   rP   	read_textr@   r\   r   r   r   r   ZDocTestParserZget_doctestexamplesr   rW   )rh   r   encodingtextr   r\   re   r}   r   r*   r   rD   rD   rE   r2     s&    

zDoctestTextfile.collect)ro   rp   rq   r   r   r   r2   rD   rD   rD   rE   rL     s   rL   r   )r   r+   c                    s0   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SKIPr   r   rD   rE   ra     rb   z%_check_all_skipped.<locals>.<genexpr>z!all tests skipped by +SKIP option)r   allr   r   )r   Zall_skippedrD   r   rE   r     s    r   )r   r+   c                 C   s   t | dddu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_existNr   r   rD   rD   rE   
_is_mocked  s    
r   )NNNc                  #   s\   t j ddtdtf tttgtf  td fdd} | t _zdV  W  t _n t _0 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             
      sz   z:|d u s|t u r t dW S |  fdddW S  tyt } z"td|f t  W Y d }~n
d }~0 0 d S )Nr   c                    s   t | p S rH   )r   r   _stopr   rD   rE   <lambda>  rb   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Zreal_unwrapr   rE   _mock_aware_unwrap  s    z4_patch_unwrap_mock_aware.<locals>._mock_aware_unwrap)inspectunwrapr   r   r
   )r   rD   r   rE   _patch_unwrap_mock_aware  s    r   c                   @   s   e Zd Zee dddZdS )rK   rG   c                 c   s   dd l }G dd d|j}| jjdkrJ| jjj| j| jd| jjd}nRz t	| j| jj| jdd}W n0 t
y   | jdrtd	| j  n Y n0 | }t| }td
|t t| jd}|||jD ] }|jrtj| |j||dV  qd S )Nr   c                       sJ   e Zd ZdZ fddZdd fddZejdk rB fd	d
Zn   Z	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                    s8   t |trt|d|}t|dr*t|}t ||S )a3  Doctest code does not take into account `@property`, this
                is a hackish way to fix it. https://bugs.python.org/issue17446

                Wrapped Doctests will need to be unwrapped so the correct
                line number is returned. This will be reported upstream. #8796
                fget__wrapped__)r   propertygetattrhasattrr   r   rt   _find_lineno)rh   r   source_linesrv   rD   rE   r    s    


zBDoctestModule.collect.<locals>.MockAwareDocTestFinder._find_linenoNrG   c              
      sN   t |rd S t ( t ||||||| W d    n1 s@0    Y  d S rH   )r   r   rt   _find)rh   testsr   r\   moduler  re   seenrv   rD   rE   r    s    z;DoctestModule.collect.<locals>.MockAwareDocTestFinder._find)      c                    s*   t tdrt|tjr|j}t ||S )a>  `cached_property` objects are never considered a part
                    of the 'current module'. As such they are skipped by doctest.
                    Here we override `_from_module` to check the underlying
                    function instead. https://github.com/python/cpython/issues/107995
                    cached_property)r   	functoolsr   r	  r   rt   _from_module)rh   r  objectrv   rD   rE   r    s
    zBDoctestModule.collect.<locals>.MockAwareDocTestFinder._from_module)
ro   rp   rq   r   r  r  r   version_infor  rx   rD   rD   rv   rE   MockAwareDocTestFinder  s   
r  zconftest.pyZ
importmode)rootpath)rootmoder>   zunable to import module %rFr   r   )r   ZDocTestFinderrP   r\   rQ   r   Z_importconftestrd   r  r"   ImportErrorr   r   r   r   r   r   findro   r   r   rW   )rh   r   r  r  finderr}   r   r   rD   rD   rE   r2     s>    9



zDoctestModule.collectN)ro   rp   rq   r   r   r2   rD   rD   rD   rE   rK     s   rK   )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.NrG   c                   S   s   d S rH   rD   rD   rD   rD   rE   r   U  s    z_setup_fixtures.<locals>.funcF)noder   r   funcargsT)Z	_ispytest)r  rc   Z_fixturemanagerZgetfixtureinfoZ_fixtureinfor   Z_fillfixtures)r  r   fmr   rD   rD   rE   r   R  s    r   c                     s*   dd l } dd l G  fddd| j}|S )Nr   c                       sf   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  Z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+)
              )
            )
            )wantr   r}   r+   c                    s   t  |||rdS |t @ }|t @ }|t @ }|sB|sB|sBdS tt ttdfdd}|rx|| j|}|| j|}|r|| j|}|| j|}|r| 	||}t  |||S )NTF)regexrZ   r+   c                    s     | d|S )Nz\1\2)sub)r  rZ   rerD   rE   remove_prefixes  s    zX_init_checker_class.<locals>.LiteralsOutputChecker.check_output.<locals>.remove_prefixes)
rt   check_outputr   r   r   r   r@   _unicode_literal_re_bytes_literal_re_remove_unwanted_precision)rh   r  r   r}   Zallow_unicodeZallow_bytesZallow_numberr  )rw   r  rD   rE   r    s"    


z?_init_checker_class.<locals>.LiteralsOutputChecker.check_output)r  r   r+   c                 S   s  t | j|}t | j|}t|t|kr4|S d}t||D ]\}}|d}|d}	|	d u rp|d}	|d u r|dnt|}
|	d ur|
t|	8 }
t| tt| d|
  dkrB|d |	 |  |  ||
 | d   }||
 |	  |
 |	   7 }qB|S )Nr   fractionZ	exponent1Z	exponent2
   )abs)list
_number_refinditerlenziprC   r   floatr$   startend)rh   r  r   Zwantsgotsoffsetwgr#  exponent	precisionrD   rD   rE   r"    s$    


&.&zM_init_checker_class.<locals>.LiteralsOutputChecker._remove_unwanted_precision)ro   rp   rq   compileUNICODEr   r!  VERBOSEr'  r@   r   r   r  r"  rx   rD   r  rv   rE   LiteralsOutputCheckerf  s   r7  )r   r  ZOutputChecker)r   r7  rD   r  rE   _init_checker_classb  s    Qr8  c                   C   s   t du 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)   r8  rD   rD   rD   rE   r     s    r   c                  C   s   ddl } | dS )z+Register and return the ALLOW_UNICODE flag.r   Nr   r   Zregister_optionflagr   rD   rD   rE   r     s    r   c                  C   s   ddl } | dS )z)Register and return the ALLOW_BYTES flag.r   Nr   r9  r   rD   rD   rE   r     s    r   c                  C   s   ddl } | dS )z$Register and return the NUMBER flag.r   Nr   r9  r   rD   rD   rE   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)
r   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:  r   rD   rD   rE   r     s    r   rc   )scopec                   C   s   t  S )a  Fixture that returns a :py:class:`dict` that will be injected into the
    namespace of doctests.

    Usually this fixture is used in conjunction with another ``autouse`` fixture:

    .. code-block:: python

        @pytest.fixture(autouse=True)
        def add_np(doctest_namespace):
            doctest_namespace["np"] = numpy

    For more details: :ref:`doctest_namespace`.
    )r   rD   rD   rD   rE   r     s    r   )NNr   T)fr   r   r
  r   osr   r   r   r   r   
contextlibr   pathlibr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   Z_pytestr   Z_pytest._code.coder   r   r   Z_pytest._ior   Z_pytest.compatr   Z_pytest.configr   Z_pytest.config.argparsingr   Z_pytest.fixturesr   r   Z_pytest.nodesr   r   Z_pytest.outcomesr   r   Z_pytest.pathlibr!   r"   Z_pytest.pythonr#   Z_pytest.python_apir$   Z_pytest.warning_typesr%   r   r?  r<  r=  r;  r>  rB   rI   r)   __annotations__rF   rJ   r[   r   rU   rX   rV   rf   r   rr   r   r   r   r   r@   r   r   r   rL   r   r  r   r   rK   r   r8  r   r   r   r   r   r   rD   rD   rD   rE   <module>   s   	2
6     	

	`X