o
    V۷i'                     @   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 d dlZej	
ej	edZdd Zedkr]eejdkr]ejd	 d
kr]ejd dkZejee dd dd Zee e  d dlZd dlZd dlmZ d dlm  mZ d dlZd dl m!a!m"Z"m#Z# d dl$m%Z& da'da(g da)dd Z*dd Z+G dd da,t,t)d  t'a-G dd dt!a.t. a/ea0G dd dZ1dd  Z2e 3e2e d!d" Z4d#d$ Z5d%d& Z6d'd( Z7d)d* Z8d+d, Z9edkre5d-d. e5d/d. e6  e7  e8  e9  dS dS )0    N)suppress)BytesIOzsession-refimported-%s.pklc                 C   s<   dd l }|  d ddt| d }d|| f S )Nr   z[obj][]z(while testing (with refimported=%s):  %s)	traceback
format_exc
splitlinesreplacereprlstrip)errorobjrefimportedr   line r   M/home/ubuntu/vllm_env/lib/python3.10/site-packages/dill/tests/test_session.py_error_line   s   $r   __main__      --child   Truemodulec              
   C   s   zUdD ]}t  | jtjv sJ qdtjv rdtjv sJ dd l}dd l}dD ]}t  | tjd j| u s8J q'tjj	dksAJ | rKtj|ju sKJ tj
|ju sSJ W d S  tyj } z	t||| f|_ d }~ww )Njsonurl	local_modsaxdomcalendarcmathr   )Calendarisleap)globals__name__sysmodulesr"   r#   __dict__r   day_name
__module__complex_loglogAssertionErrorr   args)r   r   r"   r#   r   r   r   r   test_modules!   s     r1   )r    )r$   r%   r+   )r.      )AliceBobCarolc                 C      | d S )Nr   r   xr   r   r   squaredR       r9   c                 C   r6   )Nr   r   r7   r   r   r   <lambda>S   r:   r;   c                   @      e Zd Zdd ZdS )Personc                 C   s   || _ || _d S N)nameage)selfr?   r@   r   r   r   __init__U   s   
zPerson.__init__N)r'   r,   __qualname__rB   r   r   r   r   r=   T       r=   c                   @   r<   )CalendarSubclassc                 C   s   dd |   D S )Nc                 S   s   g | ]}t | qS r   )r+   ).0ir   r   r   
<listcomp>[   s    z-CalendarSubclass.weekdays.<locals>.<listcomp>)iterweekdaysrA   r   r   r   weekdaysZ   s   zCalendarSubclass.weekdaysN)r'   r,   rC   rK   r   r   r   r   rE   Y   rD   rE   c                   @   s.   e Zd Ze  Zdd Zdd Zdd ZdS )TestNamespacec                 K   s
   || _ d S r>   )extra)rA   rM   r   r   r   rB   b   s   
zTestNamespace.__init__c                 C   s6   t   | _t    t  | j t  | j | S r>   )r&   copybackupclearupdatetest_globalsrM   rJ   r   r   r   	__enter__d   s
   
zTestNamespace.__enter__c                 G   s   t    t  | j d S r>   )r&   rP   rQ   rO   )rA   exc_infor   r   r   __exit__j   s   
zTestNamespace.__exit__N)	r'   r,   rC   r&   rN   rR   rB   rS   rU   r   r   r   r   rL   `   s
    
