Skip to content

Commit edd6a6c

Browse files
committed
Add support for new strict value "ALLOW_MISSING" in os.path.realpath
- has been introduced in latest patch version of Python >= 3.10
1 parent 7ebfca1 commit edd6a6c

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ The released versions correspond to PyPI releases.
3030
* added missing `mode` property to fake file wrapper (see [#1162](../../issues/1162))
3131
* fixed instantiation of a standalone `FakePathlibModule` for Python >= 3.11
3232
(see [#1169](../../issues/1169))
33+
* added support for new value "ALLOW_MISSING" of `strict` argument in `os.path.realpath`
34+
(introduced in latest patch version of Python >= 3.10, see [#1180](../../issues/1180))
3335

3436
### Infrastructure
3537
* adapt test for increased default buffer size in Python 3.14a6

pyfakefs/fake_path.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,21 @@ def realpath(self, filename: AnyStr, strict: Optional[bool] = None) -> AnyStr:
361361
"""
362362
if strict is not None and sys.version_info < (3, 10):
363363
raise TypeError("realpath() got an unexpected keyword argument 'strict'")
364-
if strict:
365-
# raises in strict mode if the file does not exist
364+
has_allow_missing = hasattr(os.path, "ALLOW_MISSING")
365+
if has_allow_missing and strict == os.path.ALLOW_MISSING: # type: ignore[attr-defined]
366+
# ignores non-existing file, but not other errors
367+
ignored_error: Any = FileNotFoundError
368+
elif strict:
369+
# raises on any errors
370+
ignored_error = ()
371+
else:
372+
# ignores any OSError while resolving the filename
373+
ignored_error = OSError
374+
try:
366375
self.filesystem.resolve(filename)
376+
except ignored_error:
377+
pass
378+
367379
if self.filesystem.is_windows_fs:
368380
return self.abspath(filename)
369381
filename = make_string_path(filename)

pyfakefs/tests/fake_filesystem_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,23 @@ def test_realpath_strict(self):
10361036
f"{root_dir}foo!bar", self.os.path.realpath("bar", strict=True)
10371037
)
10381038

1039+
@unittest.skipIf(
1040+
not hasattr(os.path, "ALLOW_MISSING"),
1041+
"ALLOW_MISSING has been added in different patch versions",
1042+
)
1043+
def test_realpath_allow_missing(self):
1044+
f = self.filesystem.create_file("!foo!bar")
1045+
root_dir = self.filesystem.root_dir_name
1046+
self.filesystem.cwd = f"{root_dir}foo"
1047+
self.assertEqual(
1048+
"!foo!baz",
1049+
self.os.path.realpath("baz", strict=os.path.ALLOW_MISSING), # type: ignore[attr-defined]
1050+
)
1051+
if not is_root():
1052+
self.os.chmod(f.path, 0)
1053+
with self.raises_os_error(errno.EACCES):
1054+
self.os.path.realpath(f.path, strict=os.path.ALLOW_MISSING) # type: ignore[attr-defined]
1055+
10391056
def test_samefile(self):
10401057
file_path1 = "!foo!bar!baz"
10411058
file_path2 = "!foo!bar!boo"

0 commit comments

Comments
 (0)