
    #)fE              	         d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZmZmZ d dlmZ erd dlZd dlZd dlmZmZmZ d dlmZ ej0                  dk  rd dlmZ  ed	e
      Ze G d dee                ZereZn	 d dlmZ ddZ! ejD                  d      jF                  Z$e G d dee                Z%ereZ&neZ&e G d deeee         e&e                Z'y# e $ r eZY aw xY w)    )annotationsN)	TYPE_CHECKINGCallableContextManagerGenericIterablePatternTypeVarcastoverload)final)ExceptionChainReprReprExceptionInfo	Traceback)	TypeGuard)      )BaseExceptionGroupE)boundc                      e Zd ZU dZded<   ddZddZedd       Ze	dd       Z
e	dd       Ze	dd	       Zddd
Z	 	 	 	 ddZ	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)_ExceptionInfozMinimal re-implementation of pytest.ExceptionInfo, only used if pytest is not available. Supports a subset of its features necessary for functionality of :class:`trio.testing.RaisesGroup` and :class:`trio.testing.Matcher`.-tuple[type[E], E, types.TracebackType] | None_excinfoc                    || _         y Nr   )selfexcinfos     W/var/www/html/flask-app/venv/lib/python3.12/site-packages/trio/testing/_raises_group.py__init__z_ExceptionInfo.__init__)   s	        c                8    | j                   J d       || _         y)z<Fill an unfilled ExceptionInfo created with ``for_later()``.Nz ExceptionInfo was already filledr   )r   exc_infos     r    fill_unfilledz_ExceptionInfo.fill_unfilled,   s    }}$H&HH r"   c                     | d      S )z!Return an unfilled ExceptionInfo.N )clss    r    	for_laterz_ExceptionInfo.for_later1   s     4yr"   c                F    | j                   J d       | j                   d   S )zThe exception class.z6.type can only be used after the context manager exitsr   r   r   s    r    typez_ExceptionInfo.type6   s.     MM%	DC	D }}Qr"   c                F    | j                   J d       | j                   d   S )zThe exception value.z7.value can only be used after the context manager exits   r   r+   s    r    valuez_ExceptionInfo.value>   s.     MM%	ED	E }}Qr"   c                F    | j                   J d       | j                   d   S )zThe exception raw traceback.z4.tb can only be used after the context manager exits   r   r+   s    r    tbz_ExceptionInfo.tbF   s.     MM%	BA	B }}Qr"   c                    t        d      Nz_This is a helper method only available if you use RaisesGroup with the pytest package installedNotImplementedError)r   tryshorts     r    exconlyz_ExceptionInfo.exconlyN   s    !m
 	
r"   c                    t        d      r4   r5   )r   excs     r    errisinstancez_ExceptionInfo.errisinstanceS   s     "m
 	
r"   c                    t        d      r4   r5   )r   
showlocalsstyleabspathtbfilterfuncargstruncate_localschains           r    getreprz_ExceptionInfo.getrepr[   s     "m
 	
r"   N)r   r   )r$   z&tuple[type[E], E, types.TracebackType]returnNone)rE   z_ExceptionInfo[E])rE   type[E])rE   r   )rE   ztypes.TracebackType)F)r7   boolrE   str)r:   zGbuiltins.type[BaseException] | tuple[builtins.type[BaseException], ...]rE   rH   )FlongFTFTT)r=   rH   r>   rI   r?   rH   r@   z;bool | Callable[[_ExceptionInfo[BaseException]], Traceback]rA   rH   rB   rH   rC   rH   rE   z&ReprExceptionInfo | ExceptionChainRepr)__name__
__module____qualname____doc____annotations__r!   r%   classmethodr)   propertyr,   r/   r2   r8   r;   rD   r'   r"   r    r   r   #   s     i;; !
              


T
 

 !PT $

 
 	

 N
 
 
 
 
