a
    xd.                     @   s  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mZ d dl	Z
d dlZd dlZd dlmZ d dlZejdkrd dlZe Zed edZeedZnd	d
 Zdd Zdd ZG dd dZdd Zdd ZG dd dZG dd deZe
 dZ!G dd deZ"dd Z#dS )    )ABCMetaN)wrapsupdate_wrapper)
isasyncgenntzint raise(int);z!api-ms-win-crt-runtime-l1-1-0.dllraisec                 C   s   t t |  d S N)signalpthread_kill	threading	get_ident)signum r   B/var/www/html/Ranjet/env/lib/python3.9/site-packages/trio/_util.pysignal_raise@   s    r   c                   C   s6   zt  t jt t j W dS  ty0   Y dS 0 dS )z7Attempt to reliably check if we are in the main thread.TFN)r	   SIGINT	getsignal
ValueErrorr   r   r   r   is_main_threadL   s
    r   c                 G   s   dd }z| | }W nT t yh   t| tjjrJ|   t dj| dd || rbt d| d  Y n0 t|tjjs||rt d|t|rt d|t dt| d	| |S )
Nc                 S   s:   t | tjjrdS t| dd d ur&dS | jjdv r6dS dS )NTZ_asyncio_future_blocking)ZFutureZDeferredF)
isinstancecollectionsabc	Generatorgetattr	__class____name__)valuer   r   r   &_return_value_looks_like_wrong_libraryZ   s    zBcoroutine_or_error.<locals>._return_value_looks_like_wrong_librarya  Trio was expecting an async function, but instead it got a coroutine object {async_fn!r}

Probably you did something like:

  trio.run({async_fn.__name__}(...))            # incorrect!
  nursery.start_soon({async_fn.__name__}(...))  # incorrect!

Instead, you want (notice the parentheses!):

  trio.run({async_fn.__name__}, ...)            # correct!
  nursery.start_soon({async_fn.__name__}, ...)  # correct!)async_fnu   Trio was expecting an async function, but instead it got {!r} – are you trying to use a library written for asyncio/twisted/tornado or similar? That won't work without some sort of compatibility shim.u   Trio got unexpected {!r} – are you trying to use a library written for asyncio/twisted/tornado or similar? That won't work without some sort of compatibility shim.zEstart_soon expected an async function but got an async generator {!r}zCTrio expected an async function, but {!r} appears to be synchronous__qualname__)		TypeErrorr   r   r   	Coroutinecloseformatr   r   )r   argsr   coror   r   r   coroutine_or_errorY   sR    
r&   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ConflictDetectora  Detect when two tasks are about to perform operations that would
    conflict.

    Use as a synchronous context manager; if two tasks enter it at the same
    time then the second one raises an error. You can use it when there are
    two pieces of code that *would* collide and need a lock if they ever were
    called at the same time, but that should never happen.

    We use this in particular for things like, making sure that two different
    tasks don't call sendall simultaneously on the same stream.

    c                 C   s   || _ d| _d S NF)_msg_held)selfmsgr   r   r   __init__   s    zConflictDetector.__init__c                 C   s   | j rt| jnd| _ d S )NT)r*   trioZBusyResourceErrorr)   )r+   r   r   r   	__enter__   s    zConflictDetector.__enter__c                 G   s
   d| _ d S r(   )r*   )r+   r$   r   r   r   __exit__   s    zConflictDetector.__exit__N)r   
__module__r   __doc__r-   r/   r0   r   r   r   r   r'      s   r'   c                    s    fdd}|S )z@Similar to wraps, but for async wrappers of non-async functions.c                    s0    | _ dj f| _djj | _| S )N.z,Like :meth:`~{}.{}.{}`, but async.

        )r   joinr   r#   r1   r2   )func	attr_nameclswrapped_clsr   r   	decorator   s    
zasync_wraps.<locals>.decoratorr   )r8   r9   r7   r:   r   r6   r   async_wraps   s    r;   c                    sD   t   fdd | D ]\}ds  | q d S )Nc                    s   t |v rd S t | t|dd }|d ur|dr|_t|drbd|jvrb||_| |_t|t	r|j
 D ]\}} d | || qvd S )Nr1   ztrio.r   r3   )idaddr   
