From 623b42574c37e088e220ebd7557e6915e0b2749e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 15 Feb 2019 18:29:47 +0100 Subject: [PATCH] path.local: as_cwd: do not chdir to None [ci skip] --- CHANGELOG | 3 +++ py/_path/local.py | 9 ++++++--- testing/path/test_local.py | 13 +++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 94c06345..ed60b0a0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,9 @@ - add ``"importlib"`` pyimport mode for python3.5+, allowing unimportable test suites to contain identically named modules. +- fix ``LocalPath.as_cwd()`` not calling ``os.chdir()`` with ``None``, when + being invoked from a non-existing directory. + 1.7.0 (2018-10-11) ================== diff --git a/py/_path/local.py b/py/_path/local.py index 3ac31945..0e856a66 100644 --- a/py/_path/local.py +++ b/py/_path/local.py @@ -579,14 +579,17 @@ def chdir(self): @contextmanager def as_cwd(self): - """ return context manager which changes to current dir during the - managed "with" context. On __enter__ it returns the old dir. + """ + Return a context manager, which changes to the path's dir during the + managed "with" context. + On __enter__ it returns the old dir, which might be ``None``. """ old = self.chdir() try: yield old finally: - old.chdir() + if old is not None: + old.chdir() def realpath(self): """ return a new path which contains no symbolic links.""" diff --git a/testing/path/test_local.py b/testing/path/test_local.py index 2b8519c5..d52409e5 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -125,6 +125,19 @@ def test_chdir_gone(self, path1): assert path1.chdir() is None assert os.getcwd() == str(path1) + with pytest.raises(py.error.ENOENT): + with p.as_cwd(): + raise NotImplementedError + + @skiponwin32 + def test_chdir_gone_in_as_cwd(self, path1): + p = path1.ensure("dir_to_be_removed", dir=1) + p.chdir() + p.remove() + + with path1.as_cwd() as old: + assert old is None + def test_as_cwd(self, path1): dir = path1.ensure("subdir", dir=1) old = py.path.local()