0
r"   r   )ExceptionInfoc                R    dj                  t        |       gt        | dg             S )N
	__notes__)joinrI   getattr)r:   s    r    _stringify_exceptionrX   z   s0    99H	
S+r*	
 r"    c                      e Zd ZdZe	 	 d	 	 	 	 	 	 	 dd       Zedd	 	 	 	 	 dd       Zedd       Z	 	 	 d	 	 	 	 	 ddZdd	Zdd
Zy)Matchera  Helper class to be used together with RaisesGroups when you want to specify requirements on sub-exceptions. Only specifying the type is redundant, and it's also unnecessary when the type is a nested `RaisesGroup` since it supports the same arguments.
    The type is checked with `isinstance`, and does not need to be an exact match. If that is wanted you can use the ``check`` parameter.
    :meth:`trio.testing.Matcher.matches` can also be used standalone to check individual exceptions.

    Examples::

        with RaisesGroups(Matcher(ValueError, match="string"))
            ...
        with RaisesGroups(Matcher(check=lambda x: x.args == (3, "hello"))):
            ...
        with RaisesGroups(Matcher(check=lambda x: type(x) is ValueError)):
            ...

    .c                     y r   r'   r   exception_typematchchecks       r    r!   zMatcher.__init__   s     r"   )r`   c                    y r   r'   )r   r_   r`   s      r    r!   zMatcher.__init__   s     r"   c                    y r   r'   )r   r`   s     r    r!   zMatcher.__init__   s    CFr"   Nc                    |||t        d      |t        |t              st        d| d      || _        |  t	        |t
              r"t        j                  |      | _        || _	        y || _        || _	        y )Nz4You must specify at least one parameter to match on.zexception_type z$ must be a subclass of BaseException)

