On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson <cs@cskk.id.au> wrote:
On 14Sep2021 21:43, M.-A. Lemburg <mal@egenix.com> wrote:
- The context manager is not thread safe. There's no thread safe model for the current work dir. OTOH, scripts usually don't use threads, so not a big deal.
This is the source of my concerns. Though of course it applies to any process global state. It would need this stated up front in big letters (as of course does chdir itself). My real concern is that this can leak into other functions whose use then makes whatever uses them to become inherently and unrepairably not thread safe.
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.
I think what would be more useful is a context manager which worked on a threading.local which pushed/popped a reference directory path, and had an open() method which used that (and friends for other pathname based functions).
Hmm. If it's going to push/pop directories, it should probably work with file descriptors rather than path names, and effectively turn all open calls into openat calls. I'm currently not sure what the best way to do that in Python is. ChrisA