This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: "chdir" Contex manager for pathlib
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: 4-launchpad-kalvdans-no-ip-org, FFY00, Jáchym Barvínek, cameron, eric.araujo, gaborjbernat, graingert, lukasz.langa, matrixise, pitrou, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-11-14 17:45 by Jáchym Barvínek, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28271 merged FFY00, 2021-09-10 14:33
PR 29091 merged graingert, 2021-10-20 13:38
Messages (14)
msg254664 - (view) Author: Jáchym Barvínek (Jáchym Barvínek) Date: 2015-11-14 17:45
I use this context manager in my code:

@contextmanager
def in_directory(path):
    pwd = str(Path().absolute())
    if not path.is_dir():
        path = path.parent
    os.chdir(str(path))
    yield path.absolute()
    os.chdir(pwd)

I thought it would be nice to have something like this in the pathlib as a method of Path library, I find it quite convenient, especially when dealing with subprocesses.
msg254665 - (view) Author: desbma (desbma) * Date: 2015-11-14 18:58
I'm not a Python core developer, but a few thoughts:
* this is not thread safe
* subprocess.Popen already has a cwd parameter for similar purpose
msg254667 - (view) Author: Jáchym Barvínek (Jáchym Barvínek) Date: 2015-11-14 19:11
I did not know about cwd for Popen. It seems like the better way to go.
Thank you.

2015-11-14 19:58 GMT+01:00 desbma <report@bugs.python.org>:

>
> desbma added the comment:
>
> I'm not a Python core developer, but a few thoughts:
> * this is not thread safe
> * subprocess.Popen already has a cwd parameter for similar purpose
>
> ----------
> nosy: +desbma
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue25625>
> _______________________________________
>
msg255003 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-20 17:47
What's the relation with subprocess.Popen ?
msg255004 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-11-20 18:14
I agree with desbma.
msg255005 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-11-20 18:17
> What's the relation with subprocess.Popen ?

In the context of subprocesses there is better way to run a program in specified working directory that temporary change current working directory in parent process.
msg255012 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-20 19:32
> In the context of subprocesses there is better way to run a program in specified working directory that temporary change current working directory in parent process.

Ok, haven't read the last line of the description. thanks for the
clarification.
-- 
Stéphane Wirtel - http://wirtel.be - @matrixise
msg367947 - (view) Author: gaborjbernat (gaborjbernat) * Date: 2020-05-02 20:03
So moving way from subprocess.Process; what about having this for other use cases? I know there is no thread-safe way to get this working, given it's a process state variable. However, can we have it as a non-thread-safe feature?
msg404366 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-19 22:19
New changeset 3592980f9122ab0d9ed93711347742d110b749c2 by Filipe Laíns in branch 'main':
bpo-25625: add contextlib.chdir (GH-28271)
https://github.com/python/cpython/commit/3592980f9122ab0d9ed93711347742d110b749c2
msg404368 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-19 22:21
Thanks for perservering, Filipe! ✨ 🍰 ✨ 

Per Steering Council decision, the context manager is approved: https://github.com/python/steering-council/issues/77
msg404455 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-20 13:51
New changeset a774285e7d3e63c24dbaaee0b0d9e59ded5c49bf by Thomas Grainger in branch 'main':
bpo-25625: [doc] fix async/aync typo (GH-29091)
https://github.com/python/cpython/commit/a774285e7d3e63c24dbaaee0b0d9e59ded5c49bf
msg406333 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2021-11-15 01:02
There is renewed discussion on python-dev about placing this in contextlib.
msg406334 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2021-11-15 01:04
Can you share the link? I haven't seen anything recent. Is it under other thread?
msg406335 - (view) Author: Cameron Simpson (cameron) * Date: 2021-11-15 01:41
On 15Nov2021 01:04, Python Bug Reports <report@bugs.python.org> wrote:
>Can you share the link? I haven't seen anything recent. Is it under 
>other thread?

It's in the discuss-ideas part of discuss.python.org during a 
discussions about a possible new context manager to atomically prepare a 
filename. Latest comment:

    https://discuss.python.org/t/adding-atomicwrite-in-stdlib/11899/15

and I'm with Inada-san here: I think shutil is a better fit for both the 
chdir context manager and the thing under discussion. They're both "high 
level shell-like things" to my mind.
History
Date User Action Args
2022-04-11 14:58:23adminsetgithub: 69811
2021-11-15 01:41:48cameronsetmessages: + msg406335
2021-11-15 01:04:17FFY00setmessages: + msg406334
2021-11-15 01:02:09eric.araujosetnosy: + eric.araujo
messages: + msg406333
2021-10-20 14:08:25desbmasetnosy: - desbma
2021-10-20 13:51:11lukasz.langasetmessages: + msg404455
2021-10-20 13:38:36graingertsetnosy: + graingert

pull_requests: + pull_request27360
2021-10-19 22:21:48lukasz.langasetresolution: rejected -> fixed
messages: + msg404368
versions: + Python 3.11, - Python 3.9
2021-10-19 22:19:38lukasz.langasetnosy: + lukasz.langa
messages: + msg404366
2021-09-15 21:13:01cameronsetnosy: + cameron
2021-09-10 14:33:57FFY00setpull_requests: + pull_request26691
2021-05-20 21:48:24FFY00setnosy: + FFY00
2021-03-15 08:08:024-launchpad-kalvdans-no-ip-orgsetnosy: + 4-launchpad-kalvdans-no-ip-org
2020-05-02 20:03:06gaborjbernatsetnosy: + gaborjbernat

messages: + msg367947
versions: + Python 3.9, - Python 3.6
2015-11-20 19:32:53matrixisesetmessages: + msg255012
2015-11-20 18:17:59serhiy.storchakasetmessages: + msg255005
2015-11-20 18:14:34serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg255004

resolution: rejected
stage: resolved
2015-11-20 17:47:05matrixisesetnosy: + matrixise
messages: + msg255003
2015-11-14 19:11:06Jáchym Barvíneksetmessages: + msg254667
2015-11-14 18:58:23desbmasetnosy: + desbma
messages: + msg254665
2015-11-14 17:46:56SilentGhostsetnosy: + pitrou

versions: + Python 3.6, - Python 3.5
2015-11-14 17:45:43Jáchym Barvínekcreate