o
    TZh[                     @   s@   d dl mZ ddlmZ eeZG dd dZG dd dZdS )	    )import_module   )
get_loggerc                   @   s   e Zd ZdZdddZdS )_PatchedModuleObjzMSet all the modules components as attributes of the _PatchedModuleObj object.Nc                 C   s`   |pg }|d ur |j D ]}||v s|dst| |t|| qt|tr+|j| _d S || _d S )N__)__dict__
startswithsetattrgetattr
isinstancer   _original_module)selfmoduleattrskey r   N/var/www/html/lang_env/lib/python3.10/site-packages/datasets/utils/patching.py__init__   s   
 z_PatchedModuleObj.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r   	   s    r   c                   @   sD   e Zd ZdZg ZddefddZdd Zdd	 Zd
d Z	dd Z
dS )patch_submoduleaF  
    Patch a submodule attribute of an object, by keeping all other submodules intact at all levels.

    Example::

        >>> import importlib
        >>> from datasets.load import dataset_module_factory
        >>> from datasets.streaming import patch_submodule, xjoin
        >>>
        >>> dataset_module = dataset_module_factory("snli")
        >>> snli_module = importlib.import_module(dataset_module.module_path)
        >>> patcher = patch_submodule(snli_module, "os.path.join", xjoin)
        >>> patcher.start()
        >>> assert snli_module.os.path.join is xjoin
    Ntargetc                 C   s6   || _ || _|| _|dd | _i | _|pg | _d S )N.r   )objr   newsplitr   originalr   )r   r   r   r   r   r   r   r   r   (   s   zpatch_submodule.__init__c           
      C   s  | j d^ }}tt|D ]t}ztd|d |d  }W n	 ty)   Y qw | j D ]S}t	| j|}||u sEt
|tr|j|u r|| j|< t| j|t|| jd t	| j|}||d d  D ]}t||tt	||d | jd t	||}qdt||| j q/q|rzt	td||}	W n ttfy   Y d S w | j D ]}t	| j||	u rt	| j|| j|< t| j|| j qd S |t d v rt d | | j|< t| j|| j d S td| d)Nr   r   )r   __builtins__zTried to patch attribute z instead of a submodule.)r   r   rangelenr   joinModuleNotFoundErrorr   __dir__r
   r   r   r   r   r	   r   r   AttributeErrorglobalsRuntimeError)
r   Z
submodulesZtarget_attri	submoduleattrZobj_attrZpatchedr   Z
attr_valuer   r   r   	__enter__0   sL   

zpatch_submodule.__enter__c                 G   s*   t | jD ]}t| j|| j| qd S r   )listr   r	   r   pop)r   exc_infor+   r   r   r   __exit__f   s   zpatch_submodule.__exit__c                 C   s   |    | j|  dS )zActivate a patch.N)r,   _active_patchesappendr   r   r   r   startj   s   zpatch_submodule.startc                 C   s,   z| j |  W |  S  ty   Y dS w )zStop an active patch.N)r1   remove
ValueErrorr0   r3   r   r   r   stopo   s   zpatch_submodule.stopr   )r   r   r   r   r1   strr   r,   r0   r4   r7   r   r   r   r   r      s    6r   N)	importlibr   loggingr   r   loggerr   r   r   r   r   r   <module>   s
    