o
    ¯bt  ã                   @   s   G d d„ dƒZ dS )c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚMonkeyPatcherzn
    Cover up attributes with new objects. Neat for monkey-patching things for
    unit-testing purposes.
    c                 G   s$   g | _ g | _|D ]}| j|Ž  qd S )N)Ú_patchesToApplyÚ
_originalsÚaddPatch)ÚselfÚpatchesÚpatch© r   ú7/usr/lib/python3/dist-packages/twisted/python/monkey.pyÚ__init__   s
   ÿzMonkeyPatcher.__init__c                 C   s   | j  |||f¡ dS )zæ
        Add a patch so that the attribute C{name} on C{obj} will be assigned to
        C{value} when C{patch} is called or during C{runWithPatches}.

        You can restore the original values with a call to restore().
        N)r   Úappend©r   ÚobjÚnameÚvaluer   r   r	   r      s   zMonkeyPatcher.addPatchc                 C   s,   | j D ]\}}}||f||fkr dS qdS )zc
        Has the C{name} attribute of C{obj} already been patched by this
        patcher?
        TF)r   )r   r   r   ÚoÚnÚvr   r   r	   Ú_alreadyPatched   s
   ÿzMonkeyPatcher._alreadyPatchedc                 C   sF   | j D ]\}}}|  ||¡s| j ||t||ƒf¡ t|||ƒ qdS )z†
        Apply all of the patches that have been specified with L{addPatch}.
        Reverse this operation using L{restore}.
        N)r   r   r   r   ÚgetattrÚsetattrr   r   r   r	   r   )   s
   ýzMonkeyPatcher.patchc                 C   s0   | j r| j  ¡ \}}}t|||ƒ | j sdS dS )zE
        Restore all original values to any patched objects.
        N)r   Úpopr   r   r   r   r	   Úrestore3   s   þzMonkeyPatcher.restorec                 O   s,   |   ¡  z||i |¤ŽW |  ¡  S |  ¡  w )z“
        Apply each patch already specified. Then run the function f with the
        given args and kwargs. Restore everything when done.
        )r   r   )r   ÚfÚargsÚkwr   r   r	   ÚrunWithPatches;   s   zMonkeyPatcher.runWithPatchesN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r   r   r   r   r   r	   r      s    		

r   N)r   r   r   r   r	   Ú<module>   s   