On 15/09/2021 09.21, Eric V. Smith wrote:
> On 9/15/2021 3:02 AM, Christian Heimes wrote:
>> On 15/09/2021 01.55, Guido van Rossum wrote:
>>> I know where I'd file a bug. :-)
>>>
>>> "Bug magnet" is an extremely subjective pejorative term. When the
>>> *better* way to do things (os.workdir()) is harder than the *easy* way
>>> to do (os.chdir()), which is the real bug magnet?
>> The "better way" to handle current working directory is to use the
>> modern *at() variants of syscalls, e.g. openat() instead open(). The
>> variants take an additional file descriptor dirfd that is used as the
>> current working directory for the syscall.
> 
> While I generally agree, the only times I've written a context manager
> like os.workdir() is when running an executable with subprocess.call(),
> and the executable requires that its current directory be set to some
> specific directory. So while I don't use this functionality very often,
> there are times when nothing else will do. I realize I could handle this
> temporary working directory with yet another executable (including a
> shell), but using a context manager is just easier, and I only use this
> in single-threaded programs.

You don't have to change the current working directory of your process
in order to run a child process in a different working directory.
subprocess.call() and other functions in the subprocess module accept a
"cwd" argument that lets you run a process with a different working
directory. The "cwd" argument is thread safe.


> And I'm not crazy about the name "workdir". To me, it sounds like it
> returns something, not sets and resets something. But I don't have a
> particularly great alternative in mind: in my own code I've used
> "change_dir", which isn't awesome either.

Speaking with almost 25 years experience in Unix system scripting:
Changing the current working directory during the runtime of a process
is problematic and can lead to bugs. In general applications should only
change their working directory once right after start and not rely on
the cwd. It's better to normalize paths, use absolute paths, or *at()
syscalls with dirfd.

In my opinion a workdir() context manager would only be useful for small
quick-n-dirty scripts.

Christian


_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DGGZHKSJGVRBRARZF7T63FFR2OOGF4XC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to