Skip to content

Commit

Permalink
Do not import pathlib2 as pathlib if available
Browse files Browse the repository at this point in the history
- pathlib2 is now considered to have the same functionality as pathlib
- fixes broken new pathlib functionality if pathlib2 is installed
- see pytest-dev#592
  • Loading branch information
mrbean-bremen committed Apr 5, 2021
1 parent 15c3522 commit f0d4018
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 90 deletions.
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions pyfakefs/extra_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@

try:
import pathlib2

pathlib = pathlib2
except ImportError:
pathlib2 = None
import pathlib

pathlib = pathlib

try:
import scandir
Expand Down
13 changes: 5 additions & 8 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
12 changes: 2 additions & 10 deletions pyfakefs/pytest_tests/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
10 changes: 1 addition & 9 deletions pyfakefs/tests/all_tests_without_extra_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/tests/dynamic_patch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io
import multiprocessing
import os
import pathlib
import runpy
import shutil
import sys
Expand All @@ -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
Expand Down
60 changes: 7 additions & 53 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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')
Expand All @@ -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())
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit f0d4018

Please sign in to comment.