From 3b86205be3e35fad7f0285064a3ce9cd0732c9f3 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 13 Feb 2019 21:49:14 +0100 Subject: [PATCH] path.common: handle FileNotFoundError when trying to import pathlib Python 3.4 might raise FileNotFoundError due to `os.getcwd()` failing on a non-existing cwd. This is fixed in Python 3.5. Ref: https://github.com/pytest-dev/pytest/pull/4787#issuecomment-463341251 --- py/_path/common.py | 2 ++ testing/path/test_local.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/py/_path/common.py b/py/_path/common.py index 2d490b56..958f98fb 100644 --- a/py/_path/common.py +++ b/py/_path/common.py @@ -37,6 +37,8 @@ def fspath(path): import pathlib except ImportError: pass + except FileNotFoundError: # Might happen in py34. + pass else: if isinstance(path, pathlib.PurePath): return py.builtin.text(path) diff --git a/testing/path/test_local.py b/testing/path/test_local.py index af64086d..7a5001a1 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -161,7 +161,31 @@ def test_eq_with_strings(self, path1): assert path2 != path3 def test_eq_with_none(self, path1): - assert path1 != None # noqa + assert path1 != None # noqa: E711 + + @pytest.mark.skipif( + sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" + ) + @pytest.mark.skipif( + sys.version_info < (3, 0) or sys.version_info >= (3, 5), + reason="only with Python 3 before 3.5" + ) + def test_eq_with_none_and_custom_fspath(self, monkeypatch, path1): + import os + import shutil + import tempfile + + d = tempfile.mkdtemp() + os.chdir(d) + shutil.rmtree(d) + + del sys.modules['pathlib'] + monkeypatch.setattr(sys, 'path', [''] + sys.path) + + with pytest.raises(FileNotFoundError): + import pathlib # noqa: F401 + + assert path1 != None # noqa: E711 def test_eq_non_ascii_unicode(self, path1): path2 = path1.join(u'temp')