
    e/                         d Z ddlmZ ddlZddl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ZddZdd	Zd
 Zd Zd Zy# e$ r eZY w xY w)a  
Magic Reload Library
Luke Campagnola   2010

Python reload function that actually works (the way you expect it to)
  - No re-importing necessary
  - Modules can be reloaded in any order
  - Replaces functions and methods with their updated code
  - Changes instances to use updated classes
  - Automatically decides which modules to update by comparing file modification times
 
Does NOT:
  - re-initialize exting instances, even if __init__ changes
  - update references to any module-level objects
    ie, this does not reload correctly:
        from module import someObject
        print someObject
    ..but you can use this instead: (this works even for the builtin reload)
        import module
        print module.someObject
    )print_functionN   )printExc)reloadc           	      
   g }g }i }t        t        j                  j                               D ]  \  }}t	        j
                  |      sd||<   "|dk(  rd||<   -t        |dd      d||<   @t        j                  j                  |j                        d   dvrd	d
t        |j                        z  f||<   | 3|j                  dt        |        | k7  rd	d|j                  d| f||<   t        j                  j                  |j                        d   dz   }||v rt        j                  j                  |      sd	d|z  f||<    t        |d|dz         }t        j                  j                  |      sd||<   Vt        j                  |      j                  t        j                  |      j                  kD  rd||<   |j!                  |       	 t#        ||       d||<    t        |      dkD  rt%        ddj1                  |      z        |S # t$        $ r[}	t'        d|z         |j!                  |j(                         d	dt+        j,                  t/        |	      |	      z  f||<   Y d}	~	Od}	~	ww xY w)a@  Automatically reload all modules whose __file__ begins with *prefix*.

    Skips reload if the file has not been updated (if .pyc is newer than .py)
    If *prefix* is None, then all loaded modules are checked.

    Returns a dictionary {moduleName: (reloaded, reason)} describing actions taken
    for each module.
    )Fznot a module__main__)Fzignored __main____file__N)Fzmodule has no __file__r   ).pyz.pycFz%s not a .py/pyc filezfile z not in prefix r   r
   z.py does not exist: %s
__cached__c)Fzcode has no pyc file to compare)Fz"code has not changed since compile)debug)TNz*Error while reloading module %s, skipping
zreload failed: %sz!Some modules failed to reload: %sz, )listsysmodulesitemsinspectismodulegetattrospathsplitextr	   strlenisfilestatst_mtimeappendr   	Exceptionr   __name__	tracebackformat_exception_onlytypejoin)
prefixr   failedchangedretmodNamemodpypycexcs
             2/usr/lib/python3/dist-packages/pyqtgraph/reload.py	reloadAllr.   (   s`    FG
CS[[..01 2j$2CLj 6CL 3
D)1<CL77CLL)!,OC!#:S=N#NOCL #,,|F"<"F!v#VWCLWWcll+A.6=ww~~b!!#;b#@ACL c<c2ww~~c"ECL773<  2772;#7#77HCL 	r	j3e$'CL]2jh 6{Q;dii>OOPPJ  	jBSHIMM#,,'!#69X9XY]^aYbdg9h#hiCL	js   H	J'AI==Jc           
         |rt        dt        |       z         | j                  j                         }t	        |        | j                  }t        | d      r| j                  |       |D ]V  }||   }|j                  |d      }||u s|"t        j                  |      rE|r.t        d| j                  |t        |      t        |      fz         t        |||       ||_        |t        j                  |      r:t        |||      }	|sd}
|	dkD  rd|	z  }
t        d| j                  d	||
       |rTt!        |t"              rD|j%                         }|j'                  |       t)        |      D ]  }|j+                  d        !|s%t!        |t,              s7|j/                  |       |D ]
  }||vs||=  Y y)
aa  Replacement for the builtin reload function:
    - Reloads the module as usual
    - Updates all old functions and class methods to use the new code
    - Updates all instances of each modified class to use the new class
    - Can update lists and dicts, but this is disabled by default
    - Requires that class and function names have not changed
    zReloading %s
__reload__Nz%  Updating class %s.%s (0x%x -> 0x%x) r    (and %d previous versions)z  Updating function .)printr   __dict__copyorig_reloadhasattrr0   getr   isclassr   idupdateClass__previous_reload_version__
isfunctionupdateFunction
isinstancer   r   extendrangepopdictupdate)moduler   listsdictsoldDictnewDictkoldnewdepthextralijs                 r-   r   r   n   s    ns6{*+ oo""$GooG v|$'"  ajkk!T"#:??3=RSUWX[U\^`ad^e@ffgS%(.1C+$"3U3E199EAEv5QRz#t,	AJJsO1X 
z#t,JJsO C<A7    c                 J   |j                   | _         |j                  | _        t        | d      r|j                  | _        |j                  | _        |g }| |v ry |j                  |        t        | d      rt        | j                  |||dz   |      }n|}|dk(  r| |_        |S )N__kwdefaultsr=   r   )rN   visitedr   )__code____defaults__r8   __kwdefaults____doc__r   r?   r=   )rL   rM   r   rN   rV   maxDepths         r-   r?   r?      s     <<CL''CsN# //++CK
g~NN3 s12!#"A"A3UZ[\U\fmn z*-'OrS   c                 |   t        j                  |       }|D ]  }	 t        ||       r/|j                  | u r!||_        |rt	        dt        |      z         nt        j                  |      r}t        ||       rq| |j                  v rc|j                  j                  |       }|j                  d | || fz   |j                  |dz   d  z   }	 ||_	        |rt	        dt        |      z          t        |       D ]  }t        | |      }t        j                   |      st        j"                  |      s:	 t        ||      }	t        |d|      }
t        |	d|	      }|
|usft'        |
||      }t)        |d      s||_        |sd}|dkD  rd|z  }t	        d||        t        |      D ]<  }|dk(  r	t)        | |      r|rt	        d|z         t-        | |t        ||             > t)        | d      rt/        | j0                  ||       y y # t        $ rH t	        d|z         t	        dt        |j                        z         t	        dt        |      z          w xY w# t        $ r2 t	        dt        |      dt        |       d	t        |      d
        w xY w# t$        $ r |rt	        d|z         Y w xY w)Nz    Changed class for %sr   z$    Error setting bases for class %sz        old bases: %sz        new bases: %sz    Changed superclass for %szError updating reference (z) for class change (z -> )zI    Skipping method update for %s; new class does not have this attribute__func____previous_reload_method__r1   r   r2   z    Updating method r=   z    Adding missing attribute %s)gcget_referrersr@   	__class__r4   safeStrr   r:   
issubclass	__bases__index	TypeErrorreprr   dirr   r>   ismethodAttributeErrorr?   r8   r_   setattrr<   r=   )rL   rM   r   refsrefindnewBasesattroanaofuncnfuncrN   rO   s                 r-   r<   r<      s   C D "!	#s#(< #4ws|CD%*S#*>3#--CWmm))#. ==#.#c:S]]3q56=RR$,CM 9GCLHI9"N C FS$b!W%5%5b%9S$' B
B/EB
B/EE!&ueU;u&BC79E4Eqy = EeDE/F4 C 300sD!7$>?CwsD123 s12C33S%@ 3i ! @3FG1D4GGH1DNBC	  	QXY\Q]_fgj_kmtuxmyz{	 " ehllms7   B,I 	HI 1JAII  ;JJ;:J;c                     	 t        |       }|S # t        $ rC 	 t        |       }Y |S # t        $ r' dt        t	        |             t        |       fz  }Y Y |S w xY ww xY w)Nz<instance of %s at 0x%x>)r   r   rh   rc   r"   r;   )objss     r-   rc   rc     su    KH H  K	KS	A H  	K*gd3i.@"S'-JJAH	KKs$    	A'+AAAAc                    t        | t              st        j                  |       rt	        | dd      S t        j
                  |       r| j                  t	        | j                  dd      S t	        | j                  dd      }|y| j                  }t	        |d|      }t        |d      r#|j                  }t        j                  |||      S t        j                  ||      S y)z\Return the previous version of *obj*, or None if this object has not
    been reloaded.
    r=   Nr_   r^   im_class)r@   r"   r   r>   r   rj   __self__r^   r8   rz   types
MethodType)rw   	oldmethodselfoldfuncclss        r-   getPreviousVersionr   (  s     #t 2 23 7s94@@			#	<<3<<)EtLL.JDQI <<DiY?Gy*-((''s;; ''66! 
rS   )NF)FFF)r   N)rZ   
__future__r   r`   r   r   r   r    r|   r   r   	importlibr   r7   ImportErrorr.   r?   r<   rc   r    rS   r-   <module>r      sh   , & 	  	 
   /
CL2rFNAf7I  Ks   A AA