o
    Zhe                     @   s  U d Z ddlZddlmZ ddlZddlZddlZddlm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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!l5m6Z6 dd"l7m8Z8 dd#l9m:Z: erddl;Z;dd$lm<Z< d%Z=d&Z>d'Z?d(Z@d)ZAe=e>e?e@eAfZBdaCdaDeed*  eEd+< d,e)d-dfd.d/ZFdtd0d1ZGd2ed3e.d-eed4  fd5d6ZHd7ed-eIfd8d9ZJd:e'd7ed3e.d-eIfd;d<ZKd7ed-eIfd=d>ZLG d?d@ d@e!ZMG dAdB dBeNZOd-edC fdDdEZP				FdudGed* dHeeI dIeQdJeId-dCf
dKdLZRG dMdN dNe/ZSd-eeTeQf fdOdPZUd:e'd-eQfdQdRZVd:e'd-eIfdSdTZWG dUdV dVe6ZXdvdYdZZYd[eZd-eIfd\d]Z[ed-ed^ fd_d`Z\G dadb dbe6Z]d-ed* fdcddZ^dwdedfZ_d-eQfdgdhZ`d-eQfdidjZad-eQfdkdlZbdmeTd-eQfdndoZce+dpdqd-eeTef fdrdsZddS )xz4Discover 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)
TopRequest)	Collector)Item)OutcomeException)skip
fnmatch_ex)Module)approx)PytestWarning)Self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 collection errors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   F/var/www/html/lang_env/lib/python3.10/site-packages/_pytest/doctest.pypytest_addoptionH   s^   

rF   c                   C   s   d a d S N)RUNNER_CLASSrD   rD   rD   rE   pytest_unconfigurez   s   rI   	file_pathparent)DoctestModuleDoctestTextfilec                 C   s\   |j }| jdkr|jjrtt| t| fstj|| dS d S t	|| |r,t
j|| dS d S )Nz.pypath)configsuffixoptionr4   any_is_setup_py_is_main_pyrL   from_parent_is_doctestrM   )rJ   rK   rP   rD   rD   rE   pytest_collect_file   s   

rX   rO   c                 C   s&   | j dkrdS |  }d|v pd|v S )Nzsetup.pyFs
   setuptoolss	   distutils)name
read_bytes)rO   contentsrD   rD   rE   rT      s   
rT   rP   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 rG   r    ).0globrN   rD   rE   	<genexpr>   s    z_is_doctest.<locals>.<genexpr>)rQ   sessionZ
isinitpath	getoptionrS   )rP   rO   rK   globsrD   rN   rE   rW      s   rW   c                 C   s
   | j dkS )Nz__main__.py)rY   rN   rD   rD   rE   rU      s   
rU   c                   @   s@   e Zd Zdeeeee f  ddfddZdeddfddZ	dS )	ReprFailDoctestreprlocation_linesr+   Nc                 C   s
   || _ d S rG   )rc   )selfrc   rD   rD   rE   __init__   s   
zReprFailDoctest.__init__twc                 C   s2   | j D ]\}}|D ]}|| q	|| qd S rG   )rc   line
toterminal)rd   rf   reprlocationlinesrg   rD   rD   rE   rh      s
   zReprFailDoctest.toterminal)
__name__
__module____qualname__r   r   r   r@   re   r   rh   rD   rD   rD   rE   rb      s    
rb   c                       s*   e Zd Zded ddf fddZ  ZS )MultipleDoctestFailuresfailureszdoctest.DocTestFailurer+   Nc                    s   t    || _d S rG   )superre   ro   rd   ro   	__class__rD   rE   re      s   

z MultipleDoctestFailures.__init__)rk   rl   rm   r   re   __classcell__rD   rD   rr   rE   rn      s    "rn   doctest.DocTestRunnerc                     s"   dd l  G  fddd j} | S )Nr   c                       s   e Zd ZdZ				dded dee ded	ed
df
 fddZddddded
