
    bi[                     T    d dl mZ ddlmZ  ee      Z G d d      Z G d d      Zy)	    )import_module   )
get_loggerc                       e Zd ZdZddZy)_PatchedModuleObjzMSet all the modules components as attributes of the _PatchedModuleObj object.Nc           	          |xs g }|>|j                   D ]/  }||v s|j                  d      rt        | |t        ||             1 t	        |t
              r|j                  | _        y || _        y )N__)__dict__
startswithsetattrgetattr
isinstancer   _original_module)selfmoduleattrskeys       R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/datasets/utils/patching.py__init__z_PatchedModuleObj.__init__   sl     =%<s~~d';D#wvs';<= <FfN_;` 7 7fl    N)__name__
__module____qualname____doc__r    r   r   r   r   	   s    Wmr   r   c                   :    e Zd ZdZg Zd	defdZd Zd Zd Z	d Z
y)
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                     || _         || _        || _        |j                  d      d   | _        i | _        |xs g | _        y )N.r   )objr   newsplitr   originalr   )r   r"   r   r#   r   s        r   r   zpatch_submodule.__init__(   s>    <<$Q'[b
r   c                    | j                   j                  d      ^ }}t        t        |            D ]+  }	 t	        dj                  |d |dz                }| j                  j                         D ]  }t        | j                  |      }||u s t        |t              s.|j                  |u s=|| j                  |<   t        | j                  |t        || j                               t        | j                  |      }||dz   d  D ];  }t        ||t        t        ||d       | j                               t        ||      }= t        ||| j                           . |r	 t        t	        dj                  |            |      }	| j                  j                         D ]_  }t        | j                  |      |	u st        | j                  |      | j                  |<   t        | j                  || j                          a y |t%               d   v r?t%               d   |   | j                  |<   t        | j                  || j                          y t'        d| d      # t        $ r Y <w xY w# t"        t        f$ r Y y w xY w)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   
submodulestarget_attri	submoduleattrobj_attrpatchedr   
attr_values
             r   	__enter__zpatch_submodule.__enter__0   sH   #';;#4#4S#9 [ s:' 	<A)#((:gA3F*GH	 ((* <"488T2 y(x):;@Y@Y]f@f*2DMM$'DHHd,=hdjj,YZ%dhh5G)!a%'2 8.?QTVZ@[cgcmcm.no")'3"78 G[$((;!<	<: $]388J3G%H+V
 ((* 6 488T*j8*1$((D*ADMM$'DHHdDHH56 GIn55)0>)B;)ODMM+&DHHk4884!:;-G_`aaU ' : #$78 s#    I#%$I3 #	I0/I03JJc                     t        | j                        D ]2  }t        | j                  || j                  j	                  |             4 y r   )listr%   r   r"   pop)r   exc_infor4   s      r   __exit__zpatch_submodule.__exit__f   s9    ' 	=DDHHdDMM$5$5d$;<	=r   c                 Z    | j                          | j                  j                  |        y)zActivate a patch.N)r8   _active_patchesappendr   s    r   startzpatch_submodule.startj   s     ##D)r   c                 x    	 | j                   j                  |        | j                         S # t        $ r Y yw xY w)zStop an active patch.N)r?   remove
ValueErrorr=   rA   s    r   stopzpatch_submodule.stopo   s=    	  ''-
 }}	  		s   - 	99r   )r   r   r   r   r?   strr   r8   r=   rB   rF   r   r   r   r   r      s0      O!C !4bl=*
r   r   N)	importlibr   loggingr   r   loggerr   r   r   r   r   <module>rK      s/    #  
H		m 	mb br   