
    Ϫf                         U d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZ  ej                         a G d d      Zi Ze	eef   ed	<   d
 Zd Z G d de      Zd Zd ZddZy)z&
*Real* reloading support for Python.
    N)reload)
ModuleType)Dict)logreflectc                   &    e Zd ZdZeZd Zd Zd Zy)	Sensitivez
    A utility mixin that's sensitive to rebuilds.

    This is a mixin for classes (usually those which represent collections of
    callbacks) to make sure that their code is up-to-date before running.
    c                 ,    | j                   t         k  }|S N)lastRebuild)selfyns     8/usr/lib/python3/dist-packages/twisted/python/rebuild.pyneedRebuildUpdatezSensitive.needRebuildUpdate$   s    +	    c                 6    t        j                          | _        y r   )timer   r   s    r   rebuildUpToDatezSensitive.rebuildUpToDate(   s    99;r   c                 @   t        |      }|t        j                  k(  rt        |      S |t        j                  k(  rL|j
                   t        |j                  |j                        S t        |j
                  |j                        S t        j                  d       |S )z
        Get the latest version of an object.

        This can handle just about anything callable; instances, functions,
        methods, and classes.
        zwarning returning anObject!)typetypesFunctionTypelatestFunction
MethodType__self__getattrim_class__name__r   msg)r   anObjectts      r   latestVersionOfzSensitive.latestVersionOf+   s     N"""!(++%"""  (x00(2C2CDDx00(2C2CDDGG12Or   N)r   
__module____qualname____doc__r   r   r   r#    r   r   r	   r	      s     K'r   r	   _modDictIDMapc                     t        | j                        }t        j                  |      }|| S t	        || j
                        S )z/
    Get the latest version of a function.
    )id__globals__r(   getr   r   )oldFuncdictIDmodules      r   r   r   B   sA     ##$Fv&F~67++,,r   c                    t        j                  | j                        }t        || j                        }|j
                  D cg c]  }t        |       }}|j                  dk(  r|S 	 t        |      |_        |S c c}w # t        $ r> t        |      } ||j                  t        |      t        |j                              cY S w xY w)z,
    Get the latest version of a class.
    builtins)r   namedModuler$   r   r   	__bases__latestClasstuple	TypeErrorr   dict__dict__)oldClassr/   newClassbasenewBasesctors         r   r4   r4   O   s       !4!45Fvx001H.6.@.@AdD!AHAj(Q"8_ B  QH~H%%uXX=N=N8OPPQs   A<*B ACCc                       e Zd ZdZy)RebuildErrorzT
    Exception raised when trying to rebuild a class whereas it's not possible.
    N)r   r$   r%   r&   r'   r   r   r?   r?   d   s    r   r?   c                 8    t        | j                        | _        y)z,
    Updates an instance to be current.
    N)r4   	__class__r   s    r   updateInstancerB   j   s     !0DNr   c                     |dk(  rt        d      t        |        t        j                  dt	        j
                  | j                         d| d       t        | |      }|S )z<
    A getattr method to cause a class to be refreshed.
    __del__zWithout this, Python segfaults.z(rebuilding stale z instance (z)))AttributeErrorrB   r   r    r   qualrA   r   )r   nameresults      r   __injectedgetattr__rI   q   s[     y>??4GG dnn!= >k$rRST4 FMr   c                    t        j                          at        | d      r| j                  st	        d      |r,t        j                  dt        | j                         d       ddl	m
} d|_        | j                  }| t        t        |      <   i }i }i }i }|r,t        j                  dt        | j                         d	       |j                         D ]  \  }}	t!        t#        |	      t$        j&                        r_|	j(                  | j                  u sBd
||	<   |sJt
        j*                  j-                  d       t
        j*                  j/                          t1        |	t"              s|	j2                  | j                  k(  sd
||	<   |st
        j*                  j-                  d       t
        j*                  j/                           |j5                  |       |j5                  |       |j6                  }
|j9                         }|j9                         }|j9                         }|rAt        j                  d       t        j                  dt        | j                         d       t;        |        t=        j>                          |r,t        j                  dt        | j                         d	       |D ]  }tA        | |j                        |u r-t        j                  dtC        jD                  |       d       H|r=t
        j*                  j-                  d       t
        j*                  j/                          d|_#        |j                  jI                          tJ        |_&        | j                  |_         |rddl'}|D ]  }tA        | |j                        }||u r8t        j                  djQ                  tC        jD                  |                   UjS                  |      D ]  }tA        |dd      |u s||_*          |rAt        j                  d       t        j                  dt        | j                         d	       d}tV        jX                  j                         D ]  \  }}|d
z   }|| k(  s|t        |d      s!t        |d      r.d}|j                  j                         D ]s  \  }}		 t[        |	        |
|	      s|r=t
        j*                  j-                  d       t
        j*                  j/                          t_        |	      }d
}ta        |||       u |s|r|dz  dk(  st
        j*                  j-                  d       t
        j*                  j/                           d|_        |rAt        j                  d       t        j                  dt        | j                         d       | S # t\        $ r Y w xY w)zO
    Reload a module and do as much as possible to replace its references.
    ALLOW_TWISTED_REBUILDzI am not allowed to be rebuilt.zRebuilding z...r   )
componentsTz  (scanning z):    fo z  (reload   )z  (cleaning zWARNING: class z not replaced by reload!xr'   Nz-WARNING: new-class {} not replaced by reload!rA   z  (fixing   __file__
__bundle__
   .Fz   Rebuilt )1r   r   hasattrrK   RuntimeErrorr   r    strr   twisted.pythonrL   ALLOW_DUPLICATESr8   r(   r*   items
issubclassr   r   r   r+   logfilewriteflush
isinstancer$   update__contains__keysr   	linecache
clearcacher   r   rF   r3   clearrI   __getattr__gcformatget_referrersrA   sysmoduleshash	Exceptionr   setattr)r/   doLogrL   d
newclassesclasses	functionsvalueskvfromOldModuleclazzri   nclassgarmodcountmkmodchangednvs                        r   rebuildr   }   s   
 ))+Kv./++@AA+c&//23378 *"&JA!M"Q%JGIF,s6??34C89	 (1d1gu112}}/ 	!KK%%c*KK%%'4 ||v. !
1KK%%c*KK%%'( MM'
MM)''M"JllnG I,s6??34A67 6N,s6??34C89 
/65>>*e3GGogll5&9%::RST!!#&!!# EONN  " 3E%E
/  %VV__-<GG?FFLL( %%f- %1k40F:"$AK%% ,s6??34C89H;;$$&  Ca<&=CKsJ'3% LL&&( 	$DAqQ QKK%%c*KK%%'#A&Q#	$ x"}&:KKc"KK? B #(J+c&//23156M#  s   W	WW)rM   )r&   re   rl   r   r   	importlibr   r   typingr   rZ   r   r   r   r	   r(   int__annotations__r   r4   ro   r?   rB   rI   r   r'   r   r   <module>r      s|           (diik" "J (*tCO$ )
-Q*9 1	}r   