a
    5h                     @  s   U 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mZm	Z	m
Z
 ddlZedZedeejZg Zded< d	d
 Ze
dddddZe
dddddZdddddZdddddZdd Zdd Zdd Zdd ZdS ) z
Monkey patching of distutils.
    )annotationsN)TypeVarcastoverload_T	_UnpatchTz	list[str]__all__c                 C  s"   t  dkr| f| j S t| S )am  
    Returns the bases classes for cls sorted by the MRO.

    Works around an issue on Jython where inspect.getmro will not return all
    base classes if multiple classes share the same name. Instead, this
    function will return a tuple containing the class itself, and the contents
    of cls.__bases__. See https://github.com/pypa/setuptools/issues/1024.
    ZJython)platformZpython_implementation	__bases__inspectZgetmro)cls r   0lib/python3.9/site-packages/setuptools/monkey.py_get_mro   s    	r   )itemreturnc                 C  s   d S Nr   r   r   r   r   get_unpatched(   s    r   objectNonec                 C  s   d S r   r   r   r   r   r   r   *   s    z"type | types.FunctionType | objectz type | types.FunctionType | Nonec                 C  s*   t | trt| S t | tjr&t| S d S r   )
isinstancetypeget_unpatched_classtypesFunctionTypeget_unpatched_functionr   r   r   r   r   ,   s
    
ztype[_T])r   r   c                 C  s<   dd t | D }t|}|jds8d| }t||S )zProtect against re-patching the distutils if reloaded

    Also ensures that no other distutils extension monkeypatched the distutils
    first.
    c                 s  s(   | ] }|j d sttt |V  qdS )
setuptoolsN)
__module__
startswithr   r   r   ).0r   r   r   r   	<genexpr><   s   z&get_unpatched_class.<locals>.<genexpr>	distutilsz&distutils has already been patched by )r   nextr   r   AssertionError)r   Zexternal_basesbasemsgr   r   r   r   6   s    
r   c                  C  sn   dd l } | jtj_t  tjtjtjfD ]}| jj|_q(| jj	tj_	| jj	tj_	dt
jv rj| jj	t
jd _	d S )Nr   zdistutils.command.build_ext)r   ZCommandr"   core_patch_distribution_metadatadistcmdZDistribution	extensionZ	Extensionsysmodules)r   moduler   r   r   	patch_allH   s    

r/   c                  C  s4   ddl m}  dD ]}t| |}ttjj|| qd S )N   )_core_metadata)Zwrite_pkg_infoZwrite_pkg_fileZread_pkg_fileZget_metadata_versionZget_fullname) r1   getattrsetattrr"   r)   ZDistributionMetadata)r1   attrnew_valr   r   r   r(   ]   s    
r(   c                 C  s*   t ||}t| d| t|||  dS )z
    Patch func_name in target_mod with replacement

    Important - original must be resolved by name to avoid
    patching an already patched function.
    	unpatchedN)r3   vars
setdefaultr4   )ZreplacementZ
target_modZ	func_nameZoriginalr   r   r   
patch_funcl   s    
r:   c                 C  s   | j S r   )r7   )	candidater   r   r   r   }   s    r   )__doc__Z
__future__r   r   r	   r,   r   typingr   r   r   Zdistutils.filelistr"   r   r   r   r   r   __annotations__r   r   r   r/   r(   r:   r   r   r   r   r   <module>   s*   
