o
    i(                     @   s   d Z ddlZddlmZ ddlZddlmZ ddlmZ ee	Z
ejZejZejZejZdZdd ZG d	d
 d
ejZdd ZdS )a  Provides functionality for context-based coverage to work across threads

Without this, context-based collection in the parent process would not capture code executed by threads (due to the
parent process' context variables not being shared in threads).

The collection of coverage is done when the thread's join() method is called, so context-level coverage will not be
captured if join() is not called.

Since the ModuleCodeCollector is already installed at the process level, there is no need to reinstall it or ensure that
its include_paths are set.

Session-level coverage does not need special-casing since the ModuleCodeCollector behavior is process-wide and
thread-safe.
    N)Queue)ModuleCodeCollector)
get_logger_datadog_patchc                   C   s
   t ttS N)hasattr	threadingDD_PATCH_ATTR r
   r
   `/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/coverage/threading_coverage.py_is_patched"   s   
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )CoverageCollectingThreadc                 O   s:   t  ot  | _| jrt | _t| g|R i | dS )zWraps the thread initialization creation to enable coverage collection

        Only enables coverage if the parent process' context-level coverage is enabled.
        N)r   is_installedcoverage_enabled_in_context_should_coverr   _coverage_queuethread_init)selfargskwargsr
   r
   r   __init__'   s   z!CoverageCollectingThread.__init__c                 C   s   | j rt | _| j  z;t|  W | j rEd}ztd| j i}W n tj	y3   t
d Y nw | j  |durG| j| dS dS dS | j rzd}ztd| j i}W n tj	yi   t
d Y nw | j  |dur{| j| w w w )z`Collect thread-level coverage data in a context and queue it up for the parent process to absorbNcoveredz6Could not pickle coverage data, not injecting coverage)r   r   CollectInContext_coverage_context	__enter__thread_boostrap_innerpickledumpsget_covered_linesPicklingErrorlogwarning__exit__r   put)r   covered_linesr
   r
   r   _bootstrap_inner3   s:   




z)CoverageCollectingThread._bootstrap_innerc                 O   s   t | g|R i | | jr<| j r>zt| j }|di }W n tjy3   t	d Y dS w t
j|d dS dS dS )z6Absorb coverage data from the thread after it's joinedr   z8Could not unpickle coverage data, not injecting coverageN)r   )thread_joinr   r   qsizer   loadsgetUnpicklingErrorr    r!   r   inject_coverage)r   r   r   datathread_coveredr
   r
   r   joinG   s   

zCoverageCollectingThread.joinN)__name__
__module____qualname__r   r%   r.   r
   r
   r
   r   r   &   s    r   c                   C   s"   t jtj_t jtj_t jtj_d S r   )r   r   r   Threadr%   r.   r
   r
   r
   r   _patch_threadingU   s   

r3   )__doc__r   queuer   r   ddtrace.internal.coverage.coder   ddtrace.internal.loggerr   r/   r    r2   r   r   r%   r   r.   r&   r	   r   r   r3   r
   r
   r
   r   <module>   s    /