MCPcopy Create free account
hub / github.com/ipython/ipython / superreload

Function superreload

IPython/extensions/autoreload.py:359–417  ·  view source on GitHub ↗

Enhanced version of the builtin reload function. 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

(module, reload=reload, old_objects=None)

Source from the content-addressed store, hash-verified

357
358
359def superreload(module, reload=reload, old_objects=None):
360 """Enhanced version of the builtin reload function.
361
362 superreload remembers objects previously in the module, and
363
364 - upgrades the class dictionary of every old class in the module
365 - upgrades the code object of every old function and method
366 - clears the module's namespace before reloading
367
368 """
369 if old_objects is None:
370 old_objects = {}
371
372 # collect old objects in the module
373 for name, obj in list(module.__dict__.items()):
374 if not hasattr(obj, '__module__') or obj.__module__ != module.__name__:
375 continue
376 key = (module.__name__, name)
377 try:
378 old_objects.setdefault(key, []).append(weakref.ref(obj))
379 except TypeError:
380 pass
381
382 # reload module
383 try:
384 # clear namespace first from old cruft
385 old_dict = module.__dict__.copy()
386 old_name = module.__name__
387 module.__dict__.clear()
388 module.__dict__['__name__'] = old_name
389 module.__dict__['__loader__'] = old_dict['__loader__']
390 except (TypeError, AttributeError, KeyError):
391 pass
392
393 try:
394 module = reload(module)
395 except:
396 # restore module dictionary on failed reload
397 module.__dict__.update(old_dict)
398 raise
399
400 # iterate over all objects and update functions & classes
401 for name, new_obj in list(module.__dict__.items()):
402 key = (module.__name__, name)
403 if key not in old_objects: continue
404
405 new_refs = []
406 for old_ref in old_objects[key]:
407 old_obj = old_ref()
408 if old_obj is None: continue
409 new_refs.append(old_ref)
410 update_generic(old_obj, new_obj)
411
412 if new_refs:
413 old_objects[key] = new_refs
414 else:
415 del old_objects[key]
416

Callers 1

checkMethod · 0.85

Calls 4

reloadFunction · 0.85
update_genericFunction · 0.85
copyMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected