Format: application/x-python-code
⬇ Download File (18.1 KB)
   1  o
   2  
   3  ��h�[�@sLdZddlmZddlmZmZmZdZddlZddl	Z	ddl
   4  Z
   5  ddlZddlZddl
   6  Z
   7  ddlZddlmZmZddlmZGdd	�d	�Zgd
   8  �Zdd�Zd
   9  d�Zdd�Zdd�Zdd�Zdd�efdd�efdd�efgZe�dd�dd�fg�dd�ZGdd�d�Zgd�Zd)d!d"�Z eddfd#d$�Z!eGd%d&�d&e��Z"d'd(�Z#dS)*a�IPython extension to reload modules before executing user code.
  10  
  11  ``autoreload`` reloads modules automatically before entering the execution of
  12  code typed at the IPython prompt.
  13  
  14  This makes for example the following workflow possible:
  15  
  16  .. sourcecode:: ipython
  17  
  18     In [1]: %load_ext autoreload
  19  
  20     In [2]: %autoreload 2
  21  
  22     In [3]: from foo import some_function
  23  
  24     In [4]: some_function()
  25     Out[4]: 42
  26  
  27     In [5]: # open foo.py in an editor and change some_function to return 43
  28  
  29     In [6]: some_function()
  30     Out[6]: 43
  31  
  32  The module was reloaded without reloading it explicitly, and the object
  33  imported with ``from foo import ...`` was also updated.
  34  
  35  Usage
  36  =====
  37  
  38  The following magic commands are provided:
  39  
  40  ``%autoreload``, ``%autoreload now``
  41  
  42      Reload all modules (except those excluded by ``%aimport``)
  43      automatically now.
  44  
  45  ``%autoreload 0``, ``%autoreload off``
  46  
  47      Disable automatic reloading.
  48  
  49  ``%autoreload 1``, ``%autoreload explicit``
  50  
  51      Reload all modules imported with ``%aimport`` every time before
  52      executing the Python code typed.
  53  
  54  ``%autoreload 2``, ``%autoreload all``
  55  
  56      Reload all modules (except those excluded by ``%aimport``) every
  57      time before executing the Python code typed.
  58  
  59  ``%autoreload 3``, ``%autoreload complete``
  60  
  61      Same as 2/all, but also adds any new objects in the module. See
  62      unit test at IPython/extensions/tests/test_autoreload.py::test_autoload_newly_added_objects
  63  
  64    Adding ``--print`` or ``-p`` to the ``%autoreload`` line will print autoreload activity to
  65    standard out. ``--log`` or ``-l`` will do it to the log at INFO level; both can be used
  66    simultaneously.
  67  
  68  ``%aimport``
  69  
  70      List modules which are to be automatically imported or not to be imported.
  71  
  72  ``%aimport foo``
  73  
  74      Import module 'foo' and mark it to be autoreloaded for ``%autoreload 1``
  75  
  76  ``%aimport foo, bar``
  77  
  78      Import modules 'foo', 'bar' and mark them to be autoreloaded for ``%autoreload 1``
  79  
  80  ``%aimport -foo``
  81  
  82      Mark module 'foo' to not be autoreloaded.
  83  
  84  Caveats
  85  =======
  86  
  87  Reloading Python modules in a reliable way is in general difficult,
  88  and unexpected things may occur. ``%autoreload`` tries to work around
  89  common pitfalls by replacing function code objects and parts of
  90  classes previously in the module with new versions. This makes the
  91  following things to work:
  92  
  93  - Functions and classes imported via 'from xxx import foo' are upgraded
  94    to new versions when 'xxx' is reloaded.
  95  
  96  - Methods and properties of classes are upgraded on reload, so that
  97    calling 'c.foo()' on an object 'c' created before the reload causes
  98    the new code for 'foo' to be executed.
  99  
 100  Some of the known remaining caveats are:
 101  
 102  - Replacing code objects does not always succeed: changing a @property
 103    in a class to an ordinary method or a method to a member variable
 104    can cause problems (but in old objects only).
 105  
 106  - Functions that are removed (eg. via monkey-patching) from a module
 107    before it is reloaded are not upgraded.
 108  
 109  - C extension modules cannot be reloaded, and so cannot be autoreloaded.
 110  
 111  - While comparing Enum and Flag, the 'is' Identity Operator is used (even in the case '==' has been used (Similar to the 'None' keyword)).
 112  
 113  - Reloading a module, or importing the same module by a different name, creates new Enums. These may look the same, but are not.
 114  �)�magic_arguments)�Magics�magics_class�
 115  line_magicTN)�
 116  import_module�reload)�source_from_cachec@sReZdZdZ	dZ	dZ	ddd�Zdd�Zdd	�Zd
 117  d�Z	dd
 118  �Z
 119  ddd�ZdS)�ModuleReloaderFTNcCsFi|_i|_i|_i|_i|_||_dd�|_|jddd�d|_dS)NcS�dS�N���msgrr�Sc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/extensions/autoreload.py�<lambda>��z)ModuleReloader.__init__.<locals>.<lambda>TF)�	check_all�	do_reload)	�failed�modules�skip_modules�old_objects�modules_mtimes�shell�_report�check�hide_errors)�selfrrrr�__init__�s
 120  
 121  zModuleReloader.__init__cC�.z|j|=Wn	tyYnwd|j|<dS)z-Skip reloading the named module in the futureTN)r�KeyErrorr�r�module_namerrr�mark_module_skipped���z"ModuleReloader.mark_module_skippedcCr)z9Reload the named module in the future (if it is imported)TN)rr rr!rrr�mark_module_reloadable�r$z%ModuleReloader.mark_module_reloadablecCs2|�|�t|�|�d�d}tj|}||fS)z�Import a module, and mark it reloadable
 122  
 123          Returns
 124          -------
 125          top_module : module
 126              The imported module if it is top-level, or the top-level
 127          top_name : module
 128              Name of top_module
 129  
 130          �.r)r%r�split�sysr)rr"�top_name�
 131  top_modulerrr�aimport_module�s
 132  
 133  
 134  zModuleReloader.aimport_modulecCs�t|d�r
 135  |jdurdSt|dd�dvrdS|j}tj�|�\}}|��dkr*|}nzt|�}Wn
 136  ty:YdSwzt�	|�j
 137  }W||fStyPYdSw)N�__file__)NN�__name__)N�__mp_main__�__main__z.py)�hasattrr,�getattr�os�path�splitext�lowerr�
 138  ValueError�stat�st_mtime�OSError)r�module�filenamer3�ext�py_filename�pymtimerrr�filename_and_mtime�s&���z!ModuleReloader.filename_and_mtimecCsT|js|sdS|s|jrttj���}nt|j���}|D]�}tj�|d�}||jvr,q|�|�\}}|dur8qz||j	|krBWqWnt
 139  yQ||j	|<Yqw|j�|d�|kr\q||j	|<|r�|�d|�d��z|j
 140  rzt|t|j|j�nt|t|j�||jvr�|j|=Wq|js�td�|t�d��tjd�||j|<YqqdS)z/Check whether some modules need to be reloaded.NzReloading 'z'.z[autoreload of {} failed: {}]�
 141  )�file)�enabledr�listr(r�keys�getrr?rr rr�autoload_obj�superreloadrrrr�print�format�	traceback�
 142  format_exc�stderr)rrrr�modname�mr=r>rrrr�sV
 143  
 144  
 145  �
 146  �
 147  
 148  �
 149  ����zModuleReloader.checkr)FT)r-�
 150  __module__�__qualname__rBrrFrr#r%r+r?rrrrrr	�s
 151  r	)�__code__�__defaults__�__doc__�__closure__�__globals__�__dict__c
 152  Cs:tD]}zt||t||��WqttfyYqwdS)z%Upgrade the code object of a functionN)�
 153  func_attrs�setattrr1�AttributeError�	TypeError)�old�new�namerrr�update_function1s��r^cCs2t�|�}|D]}t|�|urt�|d|�qdS)z�Use garbage collector to find all instances that refer to the old
 154      class definition and update their __class__ to point to the new class
 155      definition�	__class__N)�gcZ
 156  get_referrers�type�object�__setattr__)r[r\�refs�refrrr�update_instances:s
 157  ��rfc
 158  Cs t|j���D]W}t||�}zt||�}||kdurWqWn$ty9zt||�Wnttfy6YnwYqtyAYnwt||�rHqzt	||t||��Wqttfy^Yqwt|j���D]"}|t|j���vr�zt	||t||��Wqfttfy�Yqfwqft
 159  ||�dS)zjReplace stuff in the __dict__ of a class, and upgrade
 160      method code objects, and add new methods, if anyTN)rCrVrDr1rY�delattrrZr6�update_genericrXrf)r[r\�key�old_obj�new_objrrr�update_classFsB
 161  
 162  ���
 163  ���rlcCs.t|j|j�t|j|j�t|j|j�dS)z+Replace get/set/del functions of a propertyN)rh�fdel�fget�fset)r[r\rrr�update_propertypsrpcCst||�o	t||�Sr)�
 164  isinstance)�a�b�typrrr�isinstance2wsrucC�t||t�Sr)rura�rrrsrrrr|�rcC�t||tj�Sr)ru�types�FunctionTyperwrrrr}�cCrvr)ru�propertyrwrrrr~rxcCryr)rurz�
 165  MethodTyperwrrrr�r|cCst|j|j�Sr)r^�__func__rwrrrr�r|cCs,tD]\}}|||�r|||�dSqdS)NTF)�UPDATE_RULES)rrrsZ
 166  type_check�updaterrrrh�s
 167  
 168  �rhc@seZdZdd�Zdd�ZdS)�	StrongRefcCs
 169  ||_dSr��obj)rr�rrrr�s
 170  zStrongRef.__init__cCs|jSrr�)rrrr�__call__�szStrongRef.__call__N)r-rOrPrr�rrrrr��sr�)r-rS�__package__�
 171  __loader__�__spec__r,�
 172  __cached__�__builtins__FcCsrt|d�o
 173  |j|jk}|r|s|tvrdSn|sdS|j|f}z|�|g��t�|��WdSty8YdSw)NrOFT)	r0rOr-�	mod_attrs�
 174  setdefault�append�weakrefrerZ)r:�dr]r�ZautoloadZ	in_modulerirrr�
 175  append_obj�s�
 176  ��r�c
 177  
 178  Cs�|duri}t|j���D](\}}t||||�sq
 179  |j|f}z|�|g��t�|��Wq
 180  t	y5Yq
 181  wz|j�
 182  �}|j}|j��||jd<|d|jd<Wnt	tt
 183  fy]Ynwz||�}Wn
 184  |j�|��t|j���D]H\}}	|j|f}||vr�|dus�|dks�t||||	d�s�qv|	|j|<g}
 185  ||D]}|�}|dur�q�|
 186  �|�t||	�q�|
 187  r�|
 188  ||<qv||=qv|S)a2Enhanced version of the builtin reload function.
 189  
 190      superreload remembers objects previously in the module, and
 191  
 192      - upgrades the class dictionary of every old class in the module
 193      - upgrades the code object of every old function and method
 194      - clears the module's namespace before reloading
 195  
 196      Nr-r��EnumT)rCrV�itemsr�r-r�r�r�rerZ�copy�clearrYr r�Zuser_nsrh)
 197  r:rrrr]r�riZold_dictZold_namerkZnew_refsZold_refrjrrrrG�sZ
 198  
 199  �
 200  
 201  
 202  �
 203  �
 204  
 205  
 206  rGcs�eZdZ�fdd�Zee��ejdedddd�ejdd	d
 207  ddd
 208  �ejddd
 209  ddd
 210  �ejdd
 211  ddd
 212  �ddd�������Zeddd��Z	dd�Z
 213  dd�Z�ZS)�AutoreloadMagicscs>t�j|i|��t|j�|_d|j_d|j_ttj	�|_
 214  dS)NF)�superrr	r�	_reloaderrrF�setr(r�loaded_modules)rrr�kw�r_rrrs
 215  zAutoreloadMagics.__init__�mode�now�?aGblank or 'now' - Reload all modules (except those excluded by %%aimport)
 216               automatically now.
 217  
 218               '0' or 'off' - Disable automatic reloading.
 219  
 220               '1' or 'explicit' - Reload only modules imported with %%aimport every
 221               time before executing the Python code typed.
 222  
 223               '2' or 'all' - Reload all modules (except those excluded by %%aimport)
 224               every time before executing the Python code typed.
 225  
 226               '3' or 'complete' - Same as 2/all, but also but also adds any new
 227               objects in the module.
 228               )ra�default�nargs�helpz-pz--print�
 229  store_trueFz1Show autoreload activity using `print` statements)�actionr�r�z-lz--logz)Show autoreload activity using the loggerz
 230  --hide-errorszHide autoreload errors�cspt�|j|�}|j��}t�t�d�}|j���fdd�}|jdur.|j	dur.dd�|j
 231  _n|jdurB|j	dur=||j
 232  _n�|j
 233  _n	|j	durK�|j
 234  _|j|j
 235  _|dksX|d	kr`|j
 236  �
 237  d�dS|d
 238  ksh|dkrnd|j
 239  _dS|dksv|d
 240  kr�d|j
 241  _d|j
 242  _d|j
 243  _dS|dks�|dkr�d|j
 244  _d|j
 245  _d|j
 246  _dS|dks�|dkr�d|j
 247  _d|j
 248  _d|j
 249  _dStd|�d���)a*	%autoreload => Reload modules automatically
 250  
 251          %autoreload or %autoreload now
 252          Reload all modules (except those excluded by %aimport) automatically
 253          now.
 254  
 255          %autoreload 0 or %autoreload off
 256          Disable automatic reloading.
 257  
 258          %autoreload 1 or %autoreload explicit
 259          Reload only modules imported with %aimport every time before executing
 260          the Python code typed.
 261  
 262          %autoreload 2 or %autoreload all
 263          Reload all modules (except those excluded by %aimport) every time
 264          before executing the Python code typed.
 265  
 266          %autoreload 3 or %autoreload complete
 267          Same as 2/all, but also but also adds any new objects in the module. See
 268          unit test at IPython/extensions/tests/test_autoreload.py::test_autoload_newly_added_objects
 269  
 270          The optional arguments --print and --log control display of autoreload activity. The default
 271          is to act silently; --print (or -p) will print out the names of modules that are being
 272          reloaded, and --log (or -l) outputs them to the log at INFO level.
 273  
 274          The optional argument --hide-errors hides any errors that can happen when trying to
 275          reload code.
 276  
 277          Reloading Python modules in a reliable way is in general
 278          difficult, and unexpected things may occur. %autoreload tries to
 279          work around common pitfalls by replacing function code objects and
 280          parts of classes previously in the module with new versions. This
 281          makes the following things to work:
 282  
 283          - Functions and classes imported via 'from xxx import foo' are upgraded
 284            to new versions when 'xxx' is reloaded.
 285  
 286          - Methods and properties of classes are upgraded on reload, so that
 287            calling 'c.foo()' on an object 'c' created before the reload causes
 288            the new code for 'foo' to be executed.
 289  
 290          Some of the known remaining caveats are:
 291  
 292          - Replacing code objects does not always succeed: changing a @property
 293            in a class to an ordinary method or a method to a member variable
 294            can cause problems (but in old objects only).
 295  
 296          - Functions that are removed (eg. via monkey-patching) from a module
 297            before it is reloaded are not upgraded.
 298  
 299          - C extension modules cannot be reloaded, and so cannot be
 300            autoreloaded.
 301  
 302          �
 303  autoreloadcs�|��|�dSrrr
 304  ��l�prr�plvsz'AutoreloadMagics.autoreload.<locals>.plFcSr
 305  rrr
 306  rrrr{rz-AutoreloadMagics.autoreload.<locals>.<lambda>Tr�r��0�off�1Zexplicit�2�all�3ZcompletezUnrecognized autoreload mode "z".N)rZparse_argstringr�r�r5rH�logging�	getLogger�info�logr�rrrrBrrFr6)r�line�argsr��loggerr�rr�rr�s@a
 307  
 308  
 309  
 310  
 311  
 312  
 313  
 314  zAutoreloadMagics.autoreloadNc	Cs�|}|s;t|jj���}t|jj���}|durtj}|jjr%|�d�n
 315  |�dd�	|��|�dd�	|��dSdd�|�
 316  d�D�D]&}|�d	�r[|d
 317  d���}|j�
 318  |�qE|j�|�\}}|j�||i�qEdS)a"%aimport => Import modules for automatic reloading.
 319  
 320          %aimport
 321          List modules to automatically import and not to import.
 322  
 323          %aimport foo
 324          Import module 'foo' and mark it to be autoreloaded for %autoreload explicit
 325  
 326          %aimport foo, bar
 327          Import modules 'foo', 'bar' and mark them to be autoreloaded for %autoreload explicit
 328  
 329          %aimport -foo, bar
 330          Mark module 'foo' to not be autoreloaded for %autoreload explicit, all, or complete, and 'bar'
 331          to be autoreloaded for mode explicit.
 332          Nz&Modules to reload:
 333  all-except-skipped
 334  zModules to reload:
 335  %s
 336  � z
 337  Modules to skip:
 338  %s
 339  cSsg|]}|���qSr)�strip)�.0�_rrr�
 340  <listcomp>�sz,AutoreloadMagics.aimport.<locals>.<listcomp>�,�-�)�sortedr�rrDrr(�stdoutr�write�joinr'�
 341  startswithr�r#r+r�push)	rZparameter_s�streamrMZ	to_reloadZto_skip�_moduler*r)rrr�aimport�s"
 342  �zAutoreloadMagics.aimportcCs*|jjrz|j��WdSYdSdSr)r�rBr)rr�rrr�pre_run_cell�s�zAutoreloadMagics.pre_run_cellcCsTttj�|j}|D]}|j�tj|�\}}|dur!||jj|<q
 343  |j�|�dS)zFCache the modification times of any modules imported in this executionN)r�r(rr�r�r?rr�)rZnewly_loaded_modulesrMr�r>rrr�post_execute_hook�s�z"AutoreloadMagics.post_execute_hook)r�)r�N)
 344  r-rOrPrrrZargument�strr�r�r�r��
 345  __classcell__rrr�rr�sH����c&r�cCs6t|�}|�|�|j�d|j�|j�d|j�dS)zLoad the extension in IPython.r�Zpost_executeN)r�Zregister_magics�events�registerr�r�)�ipZauto_reloadrrr�load_ipython_extension�s
 346  r�)F)$rSZIPython.corerZIPython.core.magicrrrZ__skip_doctest__r2r(rJrzr�r`r��	importlibrr�importlib.utilrr	rWr^rfrlrprur��extendrhr�r�r�rGr�r�rrrr�<module>sPk
 347  	*
 348  
 349  
 350  ����
 351  
 352  KO