Skip to content

Commit cfdd18b

Browse files
committed
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: pytest-dev/pytest#4787 (comment)
1 parent 60f50bd commit cfdd18b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

py/_path/common.py

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def fspath(path):
3737
import pathlib
3838
except ImportError:
3939
pass
40+
except FileNotFoundError: # Might happen in py34.
41+
pass
4042
else:
4143
if isinstance(path, pathlib.PurePath):
4244
return py.builtin.text(path)

testing/path/test_local.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,31 @@ def test_eq_with_strings(self, path1):
174174
assert path2 != path3
175175

176176
def test_eq_with_none(self, path1):
177-
assert path1 != None # noqa
177+
assert path1 != None # noqa: E711
178+
179+
@pytest.mark.skipif(
180+
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
181+
)
182+
@pytest.mark.skipif(
183+
sys.version_info < (3, 0) or sys.version_info >= (3, 5),
184+
reason="only with Python 3 before 3.5"
185+
)
186+
def test_eq_with_none_and_custom_fspath(self, monkeypatch, path1):
187+
import os
188+
import shutil
189+
import tempfile
190+
191+
d = tempfile.mkdtemp()
192+
os.chdir(d)
193+
shutil.rmtree(d)
194+
195+
monkeypatch.delitem(sys.modules, 'pathlib', raising=False)
196+
monkeypatch.setattr(sys, 'path', [''] + sys.path)
197+
198+
with pytest.raises(FileNotFoundError):
199+
import pathlib # noqa: F401
200+
201+
assert path1 != None # noqa: E711
178202

179203
def test_eq_non_ascii_unicode(self, path1):
180204
path2 = path1.join(u'temp')

0 commit comments

Comments
 (0)