ValueError
issubclassBaseExceptionr^   
isinstancerI   recompiler_   r`   r]   s       r    r!   zMatcher.__init__   s     !emSTT%j.W!.!11UV  -eS!E*DJ 
 DJ
r"   c                   | j                   t        || j                         sy| j                  *t        j                  | j                  t        |            sy| j                   | j                  t        t        |            syy)a  Check if an exception matches the requirements of this Matcher.

        Examples::

            assert Matcher(ValueError).matches(my_exception):
            # is equivalent to
            assert isinstance(my_exception, ValueError)

            # this can be useful when checking e.g. the ``__cause__`` of an exception.
            with pytest.raises(ValueError) as excinfo:
                ...
            assert Matcher(SyntaxError, match="foo").matches(excinfo.value.__cause__)
            # above line is equivalent to
            assert isinstance(excinfo.value.__cause__, SyntaxError)
            assert re.search("foo", str(excinfo.value.__cause__)

        FT)	r^   rg   r_   rh   searchrX   r`   r   r   )r   	exceptions     r    matcheszMatcher.matches   st    $ *:t**4
 ::!"))JJ,Y7+
  ::!$**T!Y5G*Hr"   c                h   g }| j                   %|j                  | j                   j                         | j                  x}3|j                  d|j                  t
        k(  r|j                  n|       | j                  |j                  d| j                         ddj                  |       dS )Nzmatch=zcheck=zMatcher(, ))	r^   appendrK   r_   flags_regex_no_flagspatternr`   rV   )r   reqsr_   s      r    __str__zMatcher.__str__   s    *KK++445ZZE,KK%++*HeVW ::!KK&/0$))D/*!,,r"   )..)r   z
Matcher[E]r^   rG   r_   str | Pattern[str]r`   zCallable[[E], bool])r   zMatcher[BaseException]r_   rw   r`   Callable[[BaseException], bool])r`   rx   )NNN)r^   ztype[E] | Noner_   str | Pattern[str] | Noner`   zCallable[[E], bool] | None)rl   rf   rE   zTypeGuard[E]rE   rI   )rK   rL   rM   rN   r   r!   rm   rv   r'   r"   r    r[   r[      s       %(%(	 " #	   25$ "
 /  F F *.+/,0	& ) *	(@-r"   r[   c                      e Zd ZdZerddZdddd	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 ddZ	 	 	 	 dd	Z		 	 	 	 	 	 	 	 dd
Z
ddZy)RaisesGroupa|  Contextmanager for checking for an expected `ExceptionGroup`.
    This works similar to ``pytest.raises``, and a version of it will hopefully be added upstream, after which this can be deprecated and removed. See https://github.com/pytest-dev/pytest/issues/11538


    This differs from :ref:`except* <except_star>` in that all specified exceptions must be present, *and no others*. It will similarly not catch exceptions *not* wrapped in an exceptiongroup.
    If you don't care for the nesting level of the exceptions you can pass ``strict=False``.
    It currently does not care about the order of the exceptions, so ``RaisesGroups(ValueError, TypeError)`` is equivalent to ``RaisesGroups(TypeError, ValueError)``.

    This class is not as polished as ``pytest.raises``, and is currently not as helpful in e.g. printing diffs when strings don't match, suggesting you use ``re.escape``, etc.

    Examples::

        with RaisesGroups(ValueError):
            raise ExceptionGroup("", (ValueError(),))
        with RaisesGroups(ValueError, ValueError, Matcher(TypeError, match="expected int")):
            ...
        with RaisesGroups(KeyboardInterrupt, match="hello", check=lambda x: type(x) is BaseExceptionGroup):
            ...
        with RaisesGroups(RaisesGroups(ValueError)):
            raise ExceptionGroup("", (ExceptionGroup("", (ValueError(),)),))

        with RaisesGroups(ValueError, strict=False):
            raise ExceptionGroup("", (ExceptionGroup("", (ValueError(),)),))


    `RaisesGroup.matches` can also be used directly to check a standalone exception group.


    This class is also not perfectly smart, e.g. this will likely fail currently::

        with RaisesGroups(ValueError, Matcher(ValueError, match="hello")):
            raise ExceptionGroup("", (ValueError("hello"), ValueError("goodbye")))

    even though it generally does not care about the order of the exceptions in the group.
    To avoid the above you should specify the first ValueError with a Matcher as well.

    It is also not typechecked perfectly, and that's likely not possible with the current approach. Most common usage should work without issue though.
    c                     y r   r'   )r(   argskwargss      r    __new__zRaisesGroup.__new__0  s    r"   TN)strictr_   r`   c               ,   |g|| _         || _        || _        || _        d| _        | j                   D ]  }t        |t              r-|st        d      | xj                  |j                  z  c_        @t        |t              r<|j                  ]| xj                  t        |j                  t               z  c_        t        |t              r5t        |t              r%| xj                  t        |t               z  c_        t        d|d       y )NFzLYou cannot specify a nested structure inside a RaisesGroup with strict=FalsezInvalid argument "z2" must be exception type, Matcher, or RaisesGroup.)expected_exceptionsr   
match_exprr`   is_baseexceptiongrouprg   r|   rd   r[   r^   re   	Exceptionr,   rf   )r   rl   r   r_   r`   other_exceptionsr:   s          r    r!   zRaisesGroup.__init__2  s    J
J
  
%*"++ 	C#{+$(  **c.G.GG*C)%%-***&&	3 / * C&:c=+I***S)2L.LL* ( 0$ $ %	r"   c                L    t        j                         | _        | j                  S r   )rR   r)   r   r+   s    r    	__enter__zRaisesGroup.__enter__Z  s    =J=T=T=V||r"   c                    g }|D ]N  }t        |t              r+|j                  | j                  |j                               >|j                  |       P |S )zUsed in non-strict mode.)rg   r   extend_unroll_exceptions
exceptionsrq   )r   r   resr:   s       r    r   zRaisesGroup._unroll_exceptions^  sR     $& 	 C#12

4223>>BC 

