a
    G*èfõ  ã                   @  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
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)ÚTypeÚ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¡s6d|  }t|ƒ‚|S )z•Protect 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)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    

þÿr0   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)Ú r2   ÚgetattrÚsetattrr#   r*   ZDistributionMetadata)r2   ÚattrZ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)r4   ÚvarsÚ
setdefaultr5   )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   r   Zdistutils.filelistr#   r   r   r   r   r	   Ú__annotations__r   r   r   r0   r)   r:   r   r   r   r   r   Ú<module>   s*   
