Skip to content

Commit

Permalink
Adapt to changes in Python 3.12 beta1 (#835)
Browse files Browse the repository at this point in the history
- distutils has been removed
- variable renamed in pathlib
- add workaround for patching open, comment out not working tests
- hopefully fixes #830 and #831
  • Loading branch information
mrbean-bremen authored May 30, 2023
1 parent 403e21d commit 510e2bf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/testsuite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
with:
python-version: "3.10"
- name: install pytype
run: pip install pytype pytest scandir pathlib2 pandas xlrd django
run: pip install setuptools pytype pytest scandir pathlib2 pandas xlrd django
- name: Run pytype
run: |
pytype pyfakefs --keep-going --exclude pyfakefs/tests/* --exclude pyfakefs/pytest_tests/*
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Install dependencies
run: |
pip install wheel
pip install setuptools wheel
pip install -r requirements.txt
- name: Run unit tests without extra packages as non-root user
run: |
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The released versions correspond to PyPI releases.
(see [#814](../../issues/814)).
* Exclude pytest `pathlib` modules from patching to avoid mixup of patched/unpatched
code (see [#814](../../issues/814)).
* Adapt to changes in Python 3.12 beta1 (only working partially,
see [#830](../../issues/830) and [#831](../../issues/831))

### Infrastructure
* Added pytype check for non-test modules in CI (see [#599](../../issues/599)).
Expand Down
8 changes: 7 additions & 1 deletion pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
to `:py:class`pyfakefs.fake_filesystem_unittest.TestCase`.
"""
import _io # type:ignore[import]
import builtins
import doctest
import functools
import genericpath
Expand Down Expand Up @@ -455,7 +456,7 @@ class Patcher:
os.path,
}
if sys.platform == "win32":
import nt # type:ignore [import]
import nt # type:ignore[import]
import ntpath

SKIPMODULES.add(nt)
Expand Down Expand Up @@ -559,6 +560,7 @@ def __init__(
# save the original open function for use in pytest plugin
self.original_open = open
self.patch_open_code = patch_open_code
self.fake_open: fake_open.FakeFileOpen

if additional_skip_names is not None:
skip_names = [
Expand Down Expand Up @@ -844,6 +846,7 @@ def _refresh(self) -> None:

self.fs = fake_filesystem.FakeFilesystem(patcher=self, create_temp_dir=True)
self.fs.patch_open_code = self.patch_open_code
self.fake_open = fake_open.FakeFileOpen(self.fs)
for name in self._fake_module_classes:
self.fake_modules[name] = self._fake_module_classes[name](self.fs)
if hasattr(self.fake_modules[name], "skip_names"):
Expand Down Expand Up @@ -922,6 +925,9 @@ def patch_modules(self) -> None:
for module, attr in modules:
if attr in self.unfaked_modules:
self._stubs.smart_set(module, name, self.unfaked_modules[attr])
if sys.version_info >= (3, 12):
# workaround for patching open - does not work with skip modules
self._stubs.smart_set(builtins, "open", self.fake_open)

def patch_defaults(self) -> None:
for fct, idx, ft in self.FS_DEFARGS:
Expand Down
6 changes: 3 additions & 3 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,12 @@ def is_absolute(self):
return os.path.isabs(self._path())

def is_reserved(self):
if not self.filesystem.is_windows_fs or not self._parts:
if not self.filesystem.is_windows_fs or not self._tail:
return False
if self._parts[0].startswith("\\\\"):
if self._tail[0].startswith("\\\\"):
# UNC paths are never reserved.
return False
name = self._parts[-1].partition(".")[0].partition(":")[0].rstrip(" ")
name = self._tail[-1].partition(".")[0].partition(":")[0].rstrip(" ")
return name.upper() in pathlib._WIN_RESERVED_NAMES


Expand Down
61 changes: 38 additions & 23 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import tempfile
import unittest
import warnings
from distutils.dir_util import copy_tree, remove_tree
from pathlib import Path
from unittest import TestCase, mock

Expand All @@ -44,6 +43,10 @@
)
from pyfakefs.tests.fixtures import module_with_attributes

if sys.version_info < (3, 12):
# distutils removed in Python 3.12
from distutils.dir_util import copy_tree, remove_tree


class TestPatcher(TestCase):
def test_context_manager(self):
Expand Down Expand Up @@ -152,9 +155,9 @@ def test_shutil(self):
self.assertFalse(self.fs.exists("/test/dir1"))

def test_fakepathlib(self):
with pathlib.Path("/fake_file.txt") as p:
with p.open("w") as f:
f.write("text")
p = pathlib.Path("/fake_file.txt")
with p.open("w") as f:
f.write("text")
is_windows = sys.platform.startswith("win")
if is_windows:
self.assertTrue(self.fs.exists(r"\fake_file.txt"))
Expand Down Expand Up @@ -225,12 +228,14 @@ def test_import_function_from_os_as_other_name(self):
stat_result = pyfakefs.tests.import_as_example.file_stat2(file_path)
self.assertEqual(3, stat_result.st_size)

@unittest.skipIf(sys.version_info >= (3, 12), "Currently not working in 3.12")
def test_import_open_as_other_name(self):
file_path = "/foo/bar"
self.fs.create_file(file_path, contents=b"abc")
contents = pyfakefs.tests.import_as_example.file_contents1(file_path)
self.assertEqual("abc", contents)

@unittest.skipIf(sys.version_info >= (3, 12), "Currently not working in 3.12")
def test_import_io_open_as_other_name(self):
file_path = "/foo/bar"
self.fs.create_file(file_path, contents=b"abc")
Expand Down Expand Up @@ -393,6 +398,10 @@ def test_fake_path_does_not_exist7(self):
self.fs.create_file("foo")
self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists7("foo"))

@unittest.skipIf(
sys.version_info >= (3, 12),
"Skip modules currently not working for open in 3.12",
)
def test_open_succeeds(self):
pyfakefs.tests.import_as_example.open_this_file()

Expand Down Expand Up @@ -438,6 +447,10 @@ def test_fake_path_does_not_exist7(self):
self.fs.create_file("foo")
self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists7("foo"))

@unittest.skipIf(
sys.version_info >= (3, 12),
"Skip modules currently not working for open in 3.12",
)
def test_open_succeeds(self):
pyfakefs.tests.import_as_example.open_this_file()

Expand Down Expand Up @@ -704,31 +717,33 @@ def test_b(self):
shutil.make_archive("archive", "zip", root_dir="foo")


class TestDistutilsCopyTree(fake_filesystem_unittest.TestCase):
"""Regression test for #501."""
if sys.version_info < (3, 12):

def setUp(self):
self.setUpPyfakefs()
self.fs.create_dir("./test/subdir/")
self.fs.create_dir("./test/subdir2/")
self.fs.create_file("./test2/subdir/1.txt")
class TestDistutilsCopyTree(fake_filesystem_unittest.TestCase):
"""Regression test for #501."""

def setUp(self):
self.setUpPyfakefs()
self.fs.create_dir("./test/subdir/")
self.fs.create_dir("./test/subdir2/")
self.fs.create_file("./test2/subdir/1.txt")

def test_file_copied(self):
copy_tree("./test2/", "./test/")
remove_tree("./test2/")
def test_file_copied(self):
copy_tree("./test2/", "./test/")
remove_tree("./test2/")

self.assertTrue(os.path.isfile("./test/subdir/1.txt"))
self.assertFalse(os.path.isdir("./test2/"))
self.assertTrue(os.path.isfile("./test/subdir/1.txt"))
self.assertFalse(os.path.isdir("./test2/"))

def test_file_copied_again(self):
# used to fail because 'test2' could not be found
self.assertTrue(os.path.isfile("./test2/subdir/1.txt"))
def test_file_copied_again(self):
# used to fail because 'test2' could not be found
self.assertTrue(os.path.isfile("./test2/subdir/1.txt"))

copy_tree("./test2/", "./test/")
remove_tree("./test2/")
copy_tree("./test2/", "./test/")
remove_tree("./test2/")

self.assertTrue(os.path.isfile("./test/subdir/1.txt"))
self.assertFalse(os.path.isdir("./test2/"))
self.assertTrue(os.path.isfile("./test/subdir/1.txt"))
self.assertFalse(os.path.isdir("./test2/"))


class PathlibTest(TestCase):
Expand Down

0 comments on commit 510e2bf

Please sign in to comment.