bpo-25625: add contextlib.chdir by FFY00 · Pull Request #28271 · python/cpython (original) (raw)

The presence of a chdir decorator in Python's stdlib promotes the idea that the feature is a good standard practice. However it is ill-advised to change process global state such as the working directory in most cases.

I disagree. os.chdir exists since forever, it "promotes" the idea the same way as a context manager would. If anything, the context manager is a better way to do it, since it will at least return to the previous state.

Sure, any global modifications of the environment are potential anti-patterns, like monkey-patching standard input/output that Serhiy already mentioned (see BPO-30511 for an egregious example why that is a problem).

That being said, again, os.chdir() is a popular facility of the language, used in our unit tests many times. Using a context manager for it would make more sense. In fact, we already do support that.