o
    1&i$1                     @   sj  d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z
 ddlmZmZ ddlmZ ddlmZmZ G dd dejZeg d	Zd
d ZG dd dZeeZdd Zdd Zdd ZG dd dejZG dd deZ G dd deZ!edd Z"edd Z#edd Z$d+d d!Z%d,d"d#Z&ed+d$d%Z'd&e!fd'd(Z(d)d* Z)ej*re)  dS dS )-ag  
The ``numba.core.event`` module provides a simple event system for applications
to register callbacks to listen to specific compiler events.

The following events are built in:

- ``"numba:compile"`` is broadcast when a dispatcher is compiling. Events of
  this kind have ``data`` defined to be a ``dict`` with the following
  key-values:

  - ``"dispatcher"``: the dispatcher object that is compiling.
  - ``"args"``: the argument types.
  - ``"return_type"``: the return type.

- ``"numba:compiler_lock"`` is broadcast when the internal compiler-lock is
  acquired. This is mostly used internally to measure time spent with the lock
  acquired.

- ``"numba:llvm_lock"`` is broadcast when the internal LLVM-lock is acquired.
  This is used internally to measure time spent with the lock acquired.

- ``"numba:run_pass"`` is broadcast when a compiler pass is running.

    - ``"name"``: pass name.
    - ``"qualname"``: qualified name of the function being compiled.
    - ``"module"``: module name of the function being compiled.
    - ``"flags"``: compilation flags.
    - ``"args"``: argument types.
    - ``"return_type"`` return type.

Applications can register callbacks that are listening for specific events using
``register(kind: str, listener: Listener)``, where ``listener`` is an instance
of ``Listener`` that defines custom actions on occurrence of the specific event.
    N)default_timer)contextmanager	ExitStack)defaultdict)configutilsc                   @   s    e Zd ZdZe Ze ZdS )EventStatuszStatus of an event.
    N)__name__
__module____qualname____doc__enumautoSTARTEND r   r   ;C:\wamp64\www\opt\env\Lib\site-packages\numba/core/event.pyr   2   s    r   )znumba:compiler_lockznumba:compileznumba:llvm_locknumba:run_passc                 C   s(   |  dr| tvr|  d}t|| S )a>  Guard to ensure that an event kind is valid.

    All event kinds with a "numba:" prefix must be defined in the pre-defined
    ``numba.core.event._builtin_kinds``.
    Custom event kinds are allowed by not using the above prefix.

    Parameters
    ----------
    kind : str

    Return
    ------
    res : str
    znumba:zG is not a valid event kind, it starts with the reserved prefix 'numba:')
startswith_builtin_kinds
ValueError)kindmsgr   r   r   _guard_kindB   s   
r   c                   @   sn   e Zd ZdZdddZedd Zedd Zed	d
 Zedd Z	edd Z
edd Zdd ZeZdS )EventzAn event.

    Parameters
    ----------
    kind : str
    status : EventStatus
    data : any; optional
        Additional data for the event.
    exc_details : 3-tuple; optional
        Same 3-tuple for ``__exit__``.
    Nc                 C   s>   t || _|| _|| _|d u s|d d u rd | _d S || _d S Nr   )r   _kind_status_data_exc_details)selfr   statusdataexc_detailsr   r   r   __init__d   s   

zEvent.__init__c                 C      | j S )zFEvent kind

        Returns
        -------
        res : str
        )r   r    r   r   r   r   l      z
Event.kindc                 C   r%   )zPEvent status

        Returns
        -------
        res : EventStatus
        )r   r&   r   r   r   r!   v   r'   zEvent.statusc                 C   r%   )zIEvent data

        Returns
        -------
        res : object
        )r   r&   r   r   r   r"      r'   z
Event.datac                 C      | j tjkS )zSIs it a *START* event?

        Returns
        -------
        res : bool
        )r   r   r   r&   r   r   r   is_start      zEvent.is_startc                 C   r(   )zRIs it an *END* event?

        Returns
        -------
        res : bool
        )r   r   r   r&   r   r   r   is_end   r*   zEvent.is_endc                 C   s
   | j du S )zIs the event carrying an exception?

        This is used for *END* event. This method will never return ``True``
        in a *START* event.

        Returns
        -------
        res : bool
        N)r   r&   r   r   r   	is_failed   s   
