
    @~ZM
                     ^    d Z ddlZddlZddlmZmZmZ d Z G d de      Z G d d	e      Z	y)
z
=================
stopit.threadstop
=================

Raise asynchronous exceptions in other thread, control the timeout of blocks
or callables with a context manager or a decorator.
    N   )TimeoutExceptionBaseTimeoutbase_timeoutablec                 T   t         j                  j                  t        j                  |       t        j                  |            }|dk(  rt        dj                  |             |dkD  r>t         j                  j                  t        j                  |       d       t        d      y)a  Raises an asynchronous exception in another thread.
    Read http://docs.python.org/c-api/init.html#PyThreadState_SetAsyncExc
    for further enlightenments.

    :param target_tid: target thread identifier
    :param exception: Exception class to be raised in that thread
    r   zInvalid thread ID {}r   Nz PyThreadState_SetAsyncExc failed)ctypes	pythonapiPyThreadState_SetAsyncExcc_long	py_object
ValueErrorformatSystemError)
target_tid	exceptionrets      1lib/python3.12/site-packages/stopit/threadstop.pyasync_raiser      s     


4
4V]]:5N5;5E5Ei5PRC ax/66zBCC	q226==3LdS<== 
    c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )ThreadingTimeoutzContext manager for limiting in the time the execution of a block
    using asynchronous threads launching exception.

    See :class:`stopit.utils.BaseTimeout` for more information
    c                     t         t        |   ||       t        j                         j
                  | _        d | _        y )N)superr   __init__	threadingcurrent_threadidentr   timer)selfsecondsswallow_exc	__class__s      r   r   zThreadingTimeout.__init__+   s2    .wD#224::
r   c                 b    t         j                  | _        t        | j                  t
               y)zcCalled by timer thread at timeout. Raises a Timeout exception in the
        caller thread
        N)r   	TIMED_OUTstater   r   r   r   s    r   stopzThreadingTimeout.stop0   s      !**
DOO%56r   c                     t        j                  | j                  | j                        | _        | j                  j                          y)z:Setting up the resource that interrupts the block
        N)r   Timerr    r'   r   startr&   s    r   setup_interruptz ThreadingTimeout.setup_interrupt8   s.     __T\\499=


r   c                 8    | j                   j                          y)z8Removing the resource that interrupts the block
        N)r   cancelr&   s    r   suppress_interruptz#ThreadingTimeout.suppress_interrupt>   s     	

r   )T)	__name__
__module____qualname____doc__r   r'   r+   r.   __classcell__)r"   s   @r   r   r   %   s    

7r   r   c                       e Zd ZdZeZy)threading_timeoutablea  A function or method decorator that raises a ``TimeoutException`` to
    decorated functions that should not last a certain amount of time.
    this one uses ``ThreadingTimeout`` context manager.

    See :class:`.utils.base_timoutable`` class for further comments.
    N)r/   r0   r1   r2   r   
to_ctx_mgr r   r   r5   r5   D   s     "Jr   r5   )
r2   r   r   utilsr   r   r   r   r   r5   r7   r   r   <module>r9      s6      B B>({ >", "r   