dffddZ	ddddde
ee eejf d
df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   Tcheckerr(   verboseoptionflagscontinue_on_failurer+   c                    s   t  j|||d || _d S )N)rv   rw   rx   )rp   re   ry   )rd   rv   rw   rx   ry   rr   rD   rE   re      s   
z8_init_runner_class.<locals>.PytestDoctestRunner.__init__testdoctest.DocTestexamplezdoctest.Examplegotc                    s&     |||}| jr|| d S |rG   )DocTestFailurery   r:   )rd   outrz   r|   r}   failuredoctestrD   rE   report_failure   s   z>_init_runner_class.<locals>.PytestDoctestRunner.report_failureexc_infoc                    sV   t |d tr|d t |d tjrtd  |||}| jr)|| d S |)N   zQuitting debugger)	
isinstancer   bdbZBdbQuitr   exitUnexpectedExceptionry   r:   )rd   r   rz   r|   r   r   r   rD   rE   report_unexpected_exception   s   
zK_init_runner_class.<locals>.PytestDoctestRunner.report_unexpected_exceptionNNr   T)rk   rl   rm   __doc__r
   boolintre   r@   r   r   r   BaseExceptiontypesTracebackTyper   rt   rD   r   rr   rE   PytestDoctestRunner   sF    
r   )r   ZDebugRunner)r   rD   r   rE   _init_runner_class   s   /r   Trv   rw   rx   ry   c                 C   s   t d u rt a t | |||dS )Nrv   rw   rx   ry   )rH   r   r   rD   rD   rE   _get_runner   s   r   c                       s   e Zd Zdedddddddd	f
 fd
dZedddeddddddf
 fddZdddZdddZdddZ	dddZ
dee deeef f fddZdeedef ee ef fddZ  ZS )DoctestItemrY   rK   z%Union[DoctestTextfile, DoctestModule]runnerru   dtestr{   r+   Nc                    sR   t  || || _|| _d | _| jj}|j| d d d}|| _|j	| _
|   d S )N)nodefunccls)rp   re   r   r   objr_   _fixturemanagerZgetfixtureinfoZ_fixtureinfoZnames_closureZfixturenames_initrequest)rd   rY   rK   r   r   fmZfixtureinforr   rD   rE   re      s   zDoctestItem.__init__r%   c                   s   t  j||||dS )zThe public named constructor.)rY   rK   r   r   )rp   rV   )r   rK   rY   r   r   rr   rD   rE   rV     s   zDoctestItem.from_parentc                 C   s   i | _ t| dd| _d S )NT)Z	_ispytest)Zfuncargsr   _requestrd   rD   rD   rE   r     s   zDoctestItem._initrequestc                 C   sL   | j   t| j jd}| j d D ]\}}|||< q| jj| d S )N)Z
getfixturedoctest_namespace)r   Z_fillfixturesdictZgetfixturevalueitemsr   ra   update)rd   ra   rY   valuerD   rD   rE   setup"  s
   

zDoctestItem.setupc                 C   s8   t | j |   g }| jj| j|d |rt|d S )N)r   )_check_all_skippedr   $_disable_output_capturing_for_darwinr   runrn   rq   rD   rD   rE   runtest)  s   
zDoctestItem.runtestc                 C   sZ   t  dkrdS | jjd}|r+|jdd | \}}tj	| tj
	| dS dS )zFDisable output capturing. Otherwise, stdout is lost to doctest (#985).DarwinNZcapturemanagerT)in_)platformsystemrP   ZpluginmanagerZ	getpluginZsuspend_global_captureZread_global_capturesysstdoutwritestderr)rd   Zcapmanr   errrD   rD   rE   r   3  s   z0DoctestItem._disable_output_capturing_for_darwinexcinfoc                    s  dd l }d }t|j|j|jfr|jg}n
t|jtr|jj}|d u r)t |S g }|D ]}|j	}|j
  j} jd u r@d }n j|j d }t|j}	t|||	}
