Tests often need to temporarily change something and always restore it, even if the test raises. Build simple versions of unittest.mock.patch.
Implement two context managers:
patch_attr(obj, name, value): set obj.name = value inside the block. Afterwards restore the old value, or delete the attribute if it didn't exist before.patch_dict(d, updates, clear=False): apply updates (and empty the dict first if clear). Afterwards restore the dict to exactly its original contents, in the same object, even if the block itself modified the dict.Both must restore state when the block raises, and must not swallow the exception. patch_attr should yield the new value, and patch_dict should yield the dict.