-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Description
Feature or enhancement
Proposal:
We add an ABC ModificationContext
to contextlib
:
ModificationContext
specializes the functionality of AbstractContextManager
, for context managers designed to change the value of a state or attribute on entry, and revert it on exit. We have noticed that this style is used by many existing and proposed context managers.
The following classes already present in contextlib
are to be refactored to inherit from ModificationContext
(with no compatibility issues)
contextlib._RedirectStream
, parent ofredirect_stdout
andredirect_stderr
contextlib.chdir
After this addition, the following issues are rebased, so that their additions inherit from ModificationContext
:
- gh-128432: Add
shutil.umask
- TBC: Add
shutil.seteuid
andshutil.setegid
- gh-128432: Move
contextlib.chdir
toshutil.chdir
This addition makes those first two PRs trivial, and contextlib.chdir
is refactored whether it is moved to shutil
or not.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
Thread started by @ncoghlan
https://discuss.python.org/t/adding-a-contextlib-modificationcontext-base-class/76322/2