Skip to content

Commit affc4f3

Browse files
committed
Handle pathlib.Path.getcwd for Python 3.10
- handle dummy encoding "locale" introduced in Python 3.10
1 parent 02d5f69 commit affc4f3

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

pyfakefs/fake_filesystem.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
from pyfakefs.helpers import (
115115
FakeStatResult, BinaryBufferIO, TextBufferIO,
116116
is_int_type, is_byte_string, is_unicode_string,
117-
make_string_path, IS_WIN, to_string, matching_string
117+
make_string_path, IS_WIN, to_string, matching_string, real_encoding
118118
)
119119
from pyfakefs import __version__ # noqa: F401 for upwards compatibility
120120

@@ -293,7 +293,7 @@ def __init__(self, name, st_mode=S_IFREG | PERM_DEF_FILE,
293293
if st_mode >> 12 == 0:
294294
st_mode |= S_IFREG
295295
self.stat_result.st_mode = st_mode
296-
self.encoding = encoding
296+
self.encoding = real_encoding(encoding)
297297
self.errors = errors or 'strict'
298298
self._byte_contents = self._encode_contents(contents)
299299
self.stat_result.st_size = (
@@ -430,7 +430,7 @@ def set_contents(self, contents, encoding=None):
430430
OSError: if `st_size` is not a non-negative integer,
431431
or if it exceeds the available file system space.
432432
"""
433-
self.encoding = encoding
433+
self.encoding = real_encoding(encoding)
434434
changed = self._set_initial_contents(contents)
435435
if self._side_effect is not None:
436436
self._side_effect(self)

pyfakefs/fake_pathlib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ def lchmod(self, pathobj, mode):
134134
lambda fs, file_path, link_target:
135135
FakeFilesystem.link(fs, file_path, link_target))
136136

137+
# this will use the fake filesystem because os is patched
138+
getcwd = lambda p: os.getcwd()
139+
137140
readlink = _wrap_strfunc(FakeFilesystem.readlink)
138141

139142
utime = _wrap_strfunc(FakeFilesystem.utime)

pyfakefs/helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ def to_string(path):
5757
return path
5858

5959

60+
def real_encoding(encoding):
61+
"""Since Python 3.10, the new function ``io.text_encoding`` returns
62+
"locale" as the encoding if None is defined. This will be handled
63+
as no encoding in pyfakefs."""
64+
if sys.version_info >= (3, 10):
65+
return encoding if encoding != "locale" else None
66+
return encoding
67+
68+
6069
def matching_string(matched, string):
6170
"""Return the string as byte or unicode depending
6271
on the type of matched, assuming string is an ASCII string.

0 commit comments

Comments
 (0)