An enhanced reload module from PyDev
Project description
This is a reload library based on pydevd_reload.py from https://github.com/fabioz/PyDev.Debugger. The original library is used in PyDev & PyCharm.
Installation
If you don’t have much time, just perform
$ pip install pydevd_reload
which will install the module(without tests) on your system.
Also, you can just copy & paste the pydevd_reload.py which require no third-party dependency.
Usage
Just import pydevd_reload and use pydevd_reload.xreload as the reload built-in function.
import pydevd_reload
pydevd_reload.xreload(module_instance)
Update code by default
A running python program consists of code logic and corresponding data. Code logic is the logic of what the program perform. Corresponding data is the environment of logic running.
A program may contain modules, classes and functions.
A module may contain classes and functions.
A class may contain functions.
So, function is the basic logic structure of a program. The code logic hides in the high-level function object, reloading is based on replacing the code object of function object. The running environment may change, which means it’s probably dangerous to manipulate logic relevant data, so pydevd_reload provide custom hooks which allow data updates in demands.
pydevd_reload don’t recreate the old namespace from new classes. Rather, it keeps the existing namespace, load a new version of it and update only some of the things pydevd_reload can inplace. That way, pydevd_reload don’t break things such as singletons or end up with a second representation of the same class in memory.
If pydevd_reload find it to be a __metaclass__, then try to update it as a regular class.
pydevd_reload don’t remove old attributes (and leave them lying around even if they’re no longer used).
Reload hooks were changed
These changes make it more stable, especially in the common case (where in a debug session only the contents of a function are changed), besides providing flexibility for users that want to extend on it.
Update data by custom hooks
pydevd_reload reloads code objects in a module by default, and provides __xreload_old_new__ and __xreload_after_reload_update__ custom hooks which allow user to update data in a module. The hook functions can also be hot updated in a reload.
To participate in the change of some attribute:
In a module:
__xreload_old_new__(namespace, name, old, new): pass
in a class:
@classmethod def __xreload_old_new__(cls, name, old, new): pass
A class or module may include a method called ‘__xreload_old_new__’ which is called when pydevd_reload is unable to reload a given attribute.
To do something after the whole reload is finished:
In a module:
def __xreload_after_reload_update__(old_namespace, new_namespace): pass
In a class:
@classmethod def __xreload_after_reload_update__(cls, old_namespace, new_namespace): pass
A class or module may include a method called ‘__xreload_after_reload_update__’ which is called after the reload finishes.
Important: when providing a hook, always use the namespace or cls provided and not anything in the global namespace, as the global namespace are only temporarily created during the reload and may not reflect the actual application state (while the cls and namespace passed are).
Improvements
This standalone pydevd_reload library has following improvements than original pydevd_reload.py:
Removed pydevd dependency.
Removed limitation that functions with decorators cannot be handled. *
Added support to update function closure. *
Added support to update callable object. *
Added support to update function annotation.
Added code object name check to avoid update monkey patched code. *
Refined reload hooks. (eg, __xreload_old_new__ and __xreload_after_reload_update__)
Fixed __file__ attribute in namespace so they will be updated.
Fixed compiled python file support.
Fixed reload failed when python code compiled from different paths.
Limitations
Attributes/constants are added, but not changed (so singletons and the application state is not broken – use provided hooks to workaround it).
Code using metaclasses may not always work.
Renamings are not handled correctly.
Dependent modules are not reloaded.
New __slots__ can’t be added to existing classes.
Testing
If you have the source code you can run the tests with
$ python pydevd_reload/tests/test_pydevd_reload.py
Repository
The project is hosted on GitHub. You can look at the source here:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydevd_reload-1.2.tar.gz
.
File metadata
- Download URL: pydevd_reload-1.2.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0c440dcd7f78b916b8bb089dd97b887c67ccff1848092c9382aaa1356b8dfca |
|
MD5 | d3c6714d18833be00d8a1d5cc0b4005a |
|
BLAKE2b-256 | fd4cb528d11d797795872e3404102cf6cc546cf875fd9965f1f4d2e189ff7ba7 |
File details
Details for the file pydevd_reload-1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: pydevd_reload-1.2-py2.py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48cdea86aa4b58fb28f9c205a2b89ff54c7ad1ed65928b9b9a57f2b78729e665 |
|
MD5 | 42911f4bbda33c02590135baa6c69bb7 |
|
BLAKE2b-256 | a81e52ada85da475c74d06d7caa594a6cc234cd6143e23d81d9b1592a8cc5a55 |