o
    éT•jP$  ã                   @   s  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
 g d¢Zejdkr8d	Zd	Zneed
d	ƒZej ¡  d¡ZerQe j ejd¡anejadd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd'dd„Zdd„ Zdd„ Zdd„ Zg Z dd „ Z!d!d"„ Z"d#d$„ Z#d%d&„ Z$dS )(é    Né   )Úget_start_methodÚset_start_method)Úprocess)Ú	reduction)Úutil)Ú_mainÚfreeze_supportÚset_executableÚget_executableÚget_preparation_dataÚget_command_lineÚimport_main_pathÚwin32FÚfrozenzpythonservice.exez
python.exec                 C   s   | a d S ©N©Ú_python_exe)Úexe© r   ú,/usr/lib/python3.10/multiprocessing/spawn.pyr
   )   s   r
   c                   C   s   t S r   r   r   r   r   r   r   -   s   r   c                 C   s    t | ƒdkr| d dkrdS dS )z=
    Return whether commandline indicates we are forking
    é   r   ú--multiprocessing-forkTF)Úlen)Úargvr   r   r   Ú
is_forking4   s   r   c                  C   sl   t tjƒr4i } tjdd… D ]}| d¡\}}|dkr d| |< qt|ƒ| |< qtdi | ¤Ž t ¡  dS dS )zE
    Run code for process object if this in not the main process
    r   Nú=ÚNoner   )r   Úsysr   ÚsplitÚintÚ
spawn_mainÚexit)ÚkwdsÚargÚnameÚvaluer   r   r   r	   >   s   

÷r	   c                  K   sd   t tddƒrtjdgdd„ |  ¡ D ƒ S d}|d dd	„ |  ¡ D ƒ¡; }t ¡ }tg| d
|dg S )zJ
    Returns prefix of command line used for spawning a child process
    r   Fr   c                 S   s   g | ]}d | ‘qS )ú%s=%rr   ©Ú.0Úitemr   r   r   Ú
<listcomp>T   s    z$get_command_line.<locals>.<listcomp>z<from multiprocessing.spawn import spawn_main; spawn_main(%s)z, c                 s   s   � | ]}d | V  qdS )r'   Nr   r(   r   r   r   Ú	<genexpr>W   s   € z#get_command_line.<locals>.<genexpr>z-c)Úgetattrr   Ú
executableÚitemsÚjoinr   Ú_args_from_interpreter_flagsr   )r#   ÚprogÚoptsr   r   r   r   N   s   ÿr   c                 C   s¬   t tjƒs	J dƒ‚tjdkr9ddl}ddl}|dur&| |j|jB d|¡}nd}t	j
| |d�}| |tj¡}|}nddlm}	 ||	j_| }t | ¡}t||ƒ}
t |
¡ dS )	z7
    Run code specified by data received over pipe
    zNot forkingr   r   NF)Úsource_processr   )Úresource_tracker)r   r   r   ÚplatformÚmsvcrtÚ_winapiÚOpenProcessÚSYNCHRONIZEÚPROCESS_DUP_HANDLEr   Ú	duplicateÚopen_osfhandleÚosÚO_RDONLYÚ r5   Ú_resource_trackerÚ_fdÚdupr   r"   )Úpipe_handleÚ
parent_pidÚ
tracker_fdr7   r8   r4   Ú
new_handleÚfdÚparent_sentinelr5   Úexitcoder   r   r   r!   \   s*   

þÿ

