o
    4@Hh1                     @  sT  d dl mZ d dlZd dlZd dlmZmZ er"d dlmZm	Z	m
Z
 d dlZd dlZd dlmZmZ ddlmZ ddlmZm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j!dk rid dl"m#Z#m$Z$ erqd dlmZ edZ%d$ddZ&d$ddZ'd$ddZ(ej)*dd$ddZ+d$ddZ,d$ddZ-d$ddZ.d$d d!Z/d$d"d#Z0dS )%    )annotationsN)TYPE_CHECKINGTypeVar)AsyncGenerator	Coroutine	Generator)MatcherRaisesGroup   )_core)!create_asyncio_future_in_new_loop'ignore_coroutine_never_awaited_warnings)	ConflictDetectorMultipleExceptionErrorNoPublicConstructorcoroutine_or_errorfinalfixup_module_metadatageneric_functionis_main_thread!raise_single_exception_from_groupwait_all_tasks_blocked      )BaseExceptionGroupExceptionGroup)r   TreturnNonec               
     s~  t d t d}   |  td W d    n1 sw   Y  W d    n1 s,w   Y  tjtjdd,     W d    n1 sJw   Y  W d    n1 sYw   Y  W d    n1 shw   Y  d	 fdd}tttjd9 t 4 I d H }|	| |	| W d   I d H  n1 I d H sw   Y  W d    d S W d    d S 1 sw   Y  d S )
Nul1ul2okmatchr   r    c                     s8     t  I d H  W d    d S 1 sw   Y  d S Nr    r!   r'   U/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/trio/_tests/test_util.pywait_with_ul16   s   "z,test_ConflictDetector.<locals>.wait_with_ul1r   r    )
r   printpytestraisesr   BusyResourceErrorr	   r   open_nursery
start_soon)r"   r*   nurseryr'   r(   r)   test_ConflictDetector)   s6   

*"r3   c                  C  s  dd l } dd l} | jjdksJ | jjdksJ | jjjdks!J | jjjdks*J | j	j
jdks3J | jjjjdks=J | jjjjdksGJ | jjdksOJ | jjdksWJ | jjjjdksaJ | jjjjdkskJ | jjd	kssJ | jjjd
ks|J | jjjd
ksJ d S )Nr   trioztrio.abcztrio.lowlevelztrio.testing	Cancelledsend_allzSendStream.send_allztrio.to_threadrun_sync)r4   trio.testingr5   
__module__r0   abcStreamlowlevelwait_task_rescheduledtesting	trio_test
ParkingLot__init__r6   __name____qualname__
SendStream	to_threadr7   )r4   r'   r'   r)    test_module_metadata_is_fixed_up@   s    rF   c                    s,   t  sJ ddd} tj| I d H  d S )Nr   r    c                   S  s   t  rJ d S r&   )r   r'   r'   r'   r)   not_main_thread[   s   z,test_is_main_thread.<locals>.not_main_threadr+   )r   r4   rE   r7   )rG   r'   r'   r)   test_is_main_threadX   s   

rH   z(ignore:.*@coroutine.*:DeprecationWarningc               	     s  G dd d t  K ddd} tt}t|   W d    n1 s&w   Y  dt|jv s4J dd ltj	d	k rjj
dfdd}tt}t|  W d    n1 s\w   Y  dt|jv sjJ tt}tt  W d    n1 sw   Y  dt|jv sJ tt}tt W d    n1 sw   Y  dt|jv sJ tt}t   W d    n1 sw   Y  dt|jv sJ tt}t fdd W d    n1 sw   Y  dt|jv sJ tt}ttg dg W d    n	1 sw   Y  dt|jv s J ddd}tt}t|dg W d    n	1 s<w   Y  d}|t|jv sMJ ~W d    d S 1 sZw   Y  d S )Nc                   @  s   e Zd ZdZdS )z)test_coroutine_or_error.<locals>.DeferredzJust kiddingN)rB   r9   rC   __doc__r'   r'   r'   r)   Deferredd   s    rJ   r   r    c                     s   d S r&   r'   r'   r'   r'   r)   fi   s   z"test_coroutine_or_error.<locals>.fzexpecting an async functionr   r   2Generator[Coroutine[None, None, None], None, None]c                   3  s      dE d H  d S )N   )sleepr'   )asyncior'   r)   generator_based_corot   s   z5test_coroutine_or_error.<locals>.generator_based_cororO   twistedc                     s     S r&   r'   r'   )rJ   r'   r)   <lambda>   s    z)test_coroutine_or_error.<locals>.<lambda>)rM   r
   r   zappears to be synchronous_objectAsyncGenerator[None, None]c                 S s   d V  d S r&   r'   )rS   r'   r'   r)   	async_gen   s   
z*test_coroutine_or_error.<locals>.async_genz5expected an async function but got an async generatorr+   )r   rL   )rS   rT   r   rU   )r   r-   r.   	TypeErrorr   strvaluerO   sysversion_info	coroutiner   len)rK   excinforP   rV   msgr'   )rJ   rO   r)   test_coroutine_or_errorb   sV   