zEvent.is_failedc                 C   s8   | j d urt| j j nd}d| j d| j d| dS )NNonezEvent(z, z, data: ))r"   typer   r   r   )r    r"   r   r   r   __str__   s
   
zEvent.__str__NN)r	   r
   r   r   r$   propertyr   r!   r"   r)   r+   r,   r0   __repr__r   r   r   r   r   X   s"    

	
	
	
	
	
r   c                 C   s(   t |tsJ t| } t|  | dS )zvRegister a listener for a given event kind.

    Parameters
    ----------
    kind : str
    listener : Listener
    N)
isinstanceListenerr   _registeredappendr   listenerr   r   r   register   s   r:   c                 C   s,   t |tsJ t| } t|  }|| dS )zxUnregister a listener for a given event kind.

    Parameters
    ----------
    kind : str
    listener : Listener
    N)r4   r5   r   r6   remove)r   r9   lstr   r   r   
unregister   s   r=   c                 C   s   t | j D ]}||  qdS )zeBroadcast an event to all registered listeners.

    Parameters
    ----------
    event : Event
    N)r6   r   notify)eventr9   r   r   r   	broadcast   s   r@   c                   @   s4   e Zd ZdZejdd Zejdd Zdd ZdS )	r5   z(Base class for all event listeners.
    c                 C      dS )zkCalled when there is a *START* event.

        Parameters
        ----------
        event : Event
        Nr   r    r?   r   r   r   on_start      zListener.on_startc                 C   rA   )ziCalled when there is a *END* event.

        Parameters
        ----------
        event : Event
        Nr   rB   r   r   r   on_end   rD   zListener.on_endc                 C   s0   |j r
| | dS |jr| | dS td)zpNotify this Listener with the given Event.

        Parameters
        ----------
        event : Event
        ZunreachableN)r)   rC   r+   rE   AssertionErrorrB   r   r   r   r>      s
   zListener.notifyN)	r	   r
   r   r   abcabstractmethodrC   rE   r>   r   r   r   r   r5      s    
	
	r5   c                   @   s@   e Zd ZdZdd Zdd Zdd Zedd	 Zed
d Z	dS )TimingListenerzA listener that measures the total time spent between *START* and
    *END* events during the time this listener is active.
    c                 C   s
   d| _ d S r   )_depthr&   r   r   r   r$        
zTimingListener.__init__c                 C   s$   | j dkr	t | _|  j d7  _ d S )Nr      )rJ   timer_tsrB   r   r   r   rC   	  s   
zTimingListener.on_startc                 C   s>   |  j d8  _ | j dkrt| dd}t | j | | _d S d S )NrL   r   	_duration)rJ   getattrrM   rN   rO   )r    r?   lastr   r   r   rE     s
   
zTimingListener.on_endc                 C   s
   t | dS )zReturns a ``bool`` indicating whether a measurement has been made.

        When this returns ``False``, the matching event has never fired.
        If and only if this returns ``True``, ``.duration`` can be read without
        error.
        rO   )hasattrr&   r   r   r   done  s   
zTimingListener.donec                 C   r%   )zReturns the measured duration.

        This may raise ``AttributeError``. Users can use ``.done`` to check
        that a measurement has been made.
        )rO   r&   r   r   r   duration  s   zTimingListener.durationN)
r	   r
   r   r   r$   rC   rE   r2   rS   rT   r   r   r   r   rI     s    
	rI   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	RecordingListenera  A listener that records all events and stores them in the ``.buffer``
    attribute as a list of 2-tuple ``(float, Event)``, where the first element
    is the time the event occurred as returned by ``time.time()`` and the second
    element is the event.
    c                 C   s
   g | _ d S N)bufferr&   r   r   r   r$   .  rK   zRecordingListener.__init__c                 C      | j t |f d S rV   rW   r7   timerB   r   r   r   rC   1     zRecordingListener.on_startc                 C   rX   rV   rY   rB   r   r   r   rE   4  r[   zRecordingListener.on_endN)r	   r
   r   r   r$   rC   rE   r   r   r   r   rU   (  s
    rU   c              	   c   s0    t | | z|V  W t| | dS t| | w )a  Install a listener for event "kind" temporarily within the duration of
    the context.

    Returns
    -------
    res : Listener
        The *listener* provided.

    Examples
    --------

    >>> with install_listener("numba:compile", listener):
    >>>     some_code()  # listener will be active here.
    >>> other_code()     # listener will be unregistered by this point.

    N)r:   r=   r8   r   r   r   install_listener8  s
   
