o
    ٷi                     @   s   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mZ d dlm	Z	 d dl
mZ d dlmZ dZdZe	G dd	 d	ZG d
d dZdddddded dee dB deeef dB dedB dedefddZdS )    N)CallableSequence)	dataclass)StringIO)Anyiic                   @   s&   e Zd ZU eed< eed< eed< dS )CompletedProcess
returncodestdoutstderrN)__name__
__module____qualname__int__annotations__str r   r   a/home/ubuntu/.local/lib/python3.10/site-packages/cuda/pathfinder/_utils/spawned_process_runner.pyr      s   
 r   c                
   @   sL   e Zd Zdeded dee dB deeef dB ddf
dd	Zdd
dZ	dS )ChildProcessWrapperresult_queuetarget.NargsNkwargsreturnc                 C   s4   || _ |d u r	dn|| _|d u ri n|| _|| _d S )Nr   )r   r   r   r   )selfr   r   r   r   r   r   r   __init__   s   
zChildProcessWrapper.__init__c                 C   sT  t j}t j}t t _t t _zlz| j| ji | j d}W n+ ty: } zt|j	t
r.|j	nd}W Y d }~nd }~w tyH   t  d}Y nw W t j }t j }|t _|t _tt | j|||f W d    d S 1 stw   Y  d S t j }t j }|t _|t _tt | j|||f W d    w 1 sw   Y  w )Nr      )sysr	   r
   r   r   r   r   
SystemExit
isinstancecoder   BaseException	traceback	print_excgetvalue
contextlibsuppress	Exceptionr   put)r   
old_stdout
old_stderrr   er	   r
   r   r   r   __call__(   s:   "

"

zChildProcessWrapper.__call__)r   N)
r   r   r   r   r   r   dictr   r   r,   r   r   r   r   r      s    

r   F)r   r   timeoutrethrowr   r   r   r   r.   r/   r   c                C   sl  t d}| }|jt|| ||d}|  z{|| | r6|  |  t	t
dd| dd}n&z|jdd\}	}
}W n tjtfyT   t	tdd	d}Y nw t	|	|
|d}|rp|jd
krptd|j d|j d|W z
|  |  W n	 ty   Y nw | r|  |  S S z
|  |  W n	 ty   Y nw | r|  |  w w )a  Run `target` in a spawned child process, capturing stdout/stderr.

    The provided `target` must be defined at the top level of a module, and must
    be importable in the spawned child process. Lambdas, closures, or interactively
    defined functions (e.g., in Jupyter notebooks) will not work.

    If `rethrow=True` and the child process exits with a nonzero code,
    raises ChildProcessError with the captured stderr.
    spawn)r    zProcess timed out after z seconds and was terminated.)r   r	   r
   g      ?)r.   z3Process exited or crashed before returning results.r   zChild process exited with code z$.
--- stderr-from-child-process ---
z#<end-of-stderr-from-child-process>
)multiprocessingget_contextQueueProcessr   startjoinis_alive	terminater   PROCESS_KILLEDgetqueueEmptyEOFErrorPROCESS_NO_RESULTr   ChildProcessErrorr
   closejoin_threadr'   kill)r   r   r   r.   r/   ctxr   processresultr   r	   r
   r   r   r   run_in_spawned_child_processA   sn   






rG   )r%   r2   r<   r   r"   collections.abcr   r   dataclassesr   ior   typingr   r:   r?   r   r   r-   r   floatboolrG   r   r   r   r   <module>   s>   )
