PySnooper/tests
CodingSelim e7f8db78ab report variable changes from the last line in a with block
variable changes are diffed and printed on the next trace event. the last
line of a with block has no next event before __exit__, so its new/modified
variables were dropped on python < 3.10 (3.10+ emits an extra block-exit line
event that flushes them).

flush the pending changes in __exit__, but only when a later event hasn't
already captured the final state. track a per-frame 'pending' flag: a body
line leaves changes pending, while the block-exit line event and non-line
events (exception/return) clear it. so:
- 3.10+ normal exit: the block-exit line event clears pending -> no double
  eval of repr/watch/custom_repr.
- exceptional exit: skipped entirely (the exception events already captured,
  and this avoids replacing the user's exception from a callback).
- one-line 'with snoop(): x=1': no trace event at all, so we still flush.
- same with statement reused in a loop: the with line is resolved from
  f_lasti (reliable) instead of f_lineno at __enter__ (which can be a stale
  body line on 3.8/3.9), so every iteration's final value is reported.

cleanup runs in finally so a failing callback can't strand frame references.

tests: final line that modifies an existing var and creates a new one, the
one-line body, no double repr eval, exceptional exit (exception survives, no
extra snapshot, state cleaned), and a with block reused in a loop. ran the
full suite on 3.9, 3.11 and 3.14.
2026-07-12 09:59:41 +03:00
..
mini_toolbox Get rid of six, for reals 2021-02-27 11:04:37 +02:00
samples Show colored output 2022-01-14 20:36:54 +02:00
test_multiple_files Show colored output 2022-01-14 20:36:54 +02:00
test_utils Add support for Ansible zipped source files (#226) 2022-04-02 17:58:48 +03:00
__init__.py Add indentation tests with new assert_sample_output 2019-05-11 09:38:09 +03:00
test_chinese.py Show colored output 2022-01-14 20:36:54 +02:00
test_ended_by_exception.py Add generator close regression test 2026-06-08 11:57:40 -07:00
test_mini_toolbox.py Fix bug in OutputCapturer 2020-05-05 14:18:18 +03:00
test_not_implemented.py Show colored output 2022-01-14 20:36:54 +02:00
test_pysnooper.py report variable changes from the last line in a with block 2026-07-12 09:59:41 +03:00
utils.py Make tests compatible with Python 3.10 2021-05-19 11:21:58 +03:00