
    %^g                        d Z ddlmZ ddlZ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  G d d      ZddZdd	Z G d
 de      ZddZddZ G d d      Zy)zBasic introspection of modules.    )annotationsN)Queueget_context)
ModuleTypec                  <    e Zd Z	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)ModulePropertiesNc                `    || _         || _        || _        || _        || _        |xs g | _        y Nnamefilepathallis_c_modulesubpackages)selfr   r   r   r   r   r   s          2lib/python3.12/site-packages/mypy/moduleinspect.py__init__zModuleProperties.__init__   s5     			&&,"    ) NNNFN)r   strr   
str | Noner   list[str] | Noner   r   r   boolr   r   returnNone)__name__
__module____qualname__r    r   r   r   r      s_     !% $!(,-- - 	-
 - - &- 
-r   r   c                    | j                   j                  d      yt        j                  j	                  | j                   d         d   dv S )N__file__T)z.soz.pydz.dll)__dict__getosr   splitext)modules    r   r   r   "   sE    :&. 77FOOJ78<@WWWr   c                    t        | xr6 | j                  d      xr# t        j                  j	                  | d d              S )Nz.pycr#   )r   endswithr&   r   exists)r   s    r   is_pyc_onlyr,   *   s6    Pv.Prww~~d3Bi7P3PQQr   c                      e Zd Zy)InspectErrorN)r   r   r   r    r   r   r.   r.   .   s    r   r.   c                   	 t        j                  |       }t        |d|       }t        |dd      }t        |dd      }t        |t              sd}t        |dd      }|	 t        |      }t        |      }|q|rlt        j                  |      D cg c]K  \  }}t        j                  |      r1|j                  |j                  dz   |z   k(  r|j                  dz   |z   M }	}}n@g }	n=t        j                  ||j                  dz   d       }
|
D cg c]  \  }}}|
 }	}}}t!        |||||		      S # t        $ r}t        t	        |            |d}~ww xY w# t        $ r d}Y w xY wc c}}w c c}}}w )
zDUse runtime introspection to get information about a module/package.Nr   r"   __path____all__.c                     y r
   r    )rs    r   <lambda>z(get_package_properties.<locals>.<lambda>U   s    4r   )prefixonerrorr   )	importlibimport_moduleBaseExceptionr.   r   getattr
isinstancelist	Exceptionr   inspect
getmembersismoduler   pkgutilwalk_packagesr   )
package_idpackageer   r   r   pkg_allis_cvalr   all_packagesimporterqualified_nameispkgs                 r   get_package_propertiesrN   2   s   *))*5 7J
3D7J-D$Wj$?DdD!gy$/G	7mG wD| 
 ")!3!3G!<!<ID###C(S\\W=M=MPS=SVZ=Z-Z   3&-!<   K,,))C/
 NZZ\*I(NE~\Z4W$T_ E  *3q6")*  	G	 [s6   D7  E AE.E47	E EEE+*E+c                    |t         _        	 | j                         }	 t        |      }|j                  |       .# t        $ r$}|j                  t        |             Y d}~Wd}~ww xY w)z0The main loop of a worker introspection process.N)sysr   r%   rN   r.   putr   )tasksresultssys_pathmodproprF   s         r   workerrW   ]   s^    CH
iik	)#.D 	D   	KKA	s   ; 	A(A##A(c                  H    e Zd ZdZd
dZd
dZd
dZddZddZddZ	ddZ
y	)ModuleInspecta9  Perform runtime introspection of modules in a separate process.

    Reuse the process for multiple modules for efficiency. However, if there is an
    error, retry using a fresh process to avoid cross-contamination of state between
    modules.

    We use a separate process to isolate us from many side effects. For example, the
    import of a module may kill the current process, and we want to recover from that.

    Always use in a with statement for proper clean-up:

      with ModuleInspect() as m:
          p = m.get_package_properties('urllib.parse')
    c                $    | j                          y r
   )_startr   s    r   r   zModuleInspect.__init__z   s    r   c                p   t         j                  dk(  rt        d      }nt        d      }|j                         | _        |j                         | _        |j                  t        | j                  | j
                  t         j                  f      | _	        | j                  j                          d| _        y )Nlinux
forkserverspawn)targetargsr   )rP   platformr   r   rR   rS   ProcessrW   r   procstartcounter)r   ctxs     r   r[   zModuleInspect._start}   sx    <<7"l+Cg&C!$
69iikKKvTZZsxx4XKY			r   c                8    | j                   j                          y)zFree any resources used.N)re   	terminater\   s    r   closezModuleInspect.close   s    		r   c                |   | j                   j                  |       | j                         }|| j                          t	        d|      t        |t              rK| j                  dkD  r1| j                          | j                          | j                  |      S t	        |      | xj                  dz  c_        |S )zReturn some properties of a module/package using runtime introspection.

        Raise InspectError if the target couldn't be imported.
        zProcess died when importing r      )
rR   rQ   _get_from_queuer[   r.   r<   r   rg   rk   rN   )r   rD   ress      r   rN   z$ModuleInspect.get_package_properties   s    
 	

z"""$;KKM!=j^LMMc3||a 

22:>>s##
r   c                    d}d}	 ||k(  rt        d      	 | j                  j                  d      S # t        j                  $ r | j
                  j                         sY yY nw xY w|dz  }h)z{Get value from the queue.

        Return the value read from the queue, or None if the process unexpectedly died.
        iX  r   zTimeout waiting for subprocessg?)timeoutNrm   )RuntimeErrorrS   r%   queueEmptyre   is_alive)r   max_iterns      r   rn   zModuleInspect._get_from_queue   s|    
 H}"#CDD ||'''55;;  yy))+ ,  FA s   3 -A%$A%c                    | S r
   r    r\   s    r   	__enter__zModuleInspect.__enter__   s    r   c                $    | j                          y r
   )rk   )r   rb   s     r   __exit__zModuleInspect.__exit__   s    

r   N)r   r   rD   r   r   r   )r   zModuleProperties | str | None)r   rY   )rb   objectr   r   )r   r   r   __doc__r   r[   rk   rN   rn   ry   r{   r    r   r   rY   rY   j   s*    	."r   rY   )r(   r   r   r   )r   r   r   r   r|   )rR   z
Queue[str]rS   zQueue[str | ModuleProperties]rT   z	list[str]r   r   )r~   
__future__r   r8   r?   r&   rB   rs   rP   multiprocessingr   r   typesr   r   r   r,   r>   r.   rN   rW   rY   r    r   r   <module>r      s\    % "   	   
 . - -&XR	9 	(V
N Nr   