I think this would be convenient.

And yes, it's not thread safe. But neither is os.chdir() to start with. Someone whose script, or library, wants to chdir can already shoot themselves in the foot. This makes that slightly less likely, not more.

In terms of the bikeshed color, I think putting this in `pathlib` is the best approach for "modern Python."


On Tue, Sep 14, 2021, 7:37 PM Cameron Simpson <cs@cskk.id.au> wrote:
On 15Sep2021 07:50, Chris Angelico <rosuav@gmail.com> wrote:
>On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson <cs@cskk.id.au> wrote:
>> I know I'm atypical, but I have quite a lot of multithreaded stuff,
>> including command line code. So while it'd be ok to avoid this context
>> manager for my own code, I fear library modules, either stdlib or pypi,
>> quietly using this in their code, making them unuseable in the general
>> case. Unrepairably unuseable, for the user.
>
>Library code shouldn't be changing the working directory, context
>manager or not. That belongs to the application.

Entirely agree.

I'm concerned that convenient stackable chdir is a bug magnet, and would
creep into library code. Maybe not in the stdlib, but there's no point
writing such a context manager if it isn't goingg to be used, and
therefore it could get used in library code. Imagine when a popular pypi
module starts using it internally and breaks a multithreaded app
previously relying on it?