Context manager fixes (#2450)
* Ban use of `os.*env`
* Fix `clear_environment` to actually clear environment variables
Assigning to `os.environ` does not clear the environment (Ruff B003)
* Have environment context managers restore state even if the block raises
* Add tests for environment CMs