startswithr1   hasattrr   r   r   type__dict__items)qualnamenameobjmodr7   
attr_valuefix_onemodule_nameZobjnameZseen_idsr   r   rI      s    
z&fixup_module_metadata.<locals>.fix_one_)setrB   r>   )rJ   	namespacerE   r   rH   r   fixup_module_metadata   s
    
rN   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	generic_functiona;  Decorator that makes a function indexable, to communicate
    non-inferrable generic type parameters to a static type checker.

    If you write::

        @generic_function
        def open_memory_channel(max_buffer_size: int) -> Tuple[
            SendChannel[T], ReceiveChannel[T]
        ]: ...

    it is valid at runtime to say ``open_memory_channel[bytes](5)``.
    This behaves identically to ``open_memory_channel(5)`` at runtime,
    and currently won't type-check without a mypy plugin or clever stubs,
    but at least it becomes possible to write those.
    c                 C   s   t | | || _d S r   )r   _fn)r+   fnr   r   r   r-     s    
zgeneric_function.__init__c                 O   s   | j |i |S r   )rP   )r+   r$   kwargsr   r   r   __call__	  s    zgeneric_function.__call__c                 C   s   | S r   r   )r+   rK   r   r   r   __getitem__  s    zgeneric_function.__getitem__N)r   r1   r   r2   r-   rS   rT   r   r   r   r   rO      s   rO   c                       s    e Zd ZdZ fddZ  ZS )Finala<  Metaclass that enforces a class to be final (i.e., subclass not allowed).

    If a class uses this metaclass like this::

        class SomeClass(metaclass=Final):
            pass

    The metaclass will ensure that no sub class can be created.

    Raises
    ------
    - TypeError if a sub class is created
    c                    s>   |D ]&}t |trt|j d|j dqt | |||S )Nr3   z does not support subclassing)r   rU   r    r1   r   super__new__)r8   rD   basesZcls_namespacebaser   r   r   rW     s    
zFinal.__new__)r   r1   r   r2   rW   __classcell__r   r   rZ   r   rU     s   rU   Tc                       s>   e Zd ZdZdd Zeje ejejed fddZ	  Z
S )NoPublicConstructora  Metaclass that enforces a class to be final (i.e., subclass not allowed)
    and ensures a private constructor.

    If a class uses this metaclass like this::

        class SomeClass(metaclass=NoPublicConstructor):
            pass

    The metaclass will ensure that no sub class can be created, and that no instance
    can be initialized.

    If you try to instantiate your class (SomeClass()), a TypeError will be thrown.

    Raises
    ------
    - TypeError if a sub class or an instance is created.
    c                 O   s   t | j d| j dd S )Nr3   z has no public constructor)r    r1   r   r8   r$   rR   r   r   r   rS   >  s    zNoPublicConstructor.__call__)r8   r$   rR   returnc                    s   t  j|i |S r   )rV   rS   r^   rZ   r   r   _createC  s    zNoPublicConstructor._create)r   r1   r   r2   rS   tTyper\   Anyr`   r[   r   r   rZ   r   r]   +  s   r]   c              	   C   s|   t | dst| S z| jjd }W n$ ttfyF   d| jj}Y n0 z
| j	}W n tyl   | jj
}Y n0 | d| S )z{Return the fully-qualified name of the async generator function
    that produced the async generator iterator *agen*.
    ag_coder   z<{}>r3   )r?   reprag_frame	f_globalsAttributeErrorKeyErrorr#   rd   co_filenamer   co_name)ZagenmodulerC   r   r   r   name_asyncgenG  s    

rm   )$r   r   osr	   syspathlib	functoolsr   r   typingra   r   r   async_generatorr   r.   rD   ZcffiZFFIZ_ffiZcdefdlopenZ_libr   r   r   r&   r'   r;   rN   rO   rU   TypeVarr\   r]   rm   r   r   r   r   <module>   s6   
$

Q
