GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-25625: add contextlib.chdir by FFY00 · Pull Request #28271 · python/cpython

Conversation

@FFY00

@FFY00 FFY00 commented Sep 10, 2021 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This is probably the single snippet of code I find myself re-implementing the
most in projects. Not being thread safe is not optimal, but there isn't
really any good way to do so, and that does not negate the huge
usefulness of this function.

Signed-off-by: Filipe Laíns [email protected]

https://bugs.python.org/issue25625

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@FFY00
FFY00 requested a review from 1st1 as a code owner September 10, 2021 14:33
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Sep 10, 2021
@bedevere-bot bedevere-bot added the awaiting review label Sep 10, 2021
@FFY00
bpo-25625: add contextlib.chdir
1d1f1e8
This is probably the single snippet of code I find myself re-implementing the
most in projects. Not being thread safe is not optimal, but there isn't
really any good way to do so, and that does not negate the huge
usefulness of this function.

Signed-off-by: Filipe Laíns <[email protected]>
@FFY00
FFY00 force-pushed the bpo-25625 branch from da4b329 to 1d1f1e8 Compare September 10, 2021 14:52
kalvdans
kalvdans reviewed Sep 10, 2021
View reviewed changes
Comment thread Lib/test/test_contextlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

ambv
ambv reviewed Sep 10, 2021
View reviewed changes
Comment thread Lib/test/test_contextlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ambv
Ensure own exception
b6d9e3e
ambv
ambv reviewed Sep 10, 2021
View reviewed changes
Comment thread Doc/library/contextlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ambv
Use Oxford comma
cd63024
kalvdans
kalvdans reviewed Sep 10, 2021
View reviewed changes
Comment thread Lib/test/test_contextlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

FFY00
FFY00 commented Sep 10, 2021
View reviewed changes
Comment thread Lib/test/test_contextlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ambv @FFY00
Update Lib/test/test_contextlib.py
6bdf5bb
Co-authored-by: Filipe Laíns <[email protected]>
serhiy-storchaka
serhiy-storchaka reviewed Sep 15, 2021
View reviewed changes
Comment thread Doc/library/contextlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_contextlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@FFY00
Serhiy's feedback
e30c9d8
Signed-off-by: Filipe Laíns <[email protected]>
tiran
tiran reviewed Sep 15, 2021
View reviewed changes

@tiran tiran left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm -1 to add this feature.

The presence of a chdir decorator in Python's stdlib promotes the idea that the feature is a good standard practice. However it is ill-advised to change process global state such as the working directory in most cases.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tiran tiran mentioned this pull request Sep 15, 2021
Please decide on chdir() / workdir() context manager python/steering-council#77
Closed
serhiy-storchaka
serhiy-storchaka reviewed Sep 15, 2021
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it looks correct to me. I am not sure that such feature should be added, although the can of worms was opened by redirect_stdout().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

FFY00 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@warsaw

warsaw commented Sep 16, 2021

Copy link
Copy Markdown
Member

I agree with the comment that it can be problematic, but OTOH it's very common to reimplement this code over and over. There's something to be said for providing a solid reference implementation that at least does the best you can do, but with big red warnings for when it can go wrong. Not having this in the stdlib isn't going to stop people from doing it anyway.

ambv reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ambv

ambv commented Sep 17, 2021

Copy link
Copy Markdown
Contributor

The presence of a chdir decorator in Python's stdlib promotes the idea that the feature is a good standard practice. However it is ill-advised to change process global state such as the working directory in most cases.

I disagree. os.chdir exists since forever, it "promotes" the idea the same way as a context manager would. If anything, the context manager is a better way to do it, since it will at least return to the previous state.

Sure, any global modifications of the environment are potential anti-patterns, like monkey-patching standard input/output that Serhiy already mentioned (see BPO-30511 for an egregious example why that is a problem).

That being said, again, os.chdir() is a popular facility of the language, used in our unit tests many times. Using a context manager for it would make more sense. In fact, we already do support that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ambv
Merge main to work around BPO-45220 unrelated Windows failures
0cb2d8b
@1st1

1st1 commented Sep 17, 2021

Copy link
Copy Markdown
Member

I'm really not sure this should be part of contextlib (or the stdlib). I'd just add this as a recipe to the docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

FFY00 added 2 commits September 18, 2021 18:39
@FFY00
refeer that the cm is not parallel-safe in the news entry
9ad9398
Signed-off-by: Filipe Laíns <[email protected]>
@FFY00
fix typo in news
ce10241
Signed-off-by: Filipe Laíns <[email protected]>
@FFY00

FFY00 commented Oct 18, 2021

Copy link
Copy Markdown
Member Author

Per python/steering-council#77, it seems that addition of such a context manager was approved, granted that we have an appropriate warning.
I think the current warning is good enough, does anyone have suggestions?

Otherwise, this PR is now unblocked 🙂

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

warsaw
warsaw reviewed Oct 18, 2021
View reviewed changes
Comment thread Lib/contextlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ambv
ambv merged commit 3592980 into python:main Oct 19, 2021
@bedevere-bot bedevere-bot removed the awaiting review label Oct 19, 2021
graingert
graingert reviewed Oct 20, 2021
View reviewed changes
Comment thread Doc/library/contextlib.rst

Non parallel-safe context manager to change the current working directory.
As this changes a global state, the working directory, it is not suitable
for use in most threaded or aync contexts. It is also not suitable for most

@graingert graingert Oct 20, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo on aync #29091

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@graingert graingert mentioned this pull request Oct 23, 2021
add contextlib.chdir python/typeshed#6191
Merged
jcfr added a commit to jcfr/Slicer that referenced this pull request Apr 29, 2022
@jcfr
ENH: Add slicer.util.chdir context manager
13dfeb2
This commit implements a non thread-safe context manager to change
the current working directory.

Available in Python 3.11 as ``contextlib.chdir`` and adapted from
python/cpython#28271.
@jcfr jcfr mentioned this pull request Apr 29, 2022
ENH: Add slicer.util.chdir context manager Slicer/Slicer#6341
Merged
jcfr added a commit to Slicer/Slicer that referenced this pull request Apr 29, 2022
@jcfr
ENH: Add slicer.util.chdir context manager
5cab260
This commit implements a non thread-safe context manager to change
the current working directory.

Available in Python 3.11 as ``contextlib.chdir`` and adapted from
python/cpython#28271.
@AlexGaynor AlexGaynor mannequin mentioned this pull request Apr 10, 2022
shutil.make_archive should not need to chdir (alternatively: make shutil.make_archive thread-safe) #74696
Closed
@hamelsmu hamelsmu mentioned this pull request Jul 18, 2022
add context manager for changing directories AnswerDotAI/fastcore#436
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ambv ambv ambv left review comments
@warsaw warsaw warsaw left review comments
@graingert graingert graingert left review comments
@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@1st1 1st1 Awaiting requested review from 1st1
+2 more reviewers
@tiran tiran tiran left review comments
@kalvdans kalvdans kalvdans left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

10 participants

@FFY00 @warsaw @ambv @1st1 @graingert @tiran @kalvdans @serhiy-storchaka @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.