3	  
r"   c                   |yt        |t              syt        |j                        t        | j                        k7  ry| j
                  *t        j                  | j
                  t        |            sy| j                  | j                  |      syt        | j                        }|j                  }| j                  s| j                  |      }|D ]  }|D ]  }t        |t              rt        ||      sTt        |t              r!t        |t              r|j                  |      s#t        |t               sa|j                  |      ss|j#                  |          y y)a  Check if an exception matches the requirements of this RaisesGroup.

        Example::

            with pytest.raises(TypeError) as excinfo:
                ...
            assert RaisesGroups(ValueError).matches(excinfo.value.__cause__)
            # the above line is equivalent to
            myexc = excinfo.value.__cause
            assert isinstance(myexc, BaseExceptionGroup)
            assert len(myexc.exceptions) == 1
            assert isinstance(myexc.exceptions[0], ValueError)
        FT)rg   r   lenr   r   r   rh   rk   rX   r`   listr   r   r,   r|   rm   r[   remove)r   exc_valremaining_exceptionsactual_exceptionserem_es         r    rm   zRaisesGroup.matchesk  s8   " ? '#56w!!"c$*B*B&CC??&ryyOO1':0
 ::!$**W*=#D$<$<=5<5G5G{{ $ 7 78I J
 # 	A- t,Au1E"1&89&uk:!MM!,"5'2u}}Q7G(//6 	 r"   c                    d}|J d| j                                 | j                  J d       | j                  |      syt        d|||f      }| j                  j	                  |       y)NTz&DID NOT RAISE any exception, expected z:Internal error - should have been constructed in __enter__FzNtuple[type[BaseExceptionGroup[E]], BaseExceptionGroup[E], types.TracebackType])expected_typer   rm   r   r%   )r   exc_typer   exc_tb__tracebackhide__r$   s         r    __exit__zRaisesGroup.__exit__  s     ! 	K3D4F4F4H3IJ	K LL$	HG	H ||G$ \w'
 	""8,r"   c                   g }| j                   D ]  }t        |t              r|j                  t	        |             .t        |t
              r |j                  |j                                ^t        |t              r|j                  |j                         t        d       | j                  rdnd}| ddj                  |       dS )Nzunknown typeBaserY   zExceptionGroup(ro   rp   )r   rg   r[   rq   rI   r|   r   r,   rK   AssertionErrorr   rV   )r   subexcsr   
group_types       r    r   zRaisesGroup.expected_type  s    )) 	5A!W%s1v&A{+q01At$qzz*$^44	5  $99Vr
_TYYw-?,@BBr"   )r~   objectr   r   rE   zRaisesGroup[E])
rl   type[E] | Matcher[E] | Er   r   r   rH   r_   ry   r`   z.Callable[[BaseExceptionGroup[E]], bool] | None)rE   z$ExceptionInfo[BaseExceptionGroup[E]])r   Iterable[BaseException]rE   r   )r   BaseException | NonerE   z TypeGuard[BaseExceptionGroup[E]])r   ztype[BaseException] | Noner   r   r   ztypes.TracebackType | NonerE   rH   rz   )rK   rL   rM   rN   r   r   r!   r   r   rm   r   r   r'   r"   r    r|   r|     s    %P P +/@D&+& 4& 	&
 )& >&P1	 7%7 
*7r, & +	
 
2Cr"   r|   )r:   rf   rE   rI   )(
__future__r   rh   systypingr   r   r   r   r   r	   r
   r   r   
trio._utilr   builtinstypes_pytest._code.coder   r   r   typing_extensionsr   version_infoexceptiongroupr   rf   r   r   rR   pytestImportErrorrX   ri   rr   rs   r[   
SuperClassr|   r'   r"   r    <module>r      s'   " 	 

 
 
  SS+g1C}% C
WQZ C
 C
V "M'( "**R.&& d-gaj d- d-j #J J DC./A!/D!EF
ST DC DCa  '&'s   =C C%$C%