Skip to content

Commit

Permalink
Merge pull request #207 from blueyed/py34-fspath-filenotfound
Browse files Browse the repository at this point in the history
path.common: handle FileNotFoundError when trying to import pathlib
  • Loading branch information
nicoddemus committed Apr 5, 2019
2 parents 60f50bd + 95a2085 commit 2802b18
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
Unreleased
==========

- handle ``FileNotFoundError`` when trying to import pathlib in ``path.common``
on Python 3.4 (#207).

1.8.0 (2019-02-21)
==================

Expand Down
8 changes: 7 additions & 1 deletion py/_path/common.py
Expand Up @@ -10,6 +10,12 @@
# Moved from local.py.
iswin32 = sys.platform == "win32" or (getattr(os, '_name', False) == 'nt')

try:
# FileNotFoundError might happen in py34, and is not available with py27.
import_errors = (ImportError, FileNotFoundError)
except NameError:
import_errors = (ImportError,)

try:
from os import fspath
except ImportError:
Expand All @@ -35,7 +41,7 @@ def fspath(path):
raise
try:
import pathlib
except ImportError:
except import_errors:
pass
else:
if isinstance(path, pathlib.PurePath):
Expand Down
26 changes: 25 additions & 1 deletion testing/path/test_local.py
Expand Up @@ -174,7 +174,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()
monkeypatch.chdir(d)
shutil.rmtree(d)

monkeypatch.delitem(sys.modules, 'pathlib', raising=False)
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 2802b18

Please sign in to comment.