pyppin.testing.interact¶
A function to open an interactive debugging REPL anywhere in your code.
Functions
|
Open an interactive REPL at any given point in your code. |
- pyppin.testing.interact.interact(banner: Optional[str] = None, _test_commands: Optional[Iterable[str]] = None) None [source]¶
Open an interactive REPL at any given point in your code.
This REPL will be able to see, but not modify, any local and global variables visible from the point at which you invoke it. (If you do modify them, these modifications will be thrown out when the REPL exits.) That means that it gives you a lot of the flexibility and power of a debugger without the overhead of running your entire program in a debugging environment.
For example, your code might compute some value in a complicated way, and then use it. Rather than using printf debugging, you can use interactive debugging:
def someFunction(...): # ... Compute foo interact() # Instead of print(f"Foo: {foo}") # ... Use foo
When the code reaches interact(), it will stop and pop open a Python shell, from which you can simply examine the value of foo (by typing ‘foo’).
If you exit the shell with a ^D, your program will resume executing. If you exit the shell by calling quit() or exit(), it will abort the entire program (by raising SystemExit).
- Parameters
banner – If given, a banner to show at the start of the interactive environment.
_test_commands – UNITTESTING ARGUMENT ONLY! Simulates user input as a list of commands.
Helpful tip: If you’re running a test under tox and are getting an error that stdin can’t be read, but want to interact() to debug something from inside a unittest, change your tox.ini so that the test command is ‘pytest -s’. That’s not what you usually want – it disables stdout capture as well – but it will let you interactively probe your tests!
FUTURE NOTE: A read-write interacting environment may become possible in future versions of Python; see PEP 558, which would be required in order to implement this.