mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 17:15:35 +00:00
The initial technique to implement `record_on_exception=False` was to not emptying the generator returned by `CassetteContextDecorator._patch_generator` when an exception happens to skip the `cassette._save` call, however this had the side effect of not emptying the `ExitStack` created inside the generator which contains the `_patch.__exit__` calls to remove the patches. This was innocuous in CPython, which uses a reference counting garbage collector so the `ExitStack` was immediately collected after losing scope and therefore its `__exit__` method executed. Pypy, on the other hand, uses a generational garbage collector so its objects may survive more time, enough for the `ExitStack` not called until much later, which may cause the patches to live more than expected when `record_on_exception=False`. This was found because the test `test_nesting_context_managers_by_checking_references_of_http_connection` was failing because it was executed after `test_dont_record_on_exception`. Now the cassette instance is saved inside the `CassetteContextDecorator` instance to have better control on where to save the cassette, and moved the `cassette._save` call from the `_patch_generator` method to the `__exit__` method to be free to empty the generator and remove the patches always.
14 KiB
14 KiB