r!   c              	   C   s|   t j| ddd��)}dt ¡ _ztj |¡}t|ƒ tj |¡}W t ¡ `nt ¡ `w W d   ƒ n1 s4w   Y  | 	|¡S )NÚrbT)Úclosefd)
r>   Úfdopenr   Úcurrent_processÚ_inheritingr   ÚpickleÚloadÚprepareÚ
_bootstrap)rH   rI   Úfrom_parentÚpreparation_dataÚselfr   r   r   r   x   s   
ù
r   c                   C   s   t t ¡ ddƒrtdƒ‚d S )NrO   Fa  
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.)r-   r   rN   ÚRuntimeErrorr   r   r   r   Ú_check_not_importing_main„   s   ÿrX   c                 C   s  t ƒ  ttjt ¡ jd�}tjdurtj ¡ |d< t	j
 ¡ }z| d¡}W n	 ty.   Y nw tj||< |j| |t	jtjt ¡ tƒ d� t	jd }t|jddƒ}|durZ||d< |S t	jd	ksctsˆtsˆt|d
dƒ}|durˆtj
 |¡s€tjdur€tj
 tj|¡}tj
 |¡|d< |S )zM
    Return info about parent needed by child to unpickle process object
    )Úlog_to_stderrÚauthkeyNÚ	log_levelr@   )r%   Úsys_pathÚsys_argvÚorig_dirÚdirÚstart_methodÚ__main__r%   Úinit_main_from_namer   Ú__file__Úinit_main_from_path)rX   Údictr   Ú_log_to_stderrr   rN   rZ   Ú_loggerÚgetEffectiveLevelr   ÚpathÚcopyÚindexÚ
ValueErrorÚORIGINAL_DIRÚupdater   r>   Úgetcwdr   Úmodulesr-   Ú__spec__r6   ÚWINEXEÚ
WINSERVICEÚisabsr0   Únormpath)r%   Údr\   ÚiÚmain_moduleÚmain_mod_nameÚ	main_pathr   r   r   r   –   sF   þ

ÿ
ú
	ø
r   c                 C   sö   d| v r| d t  ¡ _d| v r| d t  ¡ _d| v r"| d r"t ¡  d| v r/t ¡  | d ¡ d| v r8| d t_	d| v rA| d t_
d| v rLt | d ¡ d| v rU| d t _d	| v rat| d	 d
d� d| v rmt| d ƒ dS d| v ryt| d ƒ dS dS )zE
    Try to get current process ready to unpickle process object
    r%   rZ   rY   r[   r\   r]   r_   r^   r`   T)Úforcerb   rd   N)r   rN   r%   rZ   r   rY   Ú
get_loggerÚsetLevelr   ri   r   r>   Úchdirrm   r   Ú_fixup_main_from_nameÚ_fixup_main_from_path)Údatar   r   r   rR   Ê   s.   


ÿrR   c                 C   s~   t jd }| dks|  d¡rd S t|jdd ƒ| krd S t |¡ t d¡}t	j
| ddd�}|j |¡ | t jd< t jd< d S )Nra   z	.__main__r%   Ú__mp_main__T)Úrun_nameÚ	alter_sys)r   rp   Úendswithr-   rq   Úold_main_modulesÚappendÚtypesÚ
ModuleTypeÚrunpyÚ
run_moduleÚ__dict__rn   )Úmod_nameÚcurrent_mainrx   Úmain_contentr   r   r   r   ð   s   


þr   c                 C   sˆ   t jd }tj tj | ¡¡d }|dkrd S t|dd ƒ| kr!d S t |¡ t	 
d¡}tj| dd�}|j |¡ | t jd< t jd< d S )Nra   r   Úipythonrc   r‚   )rƒ   )r   rp   r>   ri   ÚsplitextÚbasenamer-   r†   r‡   rˆ   r‰   rŠ   Úrun_pathrŒ   rn   )rz   rŽ   Ú	main_namerx   r�   r   r   r   r€   	  s   


ÿr€   c                 C   s   t | ƒ dS )z<
    Set sys.modules['__main__'] to module at main_path
    N)r€   )rz   r   r   r   r   %  s   r   )NN)%r>   r   rŠ   rˆ   r@   r   r   r   Úcontextr   r   Ú__all__r6   rr   rs   r-   r.   Úlowerr…   ri   r0   Úexec_prefixr   r
   r   r   r	   r   r!   r   rX   r   r†   rR   r   r€   r   r   r   r   r   Ú<module>   s>   



2&