Show More
@@ -1,350 +1,358 | |||
|
1 | 1 | """ |
|
2 | 2 | IPython extension: autoreload modules before executing the next line |
|
3 | 3 | |
|
4 | 4 | Try:: |
|
5 | 5 | |
|
6 | 6 | %autoreload? |
|
7 | 7 | |
|
8 | 8 | for documentation. |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | # Pauli Virtanen <pav@iki.fi>, 2008. |
|
12 | 12 | # Thomas Heller, 2000. |
|
13 | 13 | # |
|
14 | 14 | # This IPython module is written by Pauli Virtanen, based on the autoreload |
|
15 | 15 | # code by Thomas Heller. |
|
16 | 16 | |
|
17 | 17 | #------------------------------------------------------------------------------ |
|
18 | 18 | # Autoreload functionality |
|
19 | 19 | #------------------------------------------------------------------------------ |
|
20 | 20 | |
|
21 | 21 | import time, os, threading, sys, types, imp, inspect, traceback, atexit |
|
22 | 22 | import weakref |
|
23 | 23 | |
|
24 | 24 | def _get_compiled_ext(): |
|
25 | 25 | """Official way to get the extension of compiled files (.pyc or .pyo)""" |
|
26 | 26 | for ext, mode, typ in imp.get_suffixes(): |
|
27 | 27 | if typ == imp.PY_COMPILED: |
|
28 | 28 | return ext |
|
29 | 29 | |
|
30 | 30 | PY_COMPILED_EXT = _get_compiled_ext() |
|
31 | 31 | |
|
32 | 32 | class ModuleReloader(object): |
|
33 | 33 | failed = {} |
|
34 | 34 | """Modules that failed to reload: {module: mtime-on-failed-reload, ...}""" |
|
35 | 35 | |
|
36 | 36 | modules = {} |
|
37 | 37 | """Modules specially marked as autoreloadable.""" |
|
38 | 38 | |
|
39 | 39 | skip_modules = {} |
|
40 | 40 | """Modules specially marked as not autoreloadable.""" |
|
41 | 41 | |
|
42 | 42 | check_all = True |
|
43 | 43 | """Autoreload all modules, not just those listed in 'modules'""" |
|
44 | 44 | |
|
45 | 45 | old_objects = {} |
|
46 | 46 | """(module-name, name) -> weakref, for replacing old code objects""" |
|
47 | 47 | |
|
48 | 48 | def check(self, check_all=False): |
|
49 | 49 | """Check whether some modules need to be reloaded.""" |
|
50 | 50 | |
|
51 | 51 | if check_all or self.check_all: |
|
52 | 52 | modules = sys.modules.keys() |
|
53 | 53 | else: |
|
54 | 54 | modules = self.modules.keys() |
|
55 | 55 | |
|
56 | 56 | for modname in modules: |
|
57 | 57 | m = sys.modules.get(modname, None) |
|
58 | 58 | |
|
59 | 59 | if modname in self.skip_modules: |
|
60 | 60 | continue |
|
61 | 61 | |
|
62 | 62 | if not hasattr(m, '__file__'): |
|
63 | 63 | continue |
|
64 | 64 | |
|
65 | 65 | if m.__name__ == '__main__': |
|
66 | 66 | # we cannot reload(__main__) |
|
67 | 67 | continue |
|
68 | 68 | |
|
69 | 69 | filename = m.__file__ |
|
70 | 70 | dirname = os.path.dirname(filename) |
|
71 | 71 | path, ext = os.path.splitext(filename) |
|
72 | 72 | |
|
73 | 73 | if ext.lower() == '.py': |
|
74 | 74 | ext = PY_COMPILED_EXT |
|
75 | 75 | filename = os.path.join(dirname, path + PY_COMPILED_EXT) |
|
76 | 76 | |
|
77 | 77 | if ext != PY_COMPILED_EXT: |
|
78 | 78 | continue |
|
79 | 79 | |
|
80 | 80 | try: |
|
81 | 81 | pymtime = os.stat(filename[:-1]).st_mtime |
|
82 | 82 | if pymtime <= os.stat(filename).st_mtime: |
|
83 | 83 | continue |
|
84 | 84 | if self.failed.get(filename[:-1], None) == pymtime: |
|
85 | 85 | continue |
|
86 | 86 | except OSError: |
|
87 | 87 | continue |
|
88 | 88 | |
|
89 | 89 | try: |
|
90 | 90 | superreload(m, reload, self.old_objects) |
|
91 | 91 | if filename[:-1] in self.failed: |
|
92 | 92 | del self.failed[filename[:-1]] |
|
93 | 93 | except: |
|
94 | 94 | print >> sys.stderr, "[autoreload of %s failed: %s]" % ( |
|
95 | 95 | modname, traceback.format_exc(1)) |
|
96 | 96 | self.failed[filename[:-1]] = pymtime |
|
97 | 97 | |
|
98 | 98 | #------------------------------------------------------------------------------ |
|
99 | 99 | # superreload |
|
100 | 100 | #------------------------------------------------------------------------------ |
|
101 | 101 | |
|
102 | 102 | def update_function(old, new): |
|
103 | 103 | """Upgrade the code object of a function""" |
|
104 | 104 | for name in ['func_code', 'func_defaults', 'func_doc', |
|
105 | 105 | 'func_closure', 'func_globals', 'func_dict']: |
|
106 | 106 | try: |
|
107 | 107 | setattr(old, name, getattr(new, name)) |
|
108 | 108 | except (AttributeError, TypeError): |
|
109 | 109 | pass |
|
110 | 110 | |
|
111 | 111 | def update_class(old, new): |
|
112 | 112 | """Replace stuff in the __dict__ of a class, and upgrade |
|
113 | 113 | method code objects""" |
|
114 | 114 | for key in old.__dict__.keys(): |
|
115 | 115 | old_obj = getattr(old, key) |
|
116 | 116 | |
|
117 | 117 | try: |
|
118 | 118 | new_obj = getattr(new, key) |
|
119 | 119 | except AttributeError: |
|
120 | 120 | # obsolete attribute: remove it |
|
121 | 121 | try: |
|
122 | 122 | delattr(old, key) |
|
123 | 123 | except (AttributeError, TypeError): |
|
124 | 124 | pass |
|
125 | 125 | continue |
|
126 | 126 | |
|
127 | 127 | if update_generic(old_obj, new_obj): continue |
|
128 | 128 | |
|
129 | 129 | try: |
|
130 | 130 | setattr(old, key, getattr(new, key)) |
|
131 | 131 | except (AttributeError, TypeError): |
|
132 | 132 | pass # skip non-writable attributes |
|
133 | 133 | |
|
134 | 134 | def update_property(old, new): |
|
135 | 135 | """Replace get/set/del functions of a property""" |
|
136 | 136 | update_generic(old.fdel, new.fdel) |
|
137 | 137 | update_generic(old.fget, new.fget) |
|
138 | 138 | update_generic(old.fset, new.fset) |
|
139 | 139 | |
|
140 | 140 | def isinstance2(a, b, typ): |
|
141 | 141 | return isinstance(a, typ) and isinstance(b, typ) |
|
142 | 142 | |
|
143 | 143 | UPDATE_RULES = [ |
|
144 | 144 | (lambda a, b: isinstance2(a, b, types.ClassType), |
|
145 | 145 | update_class), |
|
146 | 146 | (lambda a, b: isinstance2(a, b, types.TypeType), |
|
147 | 147 | update_class), |
|
148 | 148 | (lambda a, b: isinstance2(a, b, types.FunctionType), |
|
149 | 149 | update_function), |
|
150 | 150 | (lambda a, b: isinstance2(a, b, property), |
|
151 | 151 | update_property), |
|
152 | 152 | (lambda a, b: isinstance2(a, b, types.MethodType), |
|
153 | 153 | lambda a, b: update_function(a.im_func, b.im_func)), |
|
154 | 154 | ] |
|
155 | 155 | |
|
156 | 156 | def update_generic(a, b): |
|
157 | 157 | for type_check, update in UPDATE_RULES: |
|
158 | 158 | if type_check(a, b): |
|
159 | 159 | update(a, b) |
|
160 | 160 | return True |
|
161 | 161 | return False |
|
162 | 162 | |
|
163 | 163 | class StrongRef(object): |
|
164 | 164 | def __init__(self, obj): |
|
165 | 165 | self.obj = obj |
|
166 | 166 | def __call__(self): |
|
167 | 167 | return self.obj |
|
168 | 168 | |
|
169 | 169 | def superreload(module, reload=reload, old_objects={}): |
|
170 | 170 | """Enhanced version of the builtin reload function. |
|
171 | 171 | |
|
172 | 172 | superreload remembers objects previously in the module, and |
|
173 | 173 | |
|
174 | 174 | - upgrades the class dictionary of every old class in the module |
|
175 | 175 | - upgrades the code object of every old function and method |
|
176 | 176 | - clears the module's namespace before reloading |
|
177 | 177 | |
|
178 | 178 | """ |
|
179 | 179 | |
|
180 | 180 | # collect old objects in the module |
|
181 | 181 | for name, obj in module.__dict__.items(): |
|
182 | 182 | if not hasattr(obj, '__module__') or obj.__module__ != module.__name__: |
|
183 | 183 | continue |
|
184 | 184 | key = (module.__name__, name) |
|
185 | 185 | try: |
|
186 | 186 | old_objects.setdefault(key, []).append(weakref.ref(obj)) |
|
187 | 187 | except TypeError: |
|
188 | 188 | # weakref doesn't work for all types; |
|
189 | 189 | # create strong references for 'important' cases |
|
190 | 190 | if isinstance(obj, types.ClassType): |
|
191 | 191 | old_objects.setdefault(key, []).append(StrongRef(obj)) |
|
192 | 192 | |
|
193 | 193 | # reload module |
|
194 | 194 | try: |
|
195 | 195 | # clear namespace first from old cruft |
|
196 | 196 | old_name = module.__name__ |
|
197 | 197 | module.__dict__.clear() |
|
198 | 198 | module.__dict__['__name__'] = old_name |
|
199 | 199 | except (TypeError, AttributeError, KeyError): |
|
200 | 200 | pass |
|
201 | 201 | module = reload(module) |
|
202 | 202 | |
|
203 | 203 | # iterate over all objects and update functions & classes |
|
204 | 204 | for name, new_obj in module.__dict__.items(): |
|
205 | 205 | key = (module.__name__, name) |
|
206 | 206 | if key not in old_objects: continue |
|
207 | 207 | |
|
208 | 208 | new_refs = [] |
|
209 | 209 | for old_ref in old_objects[key]: |
|
210 | 210 | old_obj = old_ref() |
|
211 | 211 | if old_obj is None: continue |
|
212 | 212 | new_refs.append(old_ref) |
|
213 | 213 | update_generic(old_obj, new_obj) |
|
214 | 214 | |
|
215 | 215 | if new_refs: |
|
216 | 216 | old_objects[key] = new_refs |
|
217 | 217 | else: |
|
218 | 218 | del old_objects[key] |
|
219 | 219 | |
|
220 | 220 | return module |
|
221 | 221 | |
|
222 | reloader = ModuleReloader() | |
|
223 | ||
|
224 | 222 | #------------------------------------------------------------------------------ |
|
225 | 223 | # IPython connectivity |
|
226 | 224 | #------------------------------------------------------------------------------ |
|
227 | from IPython.core import ipapi | |
|
228 | from IPython.core.error import TryNext | |
|
229 | ||
|
230 | ip = ipapi.get() | |
|
231 | ||
|
232 | autoreload_enabled = False | |
|
233 | ||
|
234 | def runcode_hook(self): | |
|
235 | if not autoreload_enabled: | |
|
236 | raise TryNext | |
|
237 | try: | |
|
238 | reloader.check() | |
|
239 | except: | |
|
240 | pass | |
|
241 | ||
|
242 | def enable_autoreload(): | |
|
243 | global autoreload_enabled | |
|
244 | autoreload_enabled = True | |
|
245 | ||
|
246 | def disable_autoreload(): | |
|
247 | global autoreload_enabled | |
|
248 | autoreload_enabled = False | |
|
249 | ||
|
250 | def autoreload_f(self, parameter_s=''): | |
|
251 | r""" %autoreload => Reload modules automatically | |
|
252 | ||
|
253 | %autoreload | |
|
254 | Reload all modules (except those excluded by %aimport) automatically now. | |
|
255 | ||
|
256 | %autoreload 0 | |
|
257 | Disable automatic reloading. | |
|
258 | ||
|
259 | %autoreload 1 | |
|
260 | Reload all modules imported with %aimport every time before executing | |
|
261 | the Python code typed. | |
|
262 | ||
|
263 | %autoreload 2 | |
|
264 | Reload all modules (except those excluded by %aimport) every time | |
|
265 | before executing the Python code typed. | |
|
266 | ||
|
267 | Reloading Python modules in a reliable way is in general | |
|
268 | difficult, and unexpected things may occur. %autoreload tries to | |
|
269 | work around common pitfalls by replacing function code objects and | |
|
270 | parts of classes previously in the module with new versions. This | |
|
271 | makes the following things to work: | |
|
272 | 225 | |
|
273 | - Functions and classes imported via 'from xxx import foo' are upgraded | |
|
274 | to new versions when 'xxx' is reloaded. | |
|
226 | from IPython.core.plugin import Plugin | |
|
227 | from IPython.core.hooks import TryNext | |
|
275 | 228 | |
|
276 | - Methods and properties of classes are upgraded on reload, so that | |
|
277 | calling 'c.foo()' on an object 'c' created before the reload causes | |
|
278 | the new code for 'foo' to be executed. | |
|
229 | class Autoreload(Plugin): | |
|
230 | def __init__(self, shell=None, config=None): | |
|
231 | super(Autoreload, self).__init__(shell=shell, config=config) | |
|
279 | 232 | |
|
280 | Some of the known remaining caveats are: | |
|
233 | self.shell.define_magic('autoreload', self.magic_autoreload) | |
|
234 | self.shell.define_magic('aimport', self.magic_aimport) | |
|
235 | self.shell.set_hook('pre_run_code_hook', self.pre_run_code_hook) | |
|
281 | 236 | |
|
282 | - Replacing code objects does not always succeed: changing a @property | |
|
283 | in a class to an ordinary method or a method to a member variable | |
|
284 | can cause problems (but in old objects only). | |
|
237 | self._enabled = False | |
|
238 | self._reloader = ModuleReloader() | |
|
239 | self._reloader.check_all = False | |
|
285 | 240 | |
|
286 | - Functions that are removed (eg. via monkey-patching) from a module | |
|
287 | before it is reloaded are not upgraded. | |
|
288 | ||
|
289 | - C extension modules cannot be reloaded, and so cannot be | |
|
290 | autoreloaded. | |
|
291 | ||
|
292 | """ | |
|
293 | if parameter_s == '': | |
|
294 | reloader.check(True) | |
|
295 | elif parameter_s == '0': | |
|
296 | disable_autoreload() | |
|
297 | elif parameter_s == '1': | |
|
298 | reloader.check_all = False | |
|
299 | enable_autoreload() | |
|
300 | elif parameter_s == '2': | |
|
301 | reloader.check_all = True | |
|
302 | enable_autoreload() | |
|
303 | ||
|
304 | def aimport_f(self, parameter_s=''): | |
|
305 | """%aimport => Import modules for automatic reloading. | |
|
241 | def pre_run_code_hook(self, ipself): | |
|
242 | if not self._enabled: | |
|
243 | raise TryNext | |
|
244 | try: | |
|
245 | self._reloader.check() | |
|
246 | except: | |
|
247 | pass | |
|
306 | 248 | |
|
307 | %aimport | |
|
308 | List modules to automatically import and not to import. | |
|
249 | def magic_autoreload(self, ipself, parameter_s=''): | |
|
250 | r"""%autoreload => Reload modules automatically | |
|
251 | ||
|
252 | %autoreload | |
|
253 | Reload all modules (except those excluded by %aimport) automatically | |
|
254 | now. | |
|
255 | ||
|
256 | %autoreload 0 | |
|
257 | Disable automatic reloading. | |
|
258 | ||
|
259 | %autoreload 1 | |
|
260 | Reload all modules imported with %aimport every time before executing | |
|
261 | the Python code typed. | |
|
262 | ||
|
263 | %autoreload 2 | |
|
264 | Reload all modules (except those excluded by %aimport) every time | |
|
265 | before executing the Python code typed. | |
|
266 | ||
|
267 | Reloading Python modules in a reliable way is in general | |
|
268 | difficult, and unexpected things may occur. %autoreload tries to | |
|
269 | work around common pitfalls by replacing function code objects and | |
|
270 | parts of classes previously in the module with new versions. This | |
|
271 | makes the following things to work: | |
|
272 | ||
|
273 | - Functions and classes imported via 'from xxx import foo' are upgraded | |
|
274 | to new versions when 'xxx' is reloaded. | |
|
275 | ||
|
276 | - Methods and properties of classes are upgraded on reload, so that | |
|
277 | calling 'c.foo()' on an object 'c' created before the reload causes | |
|
278 | the new code for 'foo' to be executed. | |
|
279 | ||
|
280 | Some of the known remaining caveats are: | |
|
281 | ||
|
282 | - Replacing code objects does not always succeed: changing a @property | |
|
283 | in a class to an ordinary method or a method to a member variable | |
|
284 | can cause problems (but in old objects only). | |
|
285 | ||
|
286 | - Functions that are removed (eg. via monkey-patching) from a module | |
|
287 | before it is reloaded are not upgraded. | |
|
288 | ||
|
289 | - C extension modules cannot be reloaded, and so cannot be | |
|
290 | autoreloaded. | |
|
291 | ||
|
292 | """ | |
|
293 | if parameter_s == '': | |
|
294 | self._reloader.check(True) | |
|
295 | elif parameter_s == '0': | |
|
296 | self._enabled = False | |
|
297 | elif parameter_s == '1': | |
|
298 | self._reloader.check_all = False | |
|
299 | self._enabled = True | |
|
300 | elif parameter_s == '2': | |
|
301 | self._reloader.check_all = True | |
|
302 | self._enabled = True | |
|
303 | ||
|
304 | def magic_aimport(self, ipself, parameter_s=''): | |
|
305 | """%aimport => Import modules for automatic reloading. | |
|
306 | ||
|
307 | %aimport | |
|
308 | List modules to automatically import and not to import. | |
|
309 | ||
|
310 | %aimport foo | |
|
311 | Import module 'foo' and mark it to be autoreloaded for %autoreload 1 | |
|
312 | ||
|
313 | %aimport -foo | |
|
314 | Mark module 'foo' to not be autoreloaded for %autoreload 1 | |
|
315 | ||
|
316 | """ | |
|
317 | ||
|
318 | modname = parameter_s | |
|
319 | if not modname: | |
|
320 | to_reload = self._reloader.modules.keys() | |
|
321 | to_reload.sort() | |
|
322 | to_skip = self._reloader.skip_modules.keys() | |
|
323 | to_skip.sort() | |
|
324 | if self._reloader.check_all: | |
|
325 | print "Modules to reload:\nall-expect-skipped" | |
|
326 | else: | |
|
327 | print "Modules to reload:\n%s" % ' '.join(to_reload) | |
|
328 | print "\nModules to skip:\n%s" % ' '.join(to_skip) | |
|
329 | elif modname.startswith('-'): | |
|
330 | modname = modname[1:] | |
|
331 | try: | |
|
332 | del self._reloader.modules[modname] | |
|
333 | except KeyError: | |
|
334 | pass | |
|
335 | self._reloader.skip_modules[modname] = True | |
|
336 | else: | |
|
337 | try: | |
|
338 | del self._reloader.skip_modules[modname] | |
|
339 | except KeyError: | |
|
340 | pass | |
|
341 | self._reloader.modules[modname] = True | |
|
309 | 342 | |
|
310 | %aimport foo | |
|
311 | Import module 'foo' and mark it to be autoreloaded for %autoreload 1 | |
|
343 | # Inject module to user namespace; handle also submodules properly | |
|
344 | __import__(modname) | |
|
345 | basename = modname.split('.')[0] | |
|
346 | mod = sys.modules[basename] | |
|
347 | ipself.push({basename: mod}) | |
|
312 | 348 | |
|
313 | %aimport -foo | |
|
314 | Mark module 'foo' to not be autoreloaded for %autoreload 1 | |
|
315 | 349 | |
|
316 | """ | |
|
350 | _loaded = False | |
|
317 | 351 | |
|
318 | modname = parameter_s | |
|
319 | if not modname: | |
|
320 | to_reload = reloader.modules.keys() | |
|
321 | to_reload.sort() | |
|
322 | to_skip = reloader.skip_modules.keys() | |
|
323 | to_skip.sort() | |
|
324 | if reloader.check_all: | |
|
325 | print "Modules to reload:\nall-expect-skipped" | |
|
326 | else: | |
|
327 | print "Modules to reload:\n%s" % ' '.join(to_reload) | |
|
328 | print "\nModules to skip:\n%s" % ' '.join(to_skip) | |
|
329 | elif modname.startswith('-'): | |
|
330 | modname = modname[1:] | |
|
331 | try: del reloader.modules[modname] | |
|
332 | except KeyError: pass | |
|
333 | reloader.skip_modules[modname] = True | |
|
334 | else: | |
|
335 | try: del reloader.skip_modules[modname] | |
|
336 | except KeyError: pass | |
|
337 | reloader.modules[modname] = True | |
|
338 | ||
|
339 | # Inject module to user namespace; handle also submodules properly | |
|
340 | __import__(modname) | |
|
341 | basename = modname.split('.')[0] | |
|
342 | mod = sys.modules[basename] | |
|
343 | ip.push({basename: mod}) | |
|
344 | ||
|
345 | def init(): | |
|
346 | ip.define_magic('autoreload', autoreload_f) | |
|
347 | ip.define_magic('aimport', aimport_f) | |
|
348 | ip.set_hook('pre_runcode_hook', runcode_hook) | |
|
349 | ||
|
350 | init() | |
|
352 | def load_ipython_extension(ip): | |
|
353 | """Load the extension in IPython.""" | |
|
354 | global _loaded | |
|
355 | if not _loaded: | |
|
356 | plugin = Autoreload(shell=ip, config=ip.config) | |
|
357 | ip.plugin_manager.register_plugin('autoreload', plugin) | |
|
358 | _loaded = True |
General Comments 0
You need to be logged in to leave comments.
Login now