Skip to content

Commit

Permalink
path.common: handle FileNotFoundError when trying to import pathlib
Browse files Browse the repository at this point in the history
Python 3.4 might raise FileNotFoundError due to `os.getcwd()` failing on
a non-existing cwd.  This is fixed in Python 3.5.

Ref: pytest-dev/pytest#4787 (comment)
  • Loading branch information
blueyed committed Feb 22, 2019
1 parent 7c2b288 commit 9960b4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions py/_path/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion testing/path/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

monkeypatch.delitem(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')
Expand Down

0 comments on commit 9960b4c

Please sign in to comment.