a
    xdO                     @   sf   d dl Z d dlmZ d dlmZ d dlZddlmZmZm	Z	 edd Z
G dd	 d	Zed
d ZdS )    N)contextmanager)OrderedDict   )signal_raiseis_main_threadConflictDetectorc                 c   sp   i }zFt | D ]}t||||< qd V  W | D ]\}}t|| q4n | D ]\}}t|| qT0 d S N)setsignalitems)signalshandlerZoriginal_handlerssignumoriginal_handler r   E/var/www/html/Ranjet/env/lib/python3.9/site-packages/trio/_signals.py_signal_handler,   s    r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )SignalReceiverc                 C   s(   t  | _tj | _td| _d| _d S )Nz8only one task can iterate on a signal receiver at a timeF)	r   _pendingtriolowlevelZ
ParkingLot_lotr   _conflict_detector_closedselfr   r   r   __init__9   s    zSignalReceiver.__init__c                 C   s(   | j rt| nd | j|< | j  d S r   )r   r   r   r   Zunpark)r   r   r   r   r   _addB   s    

zSignalReceiver._addc                    s   d_  fdd    d S )NTc                     s8   j r4j jdd\} }zt|  W    n   0 d S )NFlast)r   popitemr   r   _deliver_nextr   r   r   r$   Q   s
    
z9SignalReceiver._redeliver_remaining.<locals>.deliver_next)r   r   r   r#   r   _redeliver_remainingI   s    z#SignalReceiver._redeliver_remainingc                 C   s
   t | jS r   )lenr   r   r   r   r   _pending_signal_count\   s    z$SignalReceiver._pending_signal_countc                 C   s   | S r   r   r   r   r   r   	__aiter___   s    zSignalReceiver.__aiter__c                    sv   | j rtd| jN | js.| j I d H  ntj I d H  | jj	dd\}}|W  d    S 1 sh0    Y  d S )Nz)open_signal_receiver block already exitedFr   )
r   RuntimeErrorr   r   r   Zparkr   r   
checkpointr    )r   r   r"   r   r   r   	__anext__b   s    zSignalReceiver.__anext__N)	__name__
__module____qualname__r   r   r%   r'   r(   r+   r   r   r   r   r   8   s   	r   c                  '   s   | st dt stdtj t   fdd}z<t| |  V  W d   n1 s`0    Y  W    n
   0 dS )a  A context manager for catching signals.

    Entering this context manager starts listening for the given signals and
    returns an async iterator; exiting the context manager stops listening.

    The async iterator blocks until a signal arrives, and then yields it.

    Note that if you leave the ``with`` block while the iterator has
    unextracted signals still pending inside it, then they will be
    re-delivered using Python's regular signal handling logic. This avoids a
    race condition when signals arrives just before we exit the ``with``
    block.

    Args:
      signals: the signals to listen for.

    Raises:
      TypeError: if no signals were provided.

      RuntimeError: if you try to use this anywhere except Python's main
          thread. (This is a Python limitation.)

    Example:

      A common convention for Unix daemons is that they should reload their
      configuration when they receive a ``SIGHUP``. Here's a sketch of what
      that might look like using :func:`open_signal_receiver`::

         with trio.open_signal_receiver(signal.SIGHUP) as signal_aiter:
             async for signum in signal_aiter:
                 assert signum == signal.SIGHUP
                 reload_configuration()

    zNo signals were providedz]Sorry, open_signal_receiver is only possible when running in Python interpreter's main threadc                    s   j  j| dd d S )NT)Z
idempotent)Zrun_sync_soonr   r!   queuetokenr   r   r      s    z%open_signal_receiver.<locals>.handlerN)		TypeErrorr   r)   r   r   Zcurrent_trio_tokenr   r   r%   )r   r   r   r/   r   open_signal_receiverq   s    $
&r3   )r
   
contextlibr   collectionsr   r   Z_utilr   r   r   r   r   r3   r   r   r   r   <module>   s   &
9