Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"chdir" Contex manager for pathlib #69811

Closed
JchymBarvnek mannequin opened this issue Nov 14, 2015 · 14 comments
Closed

"chdir" Contex manager for pathlib #69811

JchymBarvnek mannequin opened this issue Nov 14, 2015 · 14 comments
Labels
3.11 bug and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@JchymBarvnek
Copy link
Mannequin

JchymBarvnek mannequin commented Nov 14, 2015

BPO 25625
Nosy @pitrou, @cameron-simpson, @merwok, @ambv, @serhiy-storchaka, @graingert, @matrixise, @gaborbernat, @FFY00
PRs
  • bpo-25625: add contextlib.chdir #28271
  • bpo-25625: fix async/aync typo #29091
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2015-11-20.18:14:34.391>
    created_at = <Date 2015-11-14.17:45:43.295>
    labels = ['type-feature', 'library', '3.11']
    title = '"chdir" Contex manager for pathlib'
    updated_at = <Date 2021-11-15.01:41:48.603>
    user = 'https://bugs.python.org/JchymBarvnek'

    bugs.python.org fields:

    activity = <Date 2021-11-15.01:41:48.603>
    actor = 'cameron'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-11-20.18:14:34.391>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2015-11-14.17:45:43.295>
    creator = 'J\xc3\xa1chym Barv\xc3\xadnek'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 25625
    keywords = []
    message_count = 14.0
    messages = ['254664', '254665', '254667', '255003', '255004', '255005', '255012', '367947', '404366', '404368', '404455', '406333', '406334', '406335']
    nosy_count = 11.0
    nosy_names = ['pitrou', 'cameron', 'eric.araujo', 'lukasz.langa', 'serhiy.storchaka', 'graingert', 'matrixise', 'J\xc3\xa1chym Barv\xc3\xadnek', 'gaborjbernat', '4-launchpad-kalvdans-no-ip-org', 'FFY00']
    pr_nums = ['28271', '29091']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue25625'
    versions = ['Python 3.11']

    @JchymBarvnek
    Copy link
    Mannequin Author

    JchymBarvnek mannequin commented Nov 14, 2015

    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.

    @JchymBarvnek JchymBarvnek mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 14, 2015
    @desbma
    Copy link
    Mannequin

    desbma mannequin commented Nov 14, 2015

    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

    @JchymBarvnek
    Copy link
    Mannequin Author

    JchymBarvnek mannequin commented Nov 14, 2015

    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\>


    @matrixise
    Copy link
    Member

    What's the relation with subprocess.Popen ?

    @serhiy-storchaka
    Copy link
    Member

    I agree with desbma.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @matrixise
    Copy link
    Member

    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

    @gaborbernat
    Copy link
    Mannequin

    gaborbernat mannequin commented May 2, 2020

    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?

    @gaborbernat gaborbernat mannequin added the 3.9 only security fixes label May 2, 2020
    @ambv
    Copy link
    Contributor

    ambv commented Oct 19, 2021

    New changeset 3592980 by Filipe Laíns in branch 'main':
    bpo-25625: add contextlib.chdir (GH-28271)
    3592980

    @ambv
    Copy link
    Contributor

    ambv commented Oct 19, 2021

    Thanks for perservering, Filipe! ✨ 🍰 ✨

    Per Steering Council decision, the context manager is approved: python/steering-council#77

    @ambv ambv added 3.11 bug and security fixes and removed 3.9 only security fixes labels Oct 19, 2021
    @ambv
    Copy link
    Contributor

    ambv commented Oct 20, 2021

    New changeset a774285 by Thomas Grainger in branch 'main':
    bpo-25625: [doc] fix async/aync typo (GH-29091)
    a774285

    @merwok
    Copy link
    Member

    merwok commented Nov 15, 2021

    There is renewed discussion on python-dev about placing this in contextlib.

    @FFY00
    Copy link
    Member

    FFY00 commented Nov 15, 2021

    Can you share the link? I haven't seen anything recent. Is it under other thread?

    @cameron-simpson
    Copy link
    Mannequin

    cameron-simpson mannequin commented Nov 15, 2021

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 bug and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants