When using either "ipython
" or "code.interact(local=locals())
", I'd like to have a way to save the entire program address space into a pickle file, and similarly a way to load such a file and then start executing in that context.
This should be totally possible for simple scripts because of virtual memory.
All defined names (and the non-orphaned objects they point to), from locals up through globals and global functions, would be pickled. When unpickling, those names would be assigned again, at their original scope.
Assume that the program doesn't use the network, so state discontinuity there is avoided.
Assume that the program is fault-tolerant with regards to the perceived discontitunity of the system clock, so no problem there either.
The only challenge seems to be what to do with file descriptors. Ideally, if this doesn't exist already, there should be a simple "file descriptor pickle" function that gets the mode bits it is open with, and the postion of the file cursor, and a checksum of the file contents (erroring if the checksums don't match when trying to unpickle).
Are there a few lines that will accomplish this "pickling the entire session"?
To do this, I'd use dill, which can serialize almost anything in python.
Dill registers it's types into the
pickle
registry, so if you have some black box code that usespickle
and you can't really edit it, then just importing dill can magically make it work without monkeypatching the 3rd party code.You also wanted to pickle the whole interpreter session... dill can do that too.
Dill also has some good tools for helping you understand what is causing your pickling to fail when your code fails.