r\   c                 c   sP    t  }t| | |V  W d   n1 sw   Y  |jr&||j dS dS )a  Install a TimingListener temporarily to measure the duration of
    an event.

    If the context completes successfully, the *callback* function is executed.
    The *callback* function is expected to take a float argument for the
    duration in seconds.

    Returns
    -------
    res : TimingListener

    Examples
    --------

    This is equivalent to:

    >>> with install_listener(kind, TimingListener()) as res:
    >>>    ...
    N)rI   r\   rS   rT   )r   callbacktlr   r   r   install_timerQ  s   r_   c                 c   s>    t  }t| | |V  W d   dS 1 sw   Y  dS )an  Install a RecordingListener temporarily to record all events.

    Once the context is closed, users can use ``RecordingListener.buffer``
    to access the recorded events.

    Returns
    -------
    res : RecordingListener

    Examples
    --------

    This is equivalent to:

    >>> with install_listener(kind, RecordingListener()) as res:
    >>>    ...
    N)rU   r\   )r   Zrlr   r   r   install_recordern  s
   "r`   c                 C   s   t | tj|d}t| dS )zTrigger the start of an event of *kind* with *data*.

    Parameters
    ----------
    kind : str
        Event kind.
    data : any; optional
        Extra event data.
    )r   r!   r"   N)r   r   r   r@   )r   r"   evtr   r   r   start_event  s   
rb   c                 C   s   t | tj||d}t| dS )a  Trigger the end of an event of *kind*, *exc_details*.

    Parameters
    ----------
    kind : str
        Event kind.
    data : any; optional
        Extra event data.
    exc_details : 3-tuple; optional
        Same 3-tuple for ``__exit__``. Or, ``None`` if no error.
    )r   r!   r"   r#   N)r   r   r   r@   )r   r"   r#   ra   r   r   r   	end_event  s   
rc   c                 #   sT    t  }|j fdd}t d dV  W d   dS 1 s#w   Y  dS )a;  A context manager to trigger the start and end events of *kind* with
    *data*. The start event is triggered when entering the context.
    The end event is triggered when exiting the context.

    Parameters
    ----------
    kind : str
        Event kind.
    data : any; optional
        Extra event data.
    c                     s   t  | d d S )N)r"   r#   )rc   )r#   r"   r   r   r   on_exit  s   ztrigger_event.<locals>.on_exit)r"   N)r   pushrb   )r   r"   scopere   r   rd   r   trigger_event  s   "rh   r9   c              
   C   sz   t  }t }g }| jD ]-\}}|j}t|j}|d }|jr"dnd}	|d }
|}t	|||||	|
|d}|
| q|S )zGPrepare events in `listener` for serializing as chrome trace data.
    i@B BEname)catpidtidtsphrk   args)osgetpid	threadingget_native_idrW   r"   strr   r)   dictr7   )r9   rm   rn   evsro   Zrecr"   rl   Z	ts_scaledrp   rk   rq   Zevr   r   r   _prepare_chrome_trace_data  s    
ry   c                     s.   t  td tj tj fdd} dS )z\Setup a RecordingListener and an exit handler to write the captured
    events to file.
    r   c                     sJ   t } t d}tj| |tjd W d    d S 1 sw   Y  d S )Nw)cls)ry   openjsondumpr   Z_LazyJSONEncoder)rx   outfilenamer9   r   r   _write_chrome_trace  s   "z=_setup_chrome_trace_exit_handler.<locals>._write_chrome_traceN)rU   r:   r   CHROME_TRACEatexit)r   r   r   r    _setup_chrome_trace_exit_handler  s
   
r   rV   r1   )+r   rr   r}   r   rG   r   rZ   rt   Ztimeitr   rM   
contextlibr   r   collectionsr   Z
numba.corer   r   Enumr   	frozensetr   r   r   listr6   r:   r=   r@   ABCr5   rI   rU   r\   r_   r`   rb   rc   rh   ry   r   r   r   r   r   r   <module>   sL    #[&&





