Package: pyteleport
A proof-of-concept serialization, transmission and restoring python runtime.Aboutpyteleportis capable of making snapshots of python runtime from
(almost) aribtrary state, including locals, globals and stack.
It then transforms snapshots into specially designed bytecode that
resumes execution from the snapshot state.
The bytecode can be run remotely: this waypyteleportteleports
python runtime.Installpip install pyteleportExampleNote that the two outputs were produced by different processes on different machines! This is whattp_bashdoes: it transmits the runtime from one python process to another (remotely).Also works from within a stack:defa():defb():defc():result="hello"tp_bash(...)returnresult+" world"returnlen(c())+float("3.5")return5*(3+b())asserta()==87.5APIfrompyteleportimporttp_shelldeftp_shell(*shell_args,python="python",before="cd $(mktemp -d)",pyc_fn="payload.pyc",shell_delimiter="; ",pack_file=bash_inline_create_file,pack_object=dumps,unpack_object=portable_loads,detect_interactive=True,files=None,stack_method=None,_frame=None,**kwargs):"""Teleport into another shell.Parameters----------shell_argsArguments to start a new shell.python : strPython executable in the shell.before : str, listShell commands to be run before running python.pyc_fn : strTemporary filename to save the bytecode to.shell_delimiter : strShell delimiter to chain multiple commands.pack_file : CallableA function `f(name, contents)` turning a fileinto a shell-friendly assembly.pack_object : Callable, NoneA method (serializer) turning objects into byteslocally.unpack_object : Callable, NoneA method (deserializer) turning bytes into objectsremotely. It does not have to rely on globals.detect_interactive : boolIf True, attempts to detect the interactive modeand to open an interactive session remotely whilepiping stdio into this python process.files : listA list of files to teleport alongside.stack_methodStack collection method._frameThe frame to collect.kwargsOther arguments to `subprocess.run`."""How it worksYou invoketeleportin your python script.pyteleportcollects the runtime state: globals, locals, stack.pyteleportdumps the runtime into a specially designed "morph" bytecode
which resumes from whereteleportwas invoked.The bytecode is transmitted to the target environment and passed to a
python interpreter there.The remote python runs the bytecode which restores the runtime state.
The python program casually resumes from where it was interrupted.The local python runtime is terminated and simply pipes stdio from the
target environment.Known limitationsThis is a proof of concept.
The package works with cPythonv3.8, 3.9, or 3.10.What is implemented:MWE: snapshot, serialize, transmit, restoreserialize generatorsyield fromthreads (currently ignored)block stack:for,try,withasync(non-python stack; needs further investigation)forking to remote (possible with bytecode sstack prediction)back-teleport (needs API development)nested teleport (needs minimal changes)cross-fork communications (need API development)REPL integrationdetecting non-python stack (peek into past value stack?)Won't fix:non-python stack (not possible)cross-version (too fragile)LicenseLICENSE.md