$r`   c                  C  s   t d	dd} | | t   u r| ttf u sJ  J | d| t d  kr,dks/J  J | jdks6J | jdks=J | jdksDJ | jtksKJ d S )
Nargr   r   c                 S  s   | S )Look, a docstring!r'   )ra   r'   r'   r)   	test_func   s   z(test_generic_function.<locals>.test_func*   rb   rc   )ra   r   r   r   )r   intrX   rI   rC   rB   r9   )rc   r'   r'   r)   test_generic_function   s   ((rf   c                  C  sR   t G dd d} tt G dd d| }W d   dS 1 s"w   Y  dS )zTest that subclassing a @final-annotated class is not allowed.

    This checks both runtime results, and verifies that type checkers detect
    the error statically through the type-ignore comment.
    c                   @     e Zd ZdS )z(test_final_decorator.<locals>.FinalClassNrB   r9   rC   r'   r'   r'   r)   
FinalClass   s    ri   c                   @  rg   )z&test_final_decorator.<locals>.SubClassNrh   r'   r'   r'   r)   SubClass   s    rj   N)r   r-   r.   rW   )ri   rj   r'   r'   r)   test_final_decorator   s
   "rk   c                  C  sb   G dd dt d} tt | dd W d   n1 sw   Y  t| jddd| s/J dS )zLThe NoPublicConstructor metaclass prevents calling the constructor directly.c                   @  s   e Zd Zd
ddZd	S )z:test_no_public_constructor_metaclass.<locals>.SpecialClassare   bfloatr   r    c                 S  s   |dksJ |dksJ dS )z*Check arguments can be passed to __init__.   333333	@Nr'   )selfrl   rm   r'   r'   r)   rA      s   zCtest_no_public_constructor_metaclass.<locals>.SpecialClass.__init__N)rl   re   rm   rn   r   r    )rB   r9   rC   rA   r'   r'   r'   r)   SpecialClass   s    rr   )	metaclassro   rp   N)rm   )r   r-   r.   rW   
isinstance_create)rr   r'   r'   r)   $test_no_public_constructor_metaclass   s
   rv   c                  C  s  t d} dd | _d| j_d| j_t| jt|  | jjdks"J | jjdks*J t d}dd |_d|j_d|j_t  |_d|j_t  |_	d|j	_
d	|j	_d
d |_d|j_
d |j_|j_tdddd dd d|_|j|j_tdt| |jjdksJ |jj
dksJ |jjdksJ |jjdksJ |jjdksJ |jj
dksJ |jjdksJ |j	jdksJ |j	j
dksJ t|j	drJ |jjjdksJ |jjj
dksJ |jjjdksJ |   |  |  |   d S )Nnot_trioc                   S     d S r&   r'   r'   r'   r'   r)   rR          z,test_fixup_module_metadata.<locals>.<lambda>	some_funcztrio._somemodule_implc                   S  rx   r&   r'   r'   r'   r'   r)   rR      ry   _something_elsenot_funclike	only_namec                   S  rx   r&   r'   r'   r'   r'   r)   rR      ry   _private	SomeClassr'   c                 S  rx   r&   r'   rq   r'   r'   r)   rR      ry   c                 S  rx   r&   r'   r   r'   r'   r)   rR      ry   )rA   methodztrio.somemoduleonly_has_namerC   r   zSomeClass.method)types
ModuleTyperz   rB   rC   r   varsSimpleNamespacer|   r   r9   r~   typer   	recursionhasattrr   )non_trio_modulemodr'   r'   r)   test_fixup_module_metadata   sZ   







	r   c                    sZ  t d} td}td}|| _|| _tj }tj	t dd}t
td| g W d    n1 s2w   Y  |jj|ks?J |jj|ksGJ td| g}td}td}||_||_ttt d	dd
d}t
td|g W d    n1 sxw   Y  |jj|ksJ |jj|ksJ tj	t dd}t
td||| g W d    n1 sw   Y  |jj|ksJ |jj|ksJ tdt dt dg}	tj	tdd}t
|	 W d    n1 sw   Y  |jj|	u sJ |jjd u sJ tdt dt dtdg}
tj	tdd}t
|
 W d    n	1 sw   Y  |jj|
u s'J |jjd u s0J tdt dt dtdg}tj	tdd}t
| W d    n	1 sUw   Y  |jj|u scJ |jjd u slJ tj	tjdd}t
td|tj g W d    n	1 sw   Y  |j|u sJ |jjd u sJ |jjd u sJ d S )Nfoocausecontextr$    zinner egzinner eg causezinner eg contextz^foo$z
^inner eg$barzy^Attempted to unwrap exceptiongroup with multiple non-cancelled exceptions. This is often caused by a bug in the caller.$zthis exc doesn't get reraisedz^$z^Cancelled$)
ValueErrorSyntaxErrorrW   	__cause____context__r4   r5   ru   r-   r.   r   r   rY   r	   r   r   r   KeyboardInterrupt
SystemExit)excr   r   	cancelledr^   inner_eginner_causeinner_contexteginfoegeg_kisystemexit_kir'   r'   r)   &test_raise_single_exception_from_group  s   



r   r+   )1
__future__r   rZ   r   typingr   r   collections.abcr   r   r   r-   r4   r8   r   r	   r   r   _core._tests.tutilr   r   _utilr   r   r   r   r   r   r   r   r   r>   r   r[   exceptiongroupr   r   r   r3   rF   rH   markfilterwarningsr`   rf   rk   rv   r   r   r'   r'   r'   r)   <module>   s8    ,






?


D