From 33afe63b23f938c7e9c5b98ab99c75502617f924 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 13 Feb 2019 21:49:14 +0100 Subject: [PATCH 1/3] 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 d52409e5..863157d6 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -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') From a64cb3f9e4d866920257e4d0435d3e51189fdd99 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 14 Feb 2019 19:55:13 +0100 Subject: [PATCH 2/3] Handle missing FileNotFoundError Might not really be necessary, since py27 triggers the ImportError always already, but better to be safe. --- py/_path/common.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/py/_path/common.py b/py/_path/common.py index 958f98fb..2364e5fe 100644 --- a/py/_path/common.py +++ b/py/_path/common.py @@ -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: @@ -35,9 +41,7 @@ def fspath(path): raise try: import pathlib - except ImportError: - pass - except FileNotFoundError: # Might happen in py34. + except import_errors: pass else: if isinstance(path, pathlib.PurePath): From 95a20856a2b57c8ab5c3a60f3c81a6a771f85284 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 5 Apr 2019 22:48:07 +0200 Subject: [PATCH 3/3] changelog [ci skip] --- CHANGELOG | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 886d8936..be9dceeb 100644 --- a/CHANGELOG +++ b/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) ==================