Skip to content

Commit 555a905

Browse files
committed
FIX: Revert directory when exception raised
1 parent 5c10c92 commit 555a905

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

nipype/utils/filemanip.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,5 +923,7 @@ def relpath(path, start=None):
923923
def indirectory(path):
924924
cwd = os.getcwd()
925925
os.chdir(path)
926-
yield
927-
os.chdir(cwd)
926+
try:
927+
yield
928+
finally:
929+
os.chdir(cwd)

nipype/utils/tests/test_filemanip.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,9 @@ def test_indirectory(tmpdir):
515515
assert os.getcwd() == sd2
516516
assert os.getcwd() == sd1
517517
assert os.getcwd() == tmpdir.strpath
518+
try:
519+
with indirectory('subdir1'):
520+
raise ValueError("Erroring out of context")
521+
except ValueError:
522+
pass
523+
assert os.getcwd() == tmpdir.strpath

0 commit comments

Comments
 (0)