rL   c              	   C   s   | j ddd d }t| dr| jn|}tjtj| j d}tj|ftj	|ffD ]\}}t
t || W d    n1 sBw   Y  q+d S )N.r   r   z.pyc
__cached____pycache__)__file__splithasattrrW   ospathjoindirnameremove
removedirsr   OSError)r   cachedpycacher`   filer   r   r   _clean_up_cachen   s   

rf   c              
   C   s$  z|t j}dD ]}t | j|| jksJ qdD ]}|| || ks$J qdD ]}|| j|u s2J || d|| dks@J q'tjt jksIJ tttsPJ tj	|d j	ksZJ t
ttsaJ tttshJ t |d  kstJ tt u szJ W d S  ty } z	t|||f|_ d }~ww )Nr   )r8   emptynames)r9   cubedr   personcal)r   r*   r&   r'   __globals__r=   r,   
isinstancerj   r@   
issubclassrE   r$   rk   rK   selfrefr/   r   r0   )mainglobals_copyr   	main_dictr   r   r   r   r   _test_objectsx   s*   rs   c           	      C   sR  i }| rddl m} ||d< tdi |}zDtjt|  | d ddlm}m}m	} |j
|tdt| g|d}|r>t | W tt tt|   W d   n1 sUw   Y  ntt tt|   W d   w 1 sqw   Y  w t }tj|| d |d tj|d	d
 |jd t|j|  W d   dS 1 sw   Y  dS )zNtest dump/load_module() for __main__, both in this process and in a subprocessr   )flagsrt   r   )pythonshellspr   )rw   Nr   r   rs   r   )r(   rt   rL   dilldump_modulesession_filedill.tests.__main__rv   rw   rx   callrY   strexitr   rb   r\   r`   r   seekload_modulerO   r   )	r   extra_objectsrt   nsrv   rw   rx   r   session_bufferr   r   r   test_session_main   s0   

 
"r   c                     s   ddl  tt    _dd  j D } t }t	|  | D ]} j|= q"|
d t|  t fdd| D sAJ  j u sHJ dS )z8test dump/load_module() for a module other than __main__r   Nc                 S   s   g | ]	}| d s|qS )__)
startswithrF   r   r   r   r   rH      s    z&test_session_other.<locals>.<listcomp>c                 3   s    | ]}| j v V  qd S r>   )r*   r   r   r   r   	<genexpr>   s    z%test_session_other.<locals>.<genexpr>)test_classdefatexitregisterrf   ro   r*   keysr   ry   rz   r   r   all)dict_objectsr   r   r   r   r   test_session_other   s   

r   c                  C   s  ddl m}  d}| |}d|_tj|}||ur&td|j|j|j	t
jd t }tj||dd | }| |}tjt||d	}|d u sHJ |j|ksOJ |jdksVJ |t
j vs_J |d tt|}|j|ksrJ |jdksyJ |t
j vsJ d S )
Nr   )
ModuleType__runtime__*   z:There are objects to save by referenece that shouldn't be:)re   T)r   r   r   )typesr   r8   ry   session_stash_modulesprint__dill_imported__dill_imported_as__dill_imported_top_levelr(   stderrr   rz   getvaluer   r'   r)   valuesr   )r   modnameruntimemodr   session_dump
return_valr   r   r   test_runtime_module   s0   
r   c                  C   s   dd l } dd l}dd l}dd l}|d }tjd< |jjddt	_
| j|_|j|_t	j
|_t }t	j||dd |d t	|}tjd= t|jh dksRJ d S )Nr   __test__r   )max_workersTru   >   ry   executorthread_exectypingAsyncContextManagerAsyncCMcollectionsUserDictDict)r   concurrent.futuresr   r   r   r(   r)   futuresThreadPoolExecutorry   r   r   r   r   r   r   r   rz   r   loadsetr   )r   
concurrentr   r   r   r   r   r   r   test_refimported_imported_as   s   

r   c                  C   s   t  _ t } t|  d aabt  }| 	d t
| }|t us'J t |ks.J |d dks6J |d tks>J |d tusFJ |d tksNJ d|vsTJ d|v sZJ W d    d S 1 sew   Y  d S )Nr   r'   r   rh   r8   yrg   )rL   r   ry   rz   r8   r   rg   r&   rN   r   load_module_asdictrh   )r   globals_state	main_varsr   r   r   test_load_module_asdict   s"   



"r   Fru   T):r   r\   r(   r   
contextlibr   ior   ry   r]   r^   r_   rY   r{   r   r'   lenargvr   r   r1   r   r   urllibr   xmlr    xml.dom.minidomr!   minidomtest_dictviewsr   r"   r$   r%   r+   r#   r.   r-   r8   rg   rh   r9   ri   r=   rj   rE   rk   ro   rL   rf   r   rs   r   r   r   r   r   r   r   r   r   <module>   sb   $	"


