o
    ^Zh                     @   s   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 ddlmZmZ dgZd	d
 ZeedoEeejej Zdd ZG dd deZdd Zdd ZdddZdS )    N)load)processutil)set_spawning_popen)Popen   )	reductionspawnr   c                 C   s    | |kpt j| t j|kS N)ospathnormcase)p1p2 r   ]/var/www/auris/lib/python3.10/site-packages/joblib/externals/loky/backend/popen_loky_win32.py_path_eq   s    r   _base_executablec                  G   s   | D ]}t | qd S r
   )_winapiCloseHandle)Zhandleshandler   r   r   _close_handles   s   r   c                   @   s   e Zd ZdZdZdd ZdS )r   a  
    Start a subprocess to run the code of a process object.

    We differ from cpython implementation with the way we handle environment
    variables, in order to be able to modify then in the child processes before
    importing any library, in order to control the number of threads in C-level
    threadpools.

    We also use the loky preparation data, in particular to handle main_module
    inits and the loky resource tracker.
    Zlokyc                 C   s  t |jt|dd}td d\}}t|d}tt	
 |d}t  }i t	j|j}trAt|tjrAtj |d< }tj|d< ddd |D }t|d	f}	zt||d d d
d|d d 	\}
}}}t| W n tyv   t|  w || _d | _|
| _t|
| _t| t| jt|f| _ t!|  zt"#||	 t"#||	 W t!d  nt!d  w W d    d S 1 sw   Y  d S )NZinit_main_moduleTr   )
parent_pidpipe_handle__PYVENV_LAUNCHER__ c                 s   s    | ]	}d | d V  qdS )"Nr   ).0xr   r   r   	<genexpr>Q   s    z!Popen.__init__.<locals>.<genexpr>wbF)$r	   Zget_preparation_data_namegetattrr   Z
CreatePipemsvcrtopen_osfhandleget_command_liner   getpidget_executableenvironenvWINENVr   sys
executabler   joinopenZCreateProcessr   BaseExceptionpid
returncodeZ_handleintsentinelr   ZFinalizer   	finalizerr   r   dump)selfZprocess_objZ	prep_dataZrhandleZwhandleZwfdcmdZ
python_exeZ	child_envZto_childhpZhtr0   _r   r   r   __init__7   sV   



"zPopen.__init__N)__name__
__module____qualname____doc__methodr:   r   r   r   r   r   (   s    c                 K   sL   t tddrtjd| gS d|  d| d}t }t g|d|dS )zAReturns prefix of command line used for spawning a child process.frozenF--multiprocessing-forkzRfrom joblib.externals.loky.backend.popen_loky_win32 import main; main(pipe_handle=z, parent_pid=)z-c)r"   r+   r,   r   _args_from_interpreter_flagsr	   r'   )r   r   kwdsprogoptsr   r   r   r%   x   s&   r%   c                 C   s    t | dkr| d dkrdS dS )z4Return whether commandline indicates we are forking.   r   rA   TF)len)argvr   r   r   
is_forking   s   rJ   c           
   	   C   s   t tjs	J d|durttjtjB d|}nd}tj| |d}t	
|tj}|}tj|ddd'}dt _zt|}t|| t|}W t `nt `w W d   n1 s]w   Y  ||}	t|	 dS )z.Run code specified by data received over pipe.zNot forkingNF)source_processrbT)closefd)rJ   r+   rI   r   ZOpenProcessZSYNCHRONIZEZPROCESS_DUP_HANDLEr   Z	duplicater#   r$   r   O_RDONLYfdopenr   current_processZ_inheritingr   r	   prepare
_bootstrapexit)
r   r   rK   Z
new_handlefdZparent_sentinelZfrom_parentZpreparation_datar6   exitcoder   r   r   main   s*   


	rV   r
   )r   r+   r#   r   pickler   multiprocessingr   r   Zmultiprocessing.contextr   Z!multiprocessing.popen_spawn_win32r   Z_Popen r   r	   __all__r   hasattrr,   r   r*   r   r%   rJ   rV   r   r   r   r   <module>   s&    P