| 424 | |
| 425 | @magics_class |
| 426 | class AutoreloadMagics(Magics): |
| 427 | def __init__(self, *a, **kw): |
| 428 | super(AutoreloadMagics, self).__init__(*a, **kw) |
| 429 | self._reloader = ModuleReloader() |
| 430 | self._reloader.check_all = False |
| 431 | self.loaded_modules = set(sys.modules) |
| 432 | |
| 433 | @line_magic |
| 434 | def autoreload(self, parameter_s=''): |
| 435 | r"""%autoreload => Reload modules automatically |
| 436 | |
| 437 | %autoreload |
| 438 | Reload all modules (except those excluded by %aimport) automatically |
| 439 | now. |
| 440 | |
| 441 | %autoreload 0 |
| 442 | Disable automatic reloading. |
| 443 | |
| 444 | %autoreload 1 |
| 445 | Reload all modules imported with %aimport every time before executing |
| 446 | the Python code typed. |
| 447 | |
| 448 | %autoreload 2 |
| 449 | Reload all modules (except those excluded by %aimport) every time |
| 450 | before executing the Python code typed. |
| 451 | |
| 452 | Reloading Python modules in a reliable way is in general |
| 453 | difficult, and unexpected things may occur. %autoreload tries to |
| 454 | work around common pitfalls by replacing function code objects and |
| 455 | parts of classes previously in the module with new versions. This |
| 456 | makes the following things to work: |
| 457 | |
| 458 | - Functions and classes imported via 'from xxx import foo' are upgraded |
| 459 | to new versions when 'xxx' is reloaded. |
| 460 | |
| 461 | - Methods and properties of classes are upgraded on reload, so that |
| 462 | calling 'c.foo()' on an object 'c' created before the reload causes |
| 463 | the new code for 'foo' to be executed. |
| 464 | |
| 465 | Some of the known remaining caveats are: |
| 466 | |
| 467 | - Replacing code objects does not always succeed: changing a @property |
| 468 | in a class to an ordinary method or a method to a member variable |
| 469 | can cause problems (but in old objects only). |
| 470 | |
| 471 | - Functions that are removed (eg. via monkey-patching) from a module |
| 472 | before it is reloaded are not upgraded. |
| 473 | |
| 474 | - C extension modules cannot be reloaded, and so cannot be |
| 475 | autoreloaded. |
| 476 | |
| 477 | """ |
| 478 | if parameter_s == '': |
| 479 | self._reloader.check(True) |
| 480 | elif parameter_s == '0': |
| 481 | self._reloader.enabled = False |
| 482 | elif parameter_s == '1': |
| 483 | self._reloader.check_all = False |
no outgoing calls