U
    
W[4$  ã                   @   sÔ   d Z ddlZddlZddlZddlZddlmZ zddlmZ W n ek
rT   Y nX ddl	m
Z
mZ ddlmZ e ¡ add„ ZG d	d
„ d
eƒZi Zdd„ Zdd„ ZG dd„ deƒZdd„ Zdd„ Zddd„ZdS )z&
*Real* reloading support for Python.
é    N)Úreload)ÚInstanceType)ÚlogÚreflect)Ú_PY3c                 C   s    t tddƒ}|dkrdS | |kS )a·  
    Compare to types.ClassType in a py2/3-compatible way

    Python 2 used comparison to types.ClassType to check for old-style
    classes Python 3 has no concept of old-style classes, so if
    ClassType doesn't exist, it can't be an old-style class - return
    False in that case.

    Note that the type() of new-style classes is NOT ClassType, and
    so this should return False for new-style classes in python 2
    as well.
    Z	ClassTypeNF)ÚgetattrÚtypes)ÚtZ
_ClassType© r
   ú8/usr/lib/python3/dist-packages/twisted/python/rebuild.pyÚ_isClassType   s    r   c                   @   s,   e Zd ZdZeZdd„ Zdd„ Zdd„ ZdS )	Ú	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                 C   s   | j t k }|S ©N)ÚlastRebuild)ÚselfZynr
   r
   r   ÚneedRebuildUpdate=   s    
zSensitive.needRebuildUpdatec                 C   s   t   ¡ | _d S r   )Útimer   ©r   r
   r
   r   ÚrebuildUpToDateB   s    zSensitive.rebuildUpToDatec                 C   sŠ   t |ƒ}|tjkrt|ƒS |tjkrL|jdkr<t|j|jƒS t|j|jƒS n:t	sh|t
krht|ddƒ |S t|ƒrxt|ƒS t d¡ |S dS )z¡
        Get the latest version of an object.

        This can handle just about anything callable; instances, functions,
        methods, and classes.
        NZnothingzwarning returning anObject!)Útyper   ÚFunctionTypeÚlatestFunctionÚ
MethodTypeÚ__self__r   Zim_classÚ__name__r   r   r   ÚlatestClassr   Úmsg)r   ZanObjectr	   r
   r
   r   ÚlatestVersionOfF   s    



zSensitive.latestVersionOfN)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r
   r
   r
   r   r   3   s
   r   c                 C   s,   t | jƒ}t |¡}|dkr | S t|| jƒS )z/
    Get the latest version of a function.
    N)ÚidÚ__globals__Ú_modDictIDMapÚgetr   r   )ZoldFuncZdictIDÚmoduler
   r
   r   r   a   s
    

r   c                 C   s˜   t  | j¡}t|| jƒ}dd„ |jD ƒ}zt|ƒ|_|W S  tk
r’   |jdkrZ| Y S t|ƒ}t	|ƒrvt|dtƒ}||jt|ƒt
|jƒƒ Y S X dS )z,
    Get the latest version of a class.
    c                 S   s   g | ]}t |ƒ‘qS r
   )r   )Ú.0Úbaser
   r
   r   Ú
<listcomp>u   s     zlatestClass.<locals>.<listcomp>)Z__builtin__ÚbuiltinsZ__metaclass__N)r   ZnamedModuler   r   r   Ú	__bases__ÚtupleÚ	TypeErrorr   r   ÚdictÚ__dict__)ZoldClassr%   ZnewClassZnewBasesZctorr
   r
   r   r   o   s    

ÿr   c                   @   s   e Zd ZdZdS )ÚRebuildErrorzT
    Exception raised when trying to rebuild a class whereas it's not possible.
    N)r   r   r   r    r
   r
   r
   r   r/   Š   s   r/   c                 C   s   t | jƒ| _dS )z,
    Updates an instance to be current.
    N)r   Ú	__class__r   r
   r
   r   ÚupdateInstance‘   s    r1   c                 C   s@   |dkrt dƒ‚t| ƒ t d t | j¡|¡¡ t| |ƒ}|S )z<
    A getattr method to cause a class to be refreshed.
    Ú__del__zWithout this, Python segfaults.z#(rebuilding stale {} instance ({})))	ÚAttributeErrorr1   r   r   Úformatr   Úqualr0   r   )r   ÚnameÚresultr
   r
   r   Ú__injectedgetattr__™   s    
 ÿ
r8   é   c              
   C   s€  t   ¡ at| dƒr | js tdƒ‚|r:t d t| j	ƒ¡¡ ddl
m} d|_| j}| tt|ƒ< i }i }i }i }|rˆt d t| j	ƒ¡¡ | ¡ D ]À\}}	tt|	ƒƒrÔ|	j| j	krÒd||	< |rÒtj d	¡ tj ¡  qt|	ƒtjkr|	j| jkrPd||	< |rPtj d
¡ tj ¡  qt|	tƒr|	j| j	krd||	< |rtj d¡ tj ¡  q| |¡ | |¡ |j}
| ¡ }| ¡ }| ¡ }|rªt d¡ t d t| j	ƒ¡¡ t| ƒ t  ¡  |rÖt d t| j	ƒ¡¡ |D ]j}t!| |j	ƒ|krt d t" #|¡¡¡ n:|r$tj d¡ tj ¡  d|_$|j %¡  t&|_'| j	|_qÚ|rTddl(}|D ]^}t!| |j	ƒ}||krŠt d t" #|¡¡¡ n*| )|¡D ]}t!|ddƒ|kr”||_*q”qX|rÞt d¡ t d t| j	ƒ¡¡ d}t+j, ¡ D ]`\}}|d }|| ksì|dkrqìt|dƒs&qìt|dƒr6qìd}|j ¡ D ]Ö\}}	zt-|	ƒ W n t.k
rt   Y qDY nX |
|	ƒrêtt|	ƒƒr´|rªtj d	¡ tj ¡  t/|	ƒ}n$|rÐtj d
¡ tj ¡  t0|	ƒ}d}t1|||ƒ n.tt|	ƒƒrD|	j$D ]}|
|ƒrþt/|	ƒ qþqD|rì|sì|d dkrìtj d¡ tj ¡  qìd|_|r|t d¡ t d t| j	ƒ¡¡ | S )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 {}...r   )Ú
componentsTz  (scanning {}): r9   ÚcÚfÚoÚ z  (reload   {})z  (cleaning {}): z)WARNING: class {} not replaced by reload!Úxr
   Nz-WARNING: new-class {} not replaced by reload!r0   z  (fixing   {}): Ú__file__Z
__bundle__é
   Ú.Fz   Rebuilt {}.)2r   r   Úhasattrr:   ÚRuntimeErrorr   r   r4   Ústrr   Útwisted.pythonr;   ZALLOW_DUPLICATESr.   r#   r!   Úitemsr   r   r   ZlogfileÚwriteÚflushr   r   r"   Ú
isinstanceÚupdateÚ__contains__Úkeysr   Ú	linecacheÚ
clearcacher   r   r5   r*   Úclearr8   Ú__getattr__ÚgcZget_referrersr0   ÚsysÚmodulesÚhashÚ	Exceptionr   r   Úsetattr)r%   ZdoLogr;   ÚdZ
newclassesÚclassesZ	functionsÚvaluesÚkÚvZfromOldModuleZclazzrS   ZnclassZgaÚrZmodcountZmkÚmodZchangedZnvr'   r
   r
   r   Úrebuild§   sâ    




ÿ


ÿ







r`   )r9   )r    rT   r   r   rO   Úimpr   r   ÚImportErrorrG   r   r   Ztwisted.python.compatr   r   r   Úobjectr   r#   r   r   rW   r/   r1   r8   r`   r
   r
   r
   r   Ú<module>   s*   ,