o
    2g                     @  s   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 dZd#ddZd$ddZG dd deZd%ddZd&dd ZG d!d" d"ZdS )'zBasic introspection of modules.    )annotationsN)Queueget_context)
ModuleTypec                   @  s$   e Zd Z						ddddZdS )ModuleProperties NFnamestrfile
str | Nonepathlist[str] | Noneallis_c_moduleboolsubpackagesreturnNonec                 C  s,   || _ || _|| _|| _|| _|pg | _d S Nr   r
   r   r   r   r   )selfr   r
   r   r   r   r    r   J/home/garg/my-data/venv/lib/python3.10/site-packages/mypy/moduleinspect.py__init__   s   	zModuleProperties.__init__)r   NNNFN)r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname__r   r   r   r   r   r      s    r   moduler   r   r   c                 C  s.   | j dd u r
dS tj| j d d dv S )N__file__T)z.soz.pydz.dll)__dict__getosr   splitext)r   r   r   r   r   "   s   r   r
   r   c                 C  s(   t | o| dotj| d d  S )Nz.pycr   )r   endswithr"   r   exists)r
   r   r   r   is_pyc_only*   s   (r&   c                   @  s   e Zd ZdS )InspectErrorN)r   r   r   r   r   r   r   r'   .   s    r'   
package_idr	   c           	   
     s  zt |  W n ty } ztt||d}~ww t d| }t dd}t dd}t|ts4d}t dd}|durPzt|}W n tyO   d}Y nw t	 }|du rj|rg fddt
 D }ng }ntj| jd d	d
 d}dd |D }t||||||dS )zDUse runtime introspection to get information about a module/package.Nr   r   __path____all__c                   s<   g | ]\}}t |r|j jd  | kr jd  | qS ).)inspectismoduler   ).0r   valpackager   r   
<listcomp>K   s    z*get_package_properties.<locals>.<listcomp>r+   c                 S  s   d S r   r   )rr   r   r   <lambda>U   s    z(get_package_properties.<locals>.<lambda>)prefixonerrorc                 S  s   g | ]\}}}|qS r   r   )r.   importerqualified_nameispkgr   r   r   r2   W   s    r   )	importlibimport_moduleBaseExceptionr'   r	   getattr
isinstancelist	Exceptionr   r,   
getmemberspkgutilwalk_packagesr   r   )	r(   er   r
   r   pkg_allis_cr   all_packagesr   r0   r   get_package_properties2   s@   

rH   tasks
Queue[str]resultsQueue[str | ModuleProperties]sys_path	list[str]r   c              
   C  s\   |t _	 |  }zt|}W n ty' } z|t| W Y d}~qd}~ww || q)z0The main loop of a worker introspection process.TN)sysr   r!   rH   r'   putr	   )rI   rK   rM   modproprD   r   r   r   worker]   s   
rS   c                   @  sV   e Zd ZdZdddZdddZddd	ZdddZdddZdddZ	dddZ
dS )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')
    r   r   c                 C     |    d S r   )_startr   r   r   r   r   z      zModuleInspect.__init__c                 C  s`   t jdkr
td}ntd}| | _| | _|jt| j| jt jfd| _	| j	
  d| _d S )Nlinux
forkserverspawn)targetargsr   )rO   platformr   r   rI   rK   ProcessrS   r   procstartcounter)r   ctxr   r   r   rV   }   s   





zModuleInspect._startc                 C  s   | j   dS )zFree any resources used.N)r`   	terminaterW   r   r   r   close   s   zModuleInspect.closer(   r	   r   c                 C  sz   | j | |  }|du r|   td|t|tr4| jdkr0|   |   | 	|S t||  jd7  _|S )zReturn some properties of a module/package using runtime introspection.

        Raise InspectError if the target couldn't be imported.
        NzProcess died when importing r      )
rI   rP   _get_from_queuerV   r'   r>   r	   rb   re   rH   )r   r(   resr   r   r   rH      s   


z$ModuleInspect.get_package_propertiesModuleProperties | str | Nonec                 C  sZ   d}d}	 ||krt dz| jjddW S  tjy'   | j s%Y dS Y nw |d7 }q)	z{Get value from the queue.

        Return the value read from the queue, or None if the process unexpectedly died.
        iX  r   TzTimeout waiting for subprocessg?)timeoutNrf   )RuntimeErrorrK   r!   queueEmptyr`   is_alive)r   max_iternr   r   r   rg      s   
zModuleInspect._get_from_queuec                 C  s   | S r   r   rW   r   r   r   	__enter__   s   zModuleInspect.__enter__r]   objectc                 G  rU   r   )re   )r   r]   r   r   r   __exit__   rX   zModuleInspect.__exit__N)r   r   r(   r	   r   r   )r   ri   )r   rT   )r]   rr   r   r   )r   r   r   __doc__r   rV   re   rH   rg   rq   rs   r   r   r   r   rT   j   s    





rT   )r   r   r   r   )r
   r   r   r   rt   )rI   rJ   rK   rL   rM   rN   r   r   )ru   
__future__r   r:   r,   r"   rB   rl   rO   multiprocessingr   r   typesr   r   r   r&   r@   r'   rH   rS   rT   r   r   r   r   <module>   s"    



+