Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
The pdb interact command#
Today Carlton told me about the interact command in the Python debugger.
Here’s how to use it with pytest (but it works anywhere else where you find yourself in a pdb session).
Use pytest --pdb to cause pytest to open a debugger at the first failed assertion (I added assert False to my test suite to demonstrate this).
Then type interact to drop into a full Python interactive prompt that keeps all of the local and global variables from the debugger:
1% pytest -k test_drop --pdb2======== test session starts ========3platform darwin -- Python 3.10.3, pytest-7.1.3, pluggy-1.0.04...5> assert False6E assert False7
8tests/test_api_write.py:272: AssertionError9>>>> entering PDB >>>>10
11>>>> PDB post_mortem (IO-capturing turned off) >>>>12> /Users/simon/Dropbox/Development/datasette/tests/test_api_write.py(272)test_drop_table()13-> assert False14(Pdb) interact15>>> locals().keys()16dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__file__', '__cached__', '__builtins__',17 '@py_builtins', '@pytest_ar', 'Datasette', 'sqlite3', 'pytest', 'time', 'ds_write', 'write_token', 'test_write_row',18 'test_write_rows', 'test_write_row_errors', 'test_delete_row', 'test_drop_table', 'scenario', 'token', 'should_work',19 'path', 'response', '@py_assert0', '@py_format2'])Crucially, once you are in the interactive prompt you can inspect local variables with names like s and c without accidentally triggering matching debugger commands.
Hit Ctrl+D to exit back to the debugger:
1>>> <Ctrl+D>2now exiting InteractiveConsole...3(pdb)