summaryrefslogtreecommitdiffstats
path: root/Doc/library/contextlib.rst
diff options
context:
space:
mode:
authorFilipe Laíns <lains@riseup.net>2021-10-19 22:19:27 (GMT)
committerGitHub <noreply@github.com>2021-10-19 22:19:27 (GMT)
commit3592980f9122ab0d9ed93711347742d110b749c2 (patch)
treee1e3a4894a60187a5152c2a8c3ae648987f5f75b /Doc/library/contextlib.rst
parentad6d162e518963711d24c80f1b7d6079bd437584 (diff)
downloadcpython-3592980f9122ab0d9ed93711347742d110b749c2.zip
cpython-3592980f9122ab0d9ed93711347742d110b749c2.tar.gz
cpython-3592980f9122ab0d9ed93711347742d110b749c2.tar.bz2
bpo-25625: add contextlib.chdir (GH-28271)
Added non parallel-safe :func:`~contextlib.chdir` context manager to change the current working directory and then restore it on exit. Simple wrapper around :func:`~os.chdir`. Signed-off-by: Filipe Laíns <lains@riseup.net> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Doc/library/contextlib.rst')
-rw-r--r--Doc/library/contextlib.rst21
1 files changed, 19 insertions, 2 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index bc38a63..ae0ee72 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -353,6 +353,23 @@ Functions and classes provided:
.. versionadded:: 3.5
+.. function:: chdir(path)
+
+ 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
+ non-linear code execution, like generators, where the program execution is
+ temporarily relinquished -- unless explicitely desired, you should not yield
+ when this context manager is active.
+
+ This is a simple wrapper around :func:`~os.chdir`, it changes the current
+ working directory upon entering and restores the old one on exit.
+
+ This context manager is :ref:`reentrant <reentrant-cms>`.
+
+ .. versionadded:: 3.11
+
+
.. class:: ContextDecorator()
A base class that enables a context manager to also be used as a decorator.
@@ -900,8 +917,8 @@ but may also be used *inside* a :keyword:`!with` statement that is already
using the same context manager.
:class:`threading.RLock` is an example of a reentrant context manager, as are
-:func:`suppress` and :func:`redirect_stdout`. Here's a very simple example of
-reentrant use::
+:func:`suppress`, :func:`redirect_stdout`, and :func:`chdir`. Here's a very
+simple example of reentrant use::
>>> from contextlib import redirect_stdout
>>> from io import StringIO