autoreload.py
521 lines
| 15.8 KiB
| text/x-python
|
PythonLexer
Fernando Perez
|
r6937 | """IPython extension to reload modules before executing user code. | ||
``autoreload`` reloads modules automatically before entering the execution of | ||||
code typed at the IPython prompt. | ||||
vivainio2
|
r1035 | |||
Pauli Virtanen
|
r4843 | This makes for example the following workflow possible: | ||
vivainio2
|
r1037 | |||
Pauli Virtanen
|
r4843 | .. sourcecode:: ipython | ||
In [1]: %load_ext autoreload | ||||
In [2]: %autoreload 2 | ||||
In [3]: from foo import some_function | ||||
In [4]: some_function() | ||||
Out[4]: 42 | ||||
In [5]: # open foo.py in an editor and change some_function to return 43 | ||||
In [6]: some_function() | ||||
Out[6]: 43 | ||||
Fernando Perez
|
r6937 | The module was reloaded without reloading it explicitly, and the object | ||
imported with ``from foo import ...`` was also updated. | ||||
Pauli Virtanen
|
r4843 | |||
Usage | ||||
===== | ||||
The following magic commands are provided: | ||||
``%autoreload`` | ||||
Reload all modules (except those excluded by ``%aimport``) | ||||
automatically now. | ||||
``%autoreload 0`` | ||||
Disable automatic reloading. | ||||
``%autoreload 1`` | ||||
Reload all modules imported with ``%aimport`` every time before | ||||
executing the Python code typed. | ||||
``%autoreload 2`` | ||||
Reload all modules (except those excluded by ``%aimport``) every | ||||
time before executing the Python code typed. | ||||
``%aimport`` | ||||
List modules which are to be automatically imported or not to be imported. | ||||
``%aimport foo`` | ||||
Import module 'foo' and mark it to be autoreloaded for ``%autoreload 1`` | ||||
``%aimport -foo`` | ||||
Mark module 'foo' to not be autoreloaded. | ||||
Caveats | ||||
======= | ||||
Reloading Python modules in a reliable way is in general difficult, | ||||
and unexpected things may occur. ``%autoreload`` tries to work around | ||||
common pitfalls by replacing function code objects and parts of | ||||
classes previously in the module with new versions. This makes the | ||||
following things to work: | ||||
- Functions and classes imported via 'from xxx import foo' are upgraded | ||||
to new versions when 'xxx' is reloaded. | ||||
- Methods and properties of classes are upgraded on reload, so that | ||||
calling 'c.foo()' on an object 'c' created before the reload causes | ||||
the new code for 'foo' to be executed. | ||||
Some of the known remaining caveats are: | ||||
- Replacing code objects does not always succeed: changing a @property | ||||
in a class to an ordinary method or a method to a member variable | ||||
can cause problems (but in old objects only). | ||||
- Functions that are removed (eg. via monkey-patching) from a module | ||||
before it is reloaded are not upgraded. | ||||
Fernando Perez
|
r6937 | - C extension modules cannot be reloaded, and so cannot be autoreloaded. | ||
vivainio2
|
r1035 | """ | ||
Matthias BUSSONNIER
|
r7817 | from __future__ import print_function | ||
vivainio2
|
r1035 | |||
Fernando Perez
|
r4866 | skip_doctest = True | ||
Fernando Perez
|
r6937 | #----------------------------------------------------------------------------- | ||
# Copyright (C) 2000 Thomas Heller | ||||
# Copyright (C) 2008 Pauli Virtanen <pav@iki.fi> | ||||
# Copyright (C) 2012 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
vivainio2
|
r1035 | # | ||
# This IPython module is written by Pauli Virtanen, based on the autoreload | ||||
# code by Thomas Heller. | ||||
Fernando Perez
|
r6937 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r8197 | |||
Fernando Perez
|
r6937 | import imp | ||
import os | ||||
import sys | ||||
import traceback | ||||
import types | ||||
Ville M. Vainio
|
r1245 | import weakref | ||
Fernando Perez
|
r6937 | |||
Thomas Kluyver
|
r4898 | try: | ||
Fernando Perez
|
r6937 | # Reload is not defined by default in Python3. | ||
Thomas Kluyver
|
r4898 | reload | ||
except NameError: | ||||
from imp import reload | ||||
vivainio2
|
r1035 | |||
Thomas Kluyver
|
r5911 | from IPython.utils import pyfile | ||
from IPython.utils.py3compat import PY3 | ||||
Fernando Perez
|
r6937 | #------------------------------------------------------------------------------ | ||
# Autoreload functionality | ||||
#------------------------------------------------------------------------------ | ||||
vivainio2
|
r1035 | def _get_compiled_ext(): | ||
"""Official way to get the extension of compiled files (.pyc or .pyo)""" | ||||
for ext, mode, typ in imp.get_suffixes(): | ||||
if typ == imp.PY_COMPILED: | ||||
return ext | ||||
Fernando Perez
|
r6937 | |||
vivainio2
|
r1035 | PY_COMPILED_EXT = _get_compiled_ext() | ||
Fernando Perez
|
r6937 | |||
vivainio2
|
r1035 | class ModuleReloader(object): | ||
Pauli Virtanen
|
r4840 | enabled = False | ||
"""Whether this reloader is enabled""" | ||||
Ville M. Vainio
|
r1245 | failed = {} | ||
vivainio2
|
r1035 | """Modules that failed to reload: {module: mtime-on-failed-reload, ...}""" | ||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | modules = {} | ||
"""Modules specially marked as autoreloadable.""" | ||||
skip_modules = {} | ||||
"""Modules specially marked as not autoreloadable.""" | ||||
check_all = True | ||||
"""Autoreload all modules, not just those listed in 'modules'""" | ||||
Ville M. Vainio
|
r1245 | |||
old_objects = {} | ||||
"""(module-name, name) -> weakref, for replacing old code objects""" | ||||
Pauli Virtanen
|
r2004 | |||
Pauli Virtanen
|
r4840 | def mark_module_skipped(self, module_name): | ||
"""Skip reloading the named module in the future""" | ||||
try: | ||||
del self.modules[module_name] | ||||
except KeyError: | ||||
pass | ||||
self.skip_modules[module_name] = True | ||||
def mark_module_reloadable(self, module_name): | ||||
"""Reload the named module in the future (if it is imported)""" | ||||
try: | ||||
del self.skip_modules[module_name] | ||||
except KeyError: | ||||
pass | ||||
self.modules[module_name] = True | ||||
def aimport_module(self, module_name): | ||||
"""Import a module, and mark it reloadable | ||||
Returns | ||||
------- | ||||
top_module : module | ||||
The imported module if it is top-level, or the top-level | ||||
top_name : module | ||||
Name of top_module | ||||
""" | ||||
self.mark_module_reloadable(module_name) | ||||
__import__(module_name) | ||||
top_name = module_name.split('.')[0] | ||||
top_module = sys.modules[top_name] | ||||
return top_module, top_name | ||||
vivainio2
|
r1035 | def check(self, check_all=False): | ||
"""Check whether some modules need to be reloaded.""" | ||||
Pauli Virtanen
|
r2004 | |||
Pauli Virtanen
|
r4840 | if not self.enabled and not check_all: | ||
return | ||||
vivainio2
|
r1035 | if check_all or self.check_all: | ||
modules = sys.modules.keys() | ||||
else: | ||||
modules = self.modules.keys() | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | for modname in modules: | ||
m = sys.modules.get(modname, None) | ||||
if modname in self.skip_modules: | ||||
continue | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | if not hasattr(m, '__file__'): | ||
continue | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | if m.__name__ == '__main__': | ||
# we cannot reload(__main__) | ||||
continue | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | filename = m.__file__ | ||
path, ext = os.path.splitext(filename) | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | if ext.lower() == '.py': | ||
ext = PY_COMPILED_EXT | ||||
Thomas Kluyver
|
r5911 | pyc_filename = pyfile.cache_from_source(filename) | ||
Pauli Virtanen
|
r4840 | py_filename = filename | ||
else: | ||||
pyc_filename = filename | ||||
Thomas Kluyver
|
r5911 | try: | ||
py_filename = pyfile.source_from_cache(filename) | ||||
except ValueError: | ||||
continue | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | try: | ||
Pauli Virtanen
|
r4840 | pymtime = os.stat(py_filename).st_mtime | ||
if pymtime <= os.stat(pyc_filename).st_mtime: | ||||
vivainio2
|
r1035 | continue | ||
Pauli Virtanen
|
r4840 | if self.failed.get(py_filename, None) == pymtime: | ||
vivainio2
|
r1035 | continue | ||
except OSError: | ||||
continue | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | try: | ||
Ville M. Vainio
|
r1245 | superreload(m, reload, self.old_objects) | ||
Pauli Virtanen
|
r4840 | if py_filename in self.failed: | ||
del self.failed[py_filename] | ||||
vivainio2
|
r1035 | except: | ||
Matthias BUSSONNIER
|
r7817 | print("[autoreload of %s failed: %s]" % ( | ||
modname, traceback.format_exc(1)), file=sys.stderr) | ||||
Pauli Virtanen
|
r4840 | self.failed[py_filename] = pymtime | ||
vivainio2
|
r1035 | |||
Ville M. Vainio
|
r1245 | #------------------------------------------------------------------------------ | ||
# superreload | ||||
#------------------------------------------------------------------------------ | ||||
vivainio2
|
r1035 | |||
Thomas Kluyver
|
r5911 | if PY3: | ||
func_attrs = ['__code__', '__defaults__', '__doc__', | ||||
'__closure__', '__globals__', '__dict__'] | ||||
else: | ||||
func_attrs = ['func_code', 'func_defaults', 'func_doc', | ||||
'func_closure', 'func_globals', 'func_dict'] | ||||
Fernando Perez
|
r6937 | |||
Ville M. Vainio
|
r1245 | def update_function(old, new): | ||
"""Upgrade the code object of a function""" | ||||
Thomas Kluyver
|
r5911 | for name in func_attrs: | ||
Ville M. Vainio
|
r1245 | try: | ||
setattr(old, name, getattr(new, name)) | ||||
except (AttributeError, TypeError): | ||||
pass | ||||
Fernando Perez
|
r6937 | |||
Ville M. Vainio
|
r1245 | def update_class(old, new): | ||
"""Replace stuff in the __dict__ of a class, and upgrade | ||||
method code objects""" | ||||
for key in old.__dict__.keys(): | ||||
old_obj = getattr(old, key) | ||||
try: | ||||
new_obj = getattr(new, key) | ||||
except AttributeError: | ||||
# obsolete attribute: remove it | ||||
Pauli Virtanen
|
r2004 | try: | ||
Ville M. Vainio
|
r1245 | delattr(old, key) | ||
except (AttributeError, TypeError): | ||||
pass | ||||
continue | ||||
Pauli Virtanen
|
r2004 | |||
Ville M. Vainio
|
r1245 | if update_generic(old_obj, new_obj): continue | ||
try: | ||||
setattr(old, key, getattr(new, key)) | ||||
except (AttributeError, TypeError): | ||||
pass # skip non-writable attributes | ||||
Fernando Perez
|
r6937 | |||
Ville M. Vainio
|
r1245 | def update_property(old, new): | ||
"""Replace get/set/del functions of a property""" | ||||
update_generic(old.fdel, new.fdel) | ||||
update_generic(old.fget, new.fget) | ||||
update_generic(old.fset, new.fset) | ||||
Fernando Perez
|
r6937 | |||
Ville M. Vainio
|
r1245 | def isinstance2(a, b, typ): | ||
return isinstance(a, typ) and isinstance(b, typ) | ||||
Fernando Perez
|
r6937 | |||
Ville M. Vainio
|
r1245 | UPDATE_RULES = [ | ||
Thomas Kluyver
|
r5911 | (lambda a, b: isinstance2(a, b, type), | ||
Ville M. Vainio
|
r1245 | update_class), | ||
(lambda a, b: isinstance2(a, b, types.FunctionType), | ||||
Pauli Virtanen
|
r2004 | update_function), | ||
Ville M. Vainio
|
r1245 | (lambda a, b: isinstance2(a, b, property), | ||
Pauli Virtanen
|
r2004 | update_property), | ||
Ville M. Vainio
|
r1245 | ] | ||
Fernando Perez
|
r6937 | |||
Thomas Kluyver
|
r5911 | if PY3: | ||
UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType), | ||||
lambda a, b: update_function(a.__func__, b.__func__)), | ||||
]) | ||||
else: | ||||
UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.ClassType), | ||||
update_class), | ||||
(lambda a, b: isinstance2(a, b, types.MethodType), | ||||
lambda a, b: update_function(a.im_func, b.im_func)), | ||||
]) | ||||
Ville M. Vainio
|
r1245 | def update_generic(a, b): | ||
for type_check, update in UPDATE_RULES: | ||||
if type_check(a, b): | ||||
update(a, b) | ||||
return True | ||||
return False | ||||
Fernando Perez
|
r6937 | |||
Ville M. Vainio
|
r1245 | class StrongRef(object): | ||
def __init__(self, obj): | ||||
self.obj = obj | ||||
def __call__(self): | ||||
return self.obj | ||||
Fernando Perez
|
r6937 | |||
Ville M. Vainio
|
r1245 | def superreload(module, reload=reload, old_objects={}): | ||
vivainio2
|
r1035 | """Enhanced version of the builtin reload function. | ||
Pauli Virtanen
|
r2004 | |||
Ville M. Vainio
|
r1245 | superreload remembers objects previously in the module, and | ||
- upgrades the class dictionary of every old class in the module | ||||
- upgrades the code object of every old function and method | ||||
- clears the module's namespace before reloading | ||||
Pauli Virtanen
|
r2004 | |||
vivainio2
|
r1035 | """ | ||
Pauli Virtanen
|
r2004 | |||
Ville M. Vainio
|
r1245 | # collect old objects in the module | ||
for name, obj in module.__dict__.items(): | ||||
if not hasattr(obj, '__module__') or obj.__module__ != module.__name__: | ||||
continue | ||||
key = (module.__name__, name) | ||||
try: | ||||
old_objects.setdefault(key, []).append(weakref.ref(obj)) | ||||
except TypeError: | ||||
# weakref doesn't work for all types; | ||||
# create strong references for 'important' cases | ||||
Thomas Kluyver
|
r5911 | if not PY3 and isinstance(obj, types.ClassType): | ||
Ville M. Vainio
|
r1245 | old_objects.setdefault(key, []).append(StrongRef(obj)) | ||
# reload module | ||||
try: | ||||
# clear namespace first from old cruft | ||||
Pauli Virtanen
|
r4841 | old_dict = module.__dict__.copy() | ||
Ville M. Vainio
|
r1245 | old_name = module.__name__ | ||
module.__dict__.clear() | ||||
module.__dict__['__name__'] = old_name | ||||
Thomas Kluyver
|
r7020 | module.__dict__['__loader__'] = old_dict['__loader__'] | ||
Ville M. Vainio
|
r1245 | except (TypeError, AttributeError, KeyError): | ||
pass | ||||
Pauli Virtanen
|
r4841 | |||
try: | ||||
module = reload(module) | ||||
except: | ||||
# restore module dictionary on failed reload | ||||
module.__dict__.update(old_dict) | ||||
raise | ||||
Pauli Virtanen
|
r2004 | |||
Ville M. Vainio
|
r1245 | # iterate over all objects and update functions & classes | ||
vivainio2
|
r1035 | for name, new_obj in module.__dict__.items(): | ||
key = (module.__name__, name) | ||||
Ville M. Vainio
|
r1245 | if key not in old_objects: continue | ||
new_refs = [] | ||||
for old_ref in old_objects[key]: | ||||
old_obj = old_ref() | ||||
if old_obj is None: continue | ||||
new_refs.append(old_ref) | ||||
update_generic(old_obj, new_obj) | ||||
if new_refs: | ||||
old_objects[key] = new_refs | ||||
else: | ||||
del old_objects[key] | ||||
vivainio2
|
r1035 | return module | ||
#------------------------------------------------------------------------------ | ||||
Ville M. Vainio
|
r1245 | # IPython connectivity | ||
vivainio2
|
r1035 | #------------------------------------------------------------------------------ | ||
Ville M. Vainio
|
r1245 | |||
Pauli Virtanen
|
r4702 | from IPython.core.hooks import TryNext | ||
Fernando Perez
|
r6973 | from IPython.core.magic import Magics, magics_class, line_magic | ||
Pauli Virtanen
|
r2004 | |||
Fernando Perez
|
r6973 | @magics_class | ||
Fernando Perez
|
r6937 | class AutoreloadMagics(Magics): | ||
Pauli Virtanen
|
r4840 | def __init__(self, *a, **kw): | ||
Fernando Perez
|
r6937 | super(AutoreloadMagics, self).__init__(*a, **kw) | ||
Pauli Virtanen
|
r4702 | self._reloader = ModuleReloader() | ||
self._reloader.check_all = False | ||||
Pauli Virtanen
|
r2004 | |||
Fernando Perez
|
r6937 | @line_magic | ||
def autoreload(self, parameter_s=''): | ||||
Pauli Virtanen
|
r4702 | r"""%autoreload => Reload modules automatically | ||
%autoreload | ||||
Reload all modules (except those excluded by %aimport) automatically | ||||
now. | ||||
%autoreload 0 | ||||
Disable automatic reloading. | ||||
%autoreload 1 | ||||
Reload all modules imported with %aimport every time before executing | ||||
the Python code typed. | ||||
%autoreload 2 | ||||
Reload all modules (except those excluded by %aimport) every time | ||||
before executing the Python code typed. | ||||
Reloading Python modules in a reliable way is in general | ||||
difficult, and unexpected things may occur. %autoreload tries to | ||||
work around common pitfalls by replacing function code objects and | ||||
parts of classes previously in the module with new versions. This | ||||
makes the following things to work: | ||||
- Functions and classes imported via 'from xxx import foo' are upgraded | ||||
to new versions when 'xxx' is reloaded. | ||||
- Methods and properties of classes are upgraded on reload, so that | ||||
calling 'c.foo()' on an object 'c' created before the reload causes | ||||
the new code for 'foo' to be executed. | ||||
Some of the known remaining caveats are: | ||||
- Replacing code objects does not always succeed: changing a @property | ||||
in a class to an ordinary method or a method to a member variable | ||||
can cause problems (but in old objects only). | ||||
- Functions that are removed (eg. via monkey-patching) from a module | ||||
before it is reloaded are not upgraded. | ||||
- C extension modules cannot be reloaded, and so cannot be | ||||
autoreloaded. | ||||
""" | ||||
if parameter_s == '': | ||||
self._reloader.check(True) | ||||
elif parameter_s == '0': | ||||
Pauli Virtanen
|
r4840 | self._reloader.enabled = False | ||
Pauli Virtanen
|
r4702 | elif parameter_s == '1': | ||
self._reloader.check_all = False | ||||
Pauli Virtanen
|
r4840 | self._reloader.enabled = True | ||
Pauli Virtanen
|
r4702 | elif parameter_s == '2': | ||
self._reloader.check_all = True | ||||
Pauli Virtanen
|
r4840 | self._reloader.enabled = True | ||
Pauli Virtanen
|
r4702 | |||
Fernando Perez
|
r6937 | @line_magic | ||
def aimport(self, parameter_s='', stream=None): | ||||
Pauli Virtanen
|
r4702 | """%aimport => Import modules for automatic reloading. | ||
%aimport | ||||
List modules to automatically import and not to import. | ||||
%aimport foo | ||||
Import module 'foo' and mark it to be autoreloaded for %autoreload 1 | ||||
%aimport -foo | ||||
Mark module 'foo' to not be autoreloaded for %autoreload 1 | ||||
""" | ||||
modname = parameter_s | ||||
if not modname: | ||||
to_reload = self._reloader.modules.keys() | ||||
to_reload.sort() | ||||
to_skip = self._reloader.skip_modules.keys() | ||||
to_skip.sort() | ||||
Pauli Virtanen
|
r4840 | if stream is None: | ||
stream = sys.stdout | ||||
Pauli Virtanen
|
r4702 | if self._reloader.check_all: | ||
Pauli Virtanen
|
r4840 | stream.write("Modules to reload:\nall-except-skipped\n") | ||
Pauli Virtanen
|
r4702 | else: | ||
Pauli Virtanen
|
r4840 | stream.write("Modules to reload:\n%s\n" % ' '.join(to_reload)) | ||
stream.write("\nModules to skip:\n%s\n" % ' '.join(to_skip)) | ||||
Pauli Virtanen
|
r4702 | elif modname.startswith('-'): | ||
modname = modname[1:] | ||||
Pauli Virtanen
|
r4840 | self._reloader.mark_module_skipped(modname) | ||
Pauli Virtanen
|
r4702 | else: | ||
Pauli Virtanen
|
r4840 | top_module, top_name = self._reloader.aimport_module(modname) | ||
vivainio2
|
r1035 | |||
Pauli Virtanen
|
r4840 | # Inject module to user namespace | ||
Fernando Perez
|
r6937 | self.shell.push({top_name: top_module}) | ||
vivainio2
|
r1035 | |||
Fernando Perez
|
r6951 | def pre_run_code_hook(self, ip): | ||
Pauli Virtanen
|
r4840 | if not self._reloader.enabled: | ||
raise TryNext | ||||
try: | ||||
self._reloader.check() | ||||
except: | ||||
pass | ||||
Fernando Perez
|
r6937 | |||
Pauli Virtanen
|
r4702 | def load_ipython_extension(ip): | ||
"""Load the extension in IPython.""" | ||||
Thomas Kluyver
|
r8552 | auto_reload = AutoreloadMagics(ip) | ||
ip.register_magics(auto_reload) | ||||
ip.set_hook('pre_run_code_hook', auto_reload.pre_run_code_hook) | ||||