From f0d4018a8a24fa810387c76957652a666f8bf4f7 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Mon, 5 Apr 2021 14:53:18 +0200 Subject: [PATCH] Do not import pathlib2 as pathlib if available - pathlib2 is now considered to have the same functionality as pathlib - fixes broken new pathlib functionality if pathlib2 is installed - see #592 --- CHANGES.md | 8 ++- pyfakefs/extra_packages.py | 5 -- pyfakefs/fake_filesystem_unittest.py | 13 ++-- pyfakefs/fake_pathlib.py | 5 +- pyfakefs/pytest_tests/example.py | 12 +--- .../tests/all_tests_without_extra_packages.py | 10 +--- pyfakefs/tests/dynamic_patch_test.py | 2 +- .../tests/fake_filesystem_unittest_test.py | 2 +- pyfakefs/tests/fake_pathlib_test.py | 60 +++---------------- 9 files changed, 27 insertions(+), 90 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 22076dd8..207d2402 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,10 +3,16 @@ The released versions correspond to PyPi releases. ## Version 4.5.0 (as yet unreleased) +### Changes + * `pathlib2` is still supported, but considered to have the same + functionality as `pathlib` and is no longer tested separately; + the previous behavior broke newer `pathlib` features if `pathlib2` + was installed (see [#592](../../issues/592)) + ## [Version 4.4.0](https://pypi.python.org/pypi/pyfakefs/4.4.0) (2021-02-24) Adds better support for Python 3.8 / 3.9. -#### New Features +### New Features * added support for `pathlib.Path.link_to` (new in Python 3.8) (see [#580](../../issues/580)) * added support for `pathlib.Path.readlink` (new in Python 3.9) diff --git a/pyfakefs/extra_packages.py b/pyfakefs/extra_packages.py index f60a8d48..d45c8541 100644 --- a/pyfakefs/extra_packages.py +++ b/pyfakefs/extra_packages.py @@ -16,13 +16,8 @@ try: import pathlib2 - - pathlib = pathlib2 except ImportError: pathlib2 = None - import pathlib - - pathlib = pathlib try: import scandir diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index ed4e5d4c..c29f5510 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -64,7 +64,7 @@ from pyfakefs import fake_filesystem_shutil from pyfakefs import fake_pathlib from pyfakefs import mox3_stubout -from pyfakefs.extra_packages import pathlib, pathlib2, use_scandir +from pyfakefs.extra_packages import pathlib2, use_scandir if use_scandir: from pyfakefs import fake_scandir @@ -504,6 +504,7 @@ def _init_fake_module_classes(self): 'os': fake_filesystem.FakeOsModule, 'shutil': fake_filesystem_shutil.FakeShutilModule, 'io': fake_filesystem.FakeIoModule, + 'pathlib': fake_pathlib.FakePathlibModule } if IS_PYPY: # in PyPy io.open, the module is referenced as _io @@ -512,13 +513,9 @@ def _init_fake_module_classes(self): # class modules maps class names against a list of modules they can # be contained in - this allows for alternative modules like # `pathlib` and `pathlib2` - self._class_modules['Path'] = [] - if pathlib: - self._fake_module_classes[ - 'pathlib'] = fake_pathlib.FakePathlibModule - self._class_modules['Path'].append('pathlib') - self._unfaked_module_classes[ - 'pathlib'] = fake_pathlib.RealPathlibModule + self._class_modules['Path'] = ['pathlib'] + self._unfaked_module_classes[ + 'pathlib'] = fake_pathlib.RealPathlibModule if pathlib2: self._fake_module_classes[ 'pathlib2'] = fake_pathlib.FakePathlibModule diff --git a/pyfakefs/fake_pathlib.py b/pyfakefs/fake_pathlib.py index 12a3fd4b..b43b1784 100644 --- a/pyfakefs/fake_pathlib.py +++ b/pyfakefs/fake_pathlib.py @@ -32,12 +32,13 @@ import fnmatch import functools import os +import pathlib import re import sys from urllib.parse import quote_from_bytes as urlquote_from_bytes from pyfakefs import fake_scandir -from pyfakefs.extra_packages import use_scandir, pathlib, pathlib2 +from pyfakefs.extra_packages import use_scandir from pyfakefs.fake_filesystem import FakeFileOpen, FakeFilesystem @@ -493,7 +494,7 @@ def resolve(self, strict=None): Raises: OSError: if the path doesn't exist (strict=True or Python < 3.6) """ - if sys.version_info >= (3, 6) or pathlib2: + if sys.version_info >= (3, 6): if strict is None: strict = False else: diff --git a/pyfakefs/pytest_tests/example.py b/pyfakefs/pytest_tests/example.py index 62d619af..c5b530b3 100644 --- a/pyfakefs/pytest_tests/example.py +++ b/pyfakefs/pytest_tests/example.py @@ -12,14 +12,6 @@ # Used as SUT for pytest_fixture_test.py -try: - from pathlib2 import Path +from pathlib import Path - EXAMPLE_FILE = Path('/test') / 'file' -except ImportError: - try: - from pathlib import Path - - EXAMPLE_FILE = Path('/test') / 'file' - except ImportError: - EXAMPLE_FILE = None +EXAMPLE_FILE = Path('/test') / 'file' diff --git a/pyfakefs/tests/all_tests_without_extra_packages.py b/pyfakefs/tests/all_tests_without_extra_packages.py index 28ecfad1..3d65e9d4 100644 --- a/pyfakefs/tests/all_tests_without_extra_packages.py +++ b/pyfakefs/tests/all_tests_without_extra_packages.py @@ -11,21 +11,13 @@ # limitations under the License. """A test suite that runs all tests for pyfakefs at once. -Excludes tests using external pathlib2 and scandir packages.""" +Excludes tests using external scandir package.""" import sys import unittest from pyfakefs import extra_packages -if extra_packages.pathlib2: - extra_packages.pathlib2 = None - try: - import pathlib - except ImportError: - pathlib = None - extra_packages.pathlib = pathlib - if extra_packages.use_scandir_package: extra_packages.use_scandir_package = False try: diff --git a/pyfakefs/tests/dynamic_patch_test.py b/pyfakefs/tests/dynamic_patch_test.py index 5bad7f53..132dd0a4 100644 --- a/pyfakefs/tests/dynamic_patch_test.py +++ b/pyfakefs/tests/dynamic_patch_test.py @@ -13,10 +13,10 @@ """ Tests for patching modules loaded after `setUpPyfakefs()`. """ +import pathlib import unittest from pyfakefs import fake_filesystem_unittest -from pyfakefs.extra_packages import pathlib class TestPyfakefsUnittestBase(fake_filesystem_unittest.TestCase): diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 824c0942..e9d72c91 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -21,6 +21,7 @@ import io import multiprocessing import os +import pathlib import runpy import shutil import sys @@ -34,7 +35,6 @@ import pyfakefs.tests.import_as_example import pyfakefs.tests.logsio from pyfakefs import fake_filesystem_unittest, fake_filesystem -from pyfakefs.extra_packages import pathlib from pyfakefs.fake_filesystem import OSType from pyfakefs.fake_filesystem_unittest import ( Patcher, Pause, patchfs, PatchMode diff --git a/pyfakefs/tests/fake_pathlib_test.py b/pyfakefs/tests/fake_pathlib_test.py index 28eea1cc..5dcc57f9 100644 --- a/pyfakefs/tests/fake_pathlib_test.py +++ b/pyfakefs/tests/fake_pathlib_test.py @@ -22,11 +22,11 @@ import errno import os +import pathlib import stat import sys import unittest -from pyfakefs.extra_packages import pathlib, pathlib2 from pyfakefs.fake_filesystem import is_root from pyfakefs import fake_pathlib, fake_filesystem @@ -35,20 +35,10 @@ is_windows = sys.platform == 'win32' -def skip_if_pathlib_36_not_available(): - if sys.version_info < (3, 6) and not pathlib2: - raise unittest.SkipTest('Changed behavior in Python 3.6') - - -def skip_if_pathlib_36_is_available(): - if sys.version_info >= (3, 6) or pathlib2: - raise unittest.SkipTest('Changed behavior in Python 3.6') - - class RealPathlibTestCase(RealFsTestCase): def __init__(self, methodName='runTest'): super(RealPathlibTestCase, self).__init__(methodName) - self.pathlib = pathlib or pathlib2 + self.pathlib = pathlib self.path = None def setUp(self): @@ -238,8 +228,8 @@ def test_relative_to(self): with self.assertRaises(ValueError): self.path('passwd').relative_to('/usr') - @unittest.skipIf(sys.version_info < (3, 9) or pathlib2, - 'readlink new in Python 3.9') + @unittest.skipIf(sys.version_info < (3, 9), + 'is_relative_to new in Python 3.9') def test_is_relative_to(self): path = self.path('/etc/passwd') self.assertTrue(path.is_relative_to('/etc')) @@ -418,11 +408,6 @@ def test_resolve(self): self.os.path.realpath( self.make_path('antoine', 'setup.py')))) - def test_resolve_nonexisting_file(self): - skip_if_pathlib_36_is_available() - path = self.path('/foo/bar') - self.assert_raises_os_error(errno.ENOENT, path.resolve) - def test_stat_file_in_unreadable_dir(self): self.check_posix_only() dir_path = self.make_path('some_dir') @@ -448,25 +433,7 @@ def test_iterdir_in_unreadable_dir(self): path = str(list(iter)[0]) self.assertTrue(path.endswith('some_file')) - @unittest.skipIf(not is_windows, 'Windows specific behavior') - def test_resolve_file_as_parent_windows(self): - skip_if_pathlib_36_is_available() - self.check_windows_only() - self.create_file(self.make_path('a_file')) - path = self.path(self.make_path('a_file', 'this can not exist')) - self.assert_raises_os_error(errno.ENOENT, path.resolve) - - @unittest.skipIf(is_windows, 'POSIX specific behavior') - def test_resolve_file_as_parent_posix(self): - skip_if_pathlib_36_is_available() - self.check_posix_only() - self.create_file(self.make_path('a_file')) - path = self.path( - self.make_path('', 'a_file', 'this can not exist')) - self.assert_raises_os_error(errno.ENOTDIR, path.resolve) - - def test_resolve_nonexisting_file_after_36(self): - skip_if_pathlib_36_not_available() + def test_resolve_nonexisting_file(self): path = self.path( self.make_path('/path', 'to', 'file', 'this can not exist')) self.assertEqual(path, path.resolve()) @@ -644,7 +611,7 @@ def test_symlink_to(self): self.assertTrue(self.os.path.exists(link_name)) self.assertTrue(path.is_symlink()) - @unittest.skipIf(sys.version_info < (3, 8) or pathlib2, + @unittest.skipIf(sys.version_info < (3, 8), 'link_to new in Python 3.8') def test_link_to(self): self.skip_if_symlink_not_supported() @@ -658,20 +625,7 @@ def test_link_to(self): self.assertFalse(path.is_symlink()) self.assertEqual(2, self.os.stat(file_name).st_nlink) - @unittest.skipIf(sys.version_info < (3, 8) or not pathlib2, - 'pathlib2 has no link_to') - def test_pathlib2_does_not_have_link_to(self): - self.skip_if_symlink_not_supported() - file_name = self.make_path('foo', 'bar.txt') - self.create_file(file_name) - self.assertEqual(1, self.os.stat(file_name).st_nlink) - link_name = self.make_path('link_to_bar') - path = self.path(file_name) - with self.assertRaises(AttributeError): - path.link_to(link_name) - self.assertFalse(self.os.path.exists(link_name)) - - @unittest.skipIf(sys.version_info < (3, 9) or pathlib2, + @unittest.skipIf(sys.version_info < (3, 9), 'readlink new in Python 3.9') def test_readlink(self): self.skip_if_symlink_not_supported()