Skip to content

Commit 68c0a42

Browse files
committed
Fix PurePosixPath.joinpath under Windows
- fixes #1070
1 parent ed752e0 commit 68c0a42

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ The released versions correspond to PyPI releases.
2929
(see [#1053](../../issues/1053))
3030
* `PurePosixPath` reported Windows reserved names as reserved in Python >= 3.12
3131
(see [#1067](../../issues/1067))
32+
* `PurePosixPath.joinpath()` incorrectly handled paths with drives under Windows in Python >= 3.12
33+
(see [#1070](../../issues/1070))
3234

3335
## [Version 5.6.0](https://pypi.python.org/pypi/pyfakefs/5.6.0) (2024-07-12)
3436
Adds preliminary Python 3.13 support.

pyfakefs/fake_filesystem.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,18 +1306,17 @@ def joinpaths(self, *paths: AnyStr) -> AnyStr:
13061306
return paths[0]
13071307
if self.is_windows_fs:
13081308
return self._join_paths_with_drive_support(*file_paths)
1309-
joined_path_segments = []
1309+
path = file_paths[0]
13101310
sep = self.get_path_separator(file_paths[0])
1311-
for path_segment in file_paths:
1312-
if self._starts_with_root_path(path_segment):
1311+
for path_segment in file_paths[1:]:
1312+
if path_segment.startswith(sep) or not path:
13131313
# An absolute path
1314-
joined_path_segments = [path_segment]
1314+
path = path_segment
1315+
elif path.endswith(sep):
1316+
path += path_segment
13151317
else:
1316-
if joined_path_segments and not joined_path_segments[-1].endswith(sep):
1317-
joined_path_segments.append(sep)
1318-
if path_segment:
1319-
joined_path_segments.append(path_segment)
1320-
return matching_string(file_paths[0], "").join(joined_path_segments)
1318+
path += sep + path_segment
1319+
return path
13211320

13221321
@overload
13231322
def _path_components(self, path: str) -> List[str]: ...

pyfakefs/tests/fake_pathlib_test.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,10 @@ def test_joinpath(self):
328328
self.assertEqual(
329329
self.path("/foo").joinpath("bar", "baz"), self.path("/foo/bar/baz")
330330
)
331-
if os.name != "nt":
332-
# under Windows, this does not work correctly at the moment
333-
# we get "C:/Program Files" instead
334-
self.assertEqual(
335-
self.path("c:").joinpath("/Program Files"),
336-
self.path("/Program Files"),
337-
)
331+
self.assertEqual(
332+
self.path("c:").joinpath("/Program Files"),
333+
self.path("/Program Files"),
334+
)
338335

339336
def test_match(self):
340337
self.assertTrue(self.path("a/b.py").match("*.py"))

0 commit comments

Comments
 (0)