Skip to content

Commit f0d4018

Browse files
committed
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
1 parent 15c3522 commit f0d4018

9 files changed

+27
-90
lines changed

Diff for: CHANGES.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ The released versions correspond to PyPi releases.
33

44
## Version 4.5.0 (as yet unreleased)
55

6+
### Changes
7+
* `pathlib2` is still supported, but considered to have the same
8+
functionality as `pathlib` and is no longer tested separately;
9+
the previous behavior broke newer `pathlib` features if `pathlib2`
10+
was installed (see [#592](../../issues/592))
11+
612
## [Version 4.4.0](https://pypi.python.org/pypi/pyfakefs/4.4.0) (2021-02-24)
713
Adds better support for Python 3.8 / 3.9.
814

9-
#### New Features
15+
### New Features
1016
* added support for `pathlib.Path.link_to` (new in Python 3.8)
1117
(see [#580](../../issues/580))
1218
* added support for `pathlib.Path.readlink` (new in Python 3.9)

Diff for: pyfakefs/extra_packages.py

-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@
1616

1717
try:
1818
import pathlib2
19-
20-
pathlib = pathlib2
2119
except ImportError:
2220
pathlib2 = None
23-
import pathlib
24-
25-
pathlib = pathlib
2621

2722
try:
2823
import scandir

Diff for: pyfakefs/fake_filesystem_unittest.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from pyfakefs import fake_filesystem_shutil
6565
from pyfakefs import fake_pathlib
6666
from pyfakefs import mox3_stubout
67-
from pyfakefs.extra_packages import pathlib, pathlib2, use_scandir
67+
from pyfakefs.extra_packages import pathlib2, use_scandir
6868

6969
if use_scandir:
7070
from pyfakefs import fake_scandir
@@ -504,6 +504,7 @@ def _init_fake_module_classes(self):
504504
'os': fake_filesystem.FakeOsModule,
505505
'shutil': fake_filesystem_shutil.FakeShutilModule,
506506
'io': fake_filesystem.FakeIoModule,
507+
'pathlib': fake_pathlib.FakePathlibModule
507508
}
508509
if IS_PYPY:
509510
# in PyPy io.open, the module is referenced as _io
@@ -512,13 +513,9 @@ def _init_fake_module_classes(self):
512513
# class modules maps class names against a list of modules they can
513514
# be contained in - this allows for alternative modules like
514515
# `pathlib` and `pathlib2`
515-
self._class_modules['Path'] = []
516-
if pathlib:
517-
self._fake_module_classes[
518-
'pathlib'] = fake_pathlib.FakePathlibModule
519-
self._class_modules['Path'].append('pathlib')
520-
self._unfaked_module_classes[
521-
'pathlib'] = fake_pathlib.RealPathlibModule
516+
self._class_modules['Path'] = ['pathlib']
517+
self._unfaked_module_classes[
518+
'pathlib'] = fake_pathlib.RealPathlibModule
522519
if pathlib2:
523520
self._fake_module_classes[
524521
'pathlib2'] = fake_pathlib.FakePathlibModule

Diff for: pyfakefs/fake_pathlib.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
import fnmatch
3333
import functools
3434
import os
35+
import pathlib
3536
import re
3637
import sys
3738
from urllib.parse import quote_from_bytes as urlquote_from_bytes
3839

3940
from pyfakefs import fake_scandir
40-
from pyfakefs.extra_packages import use_scandir, pathlib, pathlib2
41+
from pyfakefs.extra_packages import use_scandir
4142
from pyfakefs.fake_filesystem import FakeFileOpen, FakeFilesystem
4243

4344

@@ -493,7 +494,7 @@ def resolve(self, strict=None):
493494
Raises:
494495
OSError: if the path doesn't exist (strict=True or Python < 3.6)
495496
"""
496-
if sys.version_info >= (3, 6) or pathlib2:
497+
if sys.version_info >= (3, 6):
497498
if strict is None:
498499
strict = False
499500
else:

Diff for: pyfakefs/pytest_tests/example.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@
1212

1313
# Used as SUT for pytest_fixture_test.py
1414

15-
try:
16-
from pathlib2 import Path
15+
from pathlib import Path
1716

18-
EXAMPLE_FILE = Path('/test') / 'file'
19-
except ImportError:
20-
try:
21-
from pathlib import Path
22-
23-
EXAMPLE_FILE = Path('/test') / 'file'
24-
except ImportError:
25-
EXAMPLE_FILE = None
17+
EXAMPLE_FILE = Path('/test') / 'file'

Diff for: pyfakefs/tests/all_tests_without_extra_packages.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,13 @@
1111
# limitations under the License.
1212

1313
"""A test suite that runs all tests for pyfakefs at once.
14-
Excludes tests using external pathlib2 and scandir packages."""
14+
Excludes tests using external scandir package."""
1515

1616
import sys
1717
import unittest
1818

1919
from pyfakefs import extra_packages
2020

21-
if extra_packages.pathlib2:
22-
extra_packages.pathlib2 = None
23-
try:
24-
import pathlib
25-
except ImportError:
26-
pathlib = None
27-
extra_packages.pathlib = pathlib
28-
2921
if extra_packages.use_scandir_package:
3022
extra_packages.use_scandir_package = False
3123
try:

Diff for: pyfakefs/tests/dynamic_patch_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"""
1414
Tests for patching modules loaded after `setUpPyfakefs()`.
1515
"""
16+
import pathlib
1617
import unittest
1718

1819
from pyfakefs import fake_filesystem_unittest
19-
from pyfakefs.extra_packages import pathlib
2020

2121

2222
class TestPyfakefsUnittestBase(fake_filesystem_unittest.TestCase):

Diff for: pyfakefs/tests/fake_filesystem_unittest_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io
2222
import multiprocessing
2323
import os
24+
import pathlib
2425
import runpy
2526
import shutil
2627
import sys
@@ -34,7 +35,6 @@
3435
import pyfakefs.tests.import_as_example
3536
import pyfakefs.tests.logsio
3637
from pyfakefs import fake_filesystem_unittest, fake_filesystem
37-
from pyfakefs.extra_packages import pathlib
3838
from pyfakefs.fake_filesystem import OSType
3939
from pyfakefs.fake_filesystem_unittest import (
4040
Patcher, Pause, patchfs, PatchMode

Diff for: pyfakefs/tests/fake_pathlib_test.py

+7-53
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
import errno
2424
import os
25+
import pathlib
2526
import stat
2627
import sys
2728
import unittest
2829

29-
from pyfakefs.extra_packages import pathlib, pathlib2
3030
from pyfakefs.fake_filesystem import is_root
3131

3232
from pyfakefs import fake_pathlib, fake_filesystem
@@ -35,20 +35,10 @@
3535
is_windows = sys.platform == 'win32'
3636

3737

38-
def skip_if_pathlib_36_not_available():
39-
if sys.version_info < (3, 6) and not pathlib2:
40-
raise unittest.SkipTest('Changed behavior in Python 3.6')
41-
42-
43-
def skip_if_pathlib_36_is_available():
44-
if sys.version_info >= (3, 6) or pathlib2:
45-
raise unittest.SkipTest('Changed behavior in Python 3.6')
46-
47-
4838
class RealPathlibTestCase(RealFsTestCase):
4939
def __init__(self, methodName='runTest'):
5040
super(RealPathlibTestCase, self).__init__(methodName)
51-
self.pathlib = pathlib or pathlib2
41+
self.pathlib = pathlib
5242
self.path = None
5343

5444
def setUp(self):
@@ -238,8 +228,8 @@ def test_relative_to(self):
238228
with self.assertRaises(ValueError):
239229
self.path('passwd').relative_to('/usr')
240230

241-
@unittest.skipIf(sys.version_info < (3, 9) or pathlib2,
242-
'readlink new in Python 3.9')
231+
@unittest.skipIf(sys.version_info < (3, 9),
232+
'is_relative_to new in Python 3.9')
243233
def test_is_relative_to(self):
244234
path = self.path('/etc/passwd')
245235
self.assertTrue(path.is_relative_to('/etc'))
@@ -418,11 +408,6 @@ def test_resolve(self):
418408
self.os.path.realpath(
419409
self.make_path('antoine', 'setup.py'))))
420410

421-
def test_resolve_nonexisting_file(self):
422-
skip_if_pathlib_36_is_available()
423-
path = self.path('/foo/bar')
424-
self.assert_raises_os_error(errno.ENOENT, path.resolve)
425-
426411
def test_stat_file_in_unreadable_dir(self):
427412
self.check_posix_only()
428413
dir_path = self.make_path('some_dir')
@@ -448,25 +433,7 @@ def test_iterdir_in_unreadable_dir(self):
448433
path = str(list(iter)[0])
449434
self.assertTrue(path.endswith('some_file'))
450435

451-
@unittest.skipIf(not is_windows, 'Windows specific behavior')
452-
def test_resolve_file_as_parent_windows(self):
453-
skip_if_pathlib_36_is_available()
454-
self.check_windows_only()
455-
self.create_file(self.make_path('a_file'))
456-
path = self.path(self.make_path('a_file', 'this can not exist'))
457-
self.assert_raises_os_error(errno.ENOENT, path.resolve)
458-
459-
@unittest.skipIf(is_windows, 'POSIX specific behavior')
460-
def test_resolve_file_as_parent_posix(self):
461-
skip_if_pathlib_36_is_available()
462-
self.check_posix_only()
463-
self.create_file(self.make_path('a_file'))
464-
path = self.path(
465-
self.make_path('', 'a_file', 'this can not exist'))
466-
self.assert_raises_os_error(errno.ENOTDIR, path.resolve)
467-
468-
def test_resolve_nonexisting_file_after_36(self):
469-
skip_if_pathlib_36_not_available()
436+
def test_resolve_nonexisting_file(self):
470437
path = self.path(
471438
self.make_path('/path', 'to', 'file', 'this can not exist'))
472439
self.assertEqual(path, path.resolve())
@@ -644,7 +611,7 @@ def test_symlink_to(self):
644611
self.assertTrue(self.os.path.exists(link_name))
645612
self.assertTrue(path.is_symlink())
646613

647-
@unittest.skipIf(sys.version_info < (3, 8) or pathlib2,
614+
@unittest.skipIf(sys.version_info < (3, 8),
648615
'link_to new in Python 3.8')
649616
def test_link_to(self):
650617
self.skip_if_symlink_not_supported()
@@ -658,20 +625,7 @@ def test_link_to(self):
658625
self.assertFalse(path.is_symlink())
659626
self.assertEqual(2, self.os.stat(file_name).st_nlink)
660627

661-
@unittest.skipIf(sys.version_info < (3, 8) or not pathlib2,
662-
'pathlib2 has no link_to')
663-
def test_pathlib2_does_not_have_link_to(self):
664-
self.skip_if_symlink_not_supported()
665-
file_name = self.make_path('foo', 'bar.txt')
666-
self.create_file(file_name)
667-
self.assertEqual(1, self.os.stat(file_name).st_nlink)
668-
link_name = self.make_path('link_to_bar')
669-
path = self.path(file_name)
670-
with self.assertRaises(AttributeError):
671-
path.link_to(link_name)
672-
self.assertFalse(self.os.path.exists(link_name))
673-
674-
@unittest.skipIf(sys.version_info < (3, 9) or pathlib2,
628+
@unittest.skipIf(sys.version_info < (3, 9),
675629
'readlink new in Python 3.9')
676630
def test_readlink(self):
677631
self.skip_if_symlink_not_supported()

0 commit comments

Comments
 (0)