t }t| jd}|d ur|j
jd usjJ |j
jd} jd usxJ  fddt|D }|t|jd d|jd  }ndg}d	}|j D ]}|d
| d|  d}qt||jr||||j|d7 }nt|j}|dt |j g7 }|dd t!j"|j D 7 }||
|f q-t#|S )Nr   r   r8   Fc                    s&   g | ]\}}d | j  d |f qS )z%03d %sr   )lineno)r\   ixrz   rD   rE   
<listcomp>e  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   y  s    
)$r   r   r   r~   r   rn   ro   rp   repr_failurer|   rz   filenamer   r/   rk   r   _get_checker_get_report_choicerP   r`   	docstring
splitlines	enumeratemaxsourcer:   Zoutput_differencer}   splitr   Zfrom_exc_infor   repr	tracebackformat_exceptionrb   )rd   r   r   ro   rc   r   r|   r   r   messageri   rv   Zreport_choicerj   indentrg   Zinner_excinforr   r   rE   r   ?  sd   



 
zDoctestItem.repr_failurezos.PathLike[str]c                 C   s   | j | jjd| j fS )Nz[doctest] %s)rO   r   r   rY   r   rD   rD   rE   
reportinfo  s   zDoctestItem.reportinfor+   N)rk   rl   rm   r@   re   classmethodrV   r   r   r   r   r   r   r   r   r   r   r
   r   r   rt   rD   rD   rr   rE   r      sD    





,@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   s.   |  d}t }d}|D ]}||| O }q|S )Nr,   r   )getinir   )rP   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)rP   ry   rD   rD   rE   _get_continue_on_failure  s
   

r   c                   @   s"   e Zd ZdZdee fddZdS )rM   Nr+   c                 c   s    dd l }| jd}| j|}t| j}| jj}ddi}t| j}td|t	 t
| jd}| }	|	||||d}
|
jrLtj| |
j||
dV  d S d S )Nr   r1   rk   __main__Frw   rx   rv   ry   rY   r   r   )r   rP   r   rO   	read_textr@   rY   r   r   r   r   ZDocTestParserZget_doctestexamplesr   rV   )rd   r   encodingtextr   rY   ra   rx   r   r*   rz   rD   rD   rE   r2     s*   


zDoctestTextfile.collect)rk   rl   rm   r   r   r   r2   rD   rD   rD   rE   rM     s    rM   rz   r{   c                    s4   ddl  t fdd| jD }|rtd dS 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   r^     s    z%_check_all_skipped.<locals>.<genexpr>z!all tests skipped by +SKIP option)r   allr   r   )rz   Zall_skippedrD   r   rE   r     s
   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dtdtf dtttgtf  dtf fdd} | t _z	dV  W  t _dS  t _w )	zContext manager which replaces ``inspect.unwrap`` with a version
    that's aware of mock objects and doesn't recurse into them.Nstopr   .r   r+   c             
      sp   z|d u s	|t u rt dW S |  fdddW S  ty7 } ztd|ddt  d }~ww )Nr   c                    s   t | p S rG   )r   r   _stopr   rD   rE   <lambda>  s    zF_patch_unwrap_mock_aware.<locals>._mock_aware_unwrap.<locals>.<lambda>zGot z when unwrapping z.  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dee fddZdS )rL   r+   c                 c   s    dd l }G dd d|j}z| j}W n tjy,   | jdr)td| j  n Y nw | j	j
|  | }t| j}td|t t| jd}|||jD ]}|jr_tj| |j||dV  qOd S )	Nr   c                       sR   e Zd ZdZ fddZ		d fddZejdk r$ fd	d
Z  Z	S 	   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   rp   _find_lineno)rd   r   source_linesrr   rD   rE   r    s   


zBDoctestModule.collect.<locals>.MockAwareDocTestFinder._find_linenor+   Nc              
      sP   t |rd S t  t ||||||| W d    d S 1 s!w   Y  d S rG   )r   r   rp   _find)rd   Ztestsr   rY   moduler  ra   seenrr   rD   rE   r    s   "z;DoctestModule.collect.<locals>.MockAwareDocTestFinder._find)      c                    s    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
                    )r   	functoolscached_propertyr   rp   _from_module)rd   r  objectrr   rD   rE   r  "  s   zBDoctestModule.collect.<locals>.MockAwareDocTestFinder._from_moduler   )
rk   rl   rm   r   r  r  r   version_infor  rt   rD   rD   rr   rE   MockAwareDocTestFinder  s    

r  r>   zunable to import module %rFr   r   )r   ZDocTestFinderr   r   ZCollectErrorrP   r   r   rO   r_   r   Zparsefactoriesr   r   r   r   findrk   r   r   rV   rY   )rd   r   r  r  finderrx   r   rz   rD   rD   rE   r2     s6   7


zDoctestModule.collectN)rk   rl   rm   r   r   r2   rD   rD   rD   rE   rL     s    rL   c                     s*   dd l } dd l G  fddd| j}|S )Nr   c                       sp   e Zd ZdjZdjZdjZde	de	de
def fdd	Zde	de	de	f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}   rx   r+   c                    s   t  |||r
dS |t @ }|t @ }|t @ }|s!|s!|s!dS dtt dtdtffdd}|r>|| j|}|| j|}|rL|| j|}|| j|}|rT| 	||}t  |||S )NTFregextxtr+   c                    s     | d|S )Nz\1\2)sub)r  r  rerD   rE   remove_prefixes{  s   zX_init_checker_class.<locals>.LiteralsOutputChecker.check_output.<locals>.remove_prefixes)
rp   check_outputr   r   r   r   r@   _unicode_literal_re_bytes_literal_re_remove_unwanted_precision)rd   r  r}   rx   Zallow_unicodeZallow_bytesZallow_numberr  )rs   r  rD   rE   r  p  s"   


z?_init_checker_class.<locals>.LiteralsOutputChecker.check_outputc                 S   s  t | j|}t | j|}t|t|kr|S d}t||D ]h\}}|d}|d}	|	d u r8|d}	|d u r>dnt|}
|	d urL|
t|	8 }
t| tt| d|
  dkr|d |	 |  |  ||
 | d   }||
 |	  |
 |	   7 }q!|S )Nr   fractionZ	exponent1Z	exponent2
   )abs)list
_number_refinditerlenziprC   r   floatr#   startend)rd   r  r}   Zwantsgotsoffsetwgr  exponent	precisionrD   rD   rE   r    s&   


&.$zM_init_checker_class.<locals>.LiteralsOutputChecker._remove_unwanted_precision)rk   rl   rm   compileUNICODEr  r  VERBOSEr   r@   r   r   r  r  rt   rD   r  rr   rE   LiteralsOutputCheckerR  s     r0  )r   r  ZOutputChecker)r   r0  rD   r  rE   _init_checker_classN  s   Qr1  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)   r1  rD   rD   rD   rE   r     s   r   c                  C      ddl } | dS )z+Register and return the ALLOW_UNICODE flag.r   Nr   r   Zregister_optionflagr   rD   rD   rE   r        
r   c                  C   r2  )z)Register and return the ALLOW_BYTES flag.r   Nr   r3  r   rD   rD   rE   r     r4  r   c                  C   r2  )z$Register and return the NUMBER flag.r   Nr   r3  r   rD   rD   rE   r     r4  r   keyc              
   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)r5  r   rD   rD   rE   r     s   r   r_   )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   r   r   )rz   r{   r+   N)r+   r(   )er   r   
contextlibr   r	  r   ospathlibr   r   r   r   r   typingr   r   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!   Z_pytest.pythonr"   Z_pytest.python_apir#   Z_pytest.warning_typesr$   r   r%   r:  r7  r8  r6  r9  rB   rH   r)   __annotations__rF   rI   rX   r   rT   rW   rU   rb   r   rn   r   r   r   r   r@   r   r   r   rM   r   r  r   r   rL   r1  r   r   r   r   r   r   rD   rD   rD   rE   <module>   s   	
2

6
 	


	X
X