Skip to content

Commit e30c9d8

Browse files
committed
Serhiy's feedback
Signed-off-by: Filipe Laíns <lains@riseup.net>
1 parent 6bdf5bb commit e30c9d8

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

Doc/library/contextlib.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,12 @@ Functions and classes provided:
355355

356356
.. function:: chdir(path)
357357

358-
Non thread-safe context manager to change the current working directory.
358+
Non parallel-safe context manager to change the current working directory.
359+
As this changes a global state, the working directory, it is not suitable
360+
for use in most threaded or aync contexts. It is also not suitable for most
361+
non-linear code execution, like generators, where the program execution is
362+
temporarily relinquished -- unless explicitely desired, you should not yield
363+
when this context manager is active.
359364

360365
This is a simple wrapper around :func:`~os.chdir`, it changes the current
361366
working directory upon entering and restores the old one on exit.

Lib/test/test_contextlib.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,41 +1086,41 @@ class TestChdir(unittest.TestCase):
10861086
def test_simple(self):
10871087
old_cwd = os.getcwd()
10881088
target = os.path.join(os.path.dirname(__file__), 'data')
1089-
assert old_cwd != target
1089+
self.assertNotEqual(old_cwd, target)
10901090

10911091
with chdir(target):
1092-
assert os.getcwd() == target
1093-
assert os.getcwd() == old_cwd
1092+
self.assertEqual(os.getcwd(), target)
1093+
self.assertEqual(os.getcwd(), old_cwd)
10941094

10951095
def test_reentrant(self):
10961096
old_cwd = os.getcwd()
10971097
target1 = os.path.join(os.path.dirname(__file__), 'data')
10981098
target2 = os.path.join(os.path.dirname(__file__), 'ziptestdata')
1099-
assert old_cwd not in (target1, target2)
1099+
self.assertNotIn(old_cwd, (target1, target2))
11001100
chdir1, chdir2 = chdir(target1), chdir(target2)
11011101

11021102
with chdir1:
1103-
assert os.getcwd() == target1
1103+
self.assertEqual(os.getcwd(), target1)
11041104
with chdir2:
1105-
assert os.getcwd() == target2
1105+
self.assertEqual(os.getcwd(), target2)
11061106
with chdir1:
1107-
assert os.getcwd() == target1
1108-
assert os.getcwd() == target2
1109-
assert os.getcwd() == target1
1110-
assert os.getcwd() == old_cwd
1107+
self.assertEqual(os.getcwd(), target1)
1108+
self.assertEqual(os.getcwd(), target2)
1109+
self.assertEqual(os.getcwd(), target1)
1110+
self.assertEqual(os.getcwd(), old_cwd)
11111111

11121112
def test_exception(self):
11131113
old_cwd = os.getcwd()
11141114
target = os.path.join(os.path.dirname(__file__), 'data')
1115-
assert old_cwd != target
1115+
self.assertNotEqual(old_cwd, target)
11161116

11171117
try:
11181118
with chdir(target):
1119-
assert os.getcwd() == target
1119+
self.assertEqual(os.getcwd(), target)
11201120
raise RuntimeError("boom")
11211121
except RuntimeError as re:
1122-
assert str(re) == "boom"
1123-
assert os.getcwd() == old_cwd
1122+
self.assertEqual(str(re), "boom")
1123+
self.assertEqual(os.getcwd(), old_cwd)
11241124

11251125

11261126
if __name__ == "__main__":

0 commit comments

Comments
 (0)