From 0f4122a2a410d36be5fed3d7721723f9f5b1e989 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 4 Dec 2023 18:08:12 +0000 Subject: [PATCH] Ignore empty segments. --- Lib/pathlib.py | 7 +++---- Lib/test/test_pathlib.py | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 255b4fdf740871..f6d30c027cfeaa 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -261,8 +261,6 @@ def with_segments(self, *pathsegments): @classmethod def _parse_path(cls, path): - if not path: - return '', '', [] sep = cls.pathmod.sep altsep = cls.pathmod.altsep if altsep: @@ -605,12 +603,13 @@ def __init__(self, *args): "argument should be a str or an os.PathLike " "object where __fspath__ returns a str, " f"not {type(path).__name__!r}") - paths.append(path) + if path: + paths.append(path) self._raw_paths = paths if len(paths) == 1: self._raw_path_cached = paths[0] elif len(paths) == 0: - self._raw_path_cached = '' + self._raw_path_cached = '.' self._resolving = False def __reduce__(self): diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index e1121a9d76c040..b1cac781b6802e 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -708,6 +708,13 @@ def test_fspath_common(self): p = P('a/b') self._check_str(p.__fspath__(), ('a/b',)) self._check_str(os.fspath(p), ('a/b',)) + self.assertEqual('.', P().__fspath__()) + self.assertEqual('.', P('').__fspath__()) + self.assertEqual('.', P('.').__fspath__()) + self.assertEqual('.', P('', '').__fspath__()) + self.assertEqual('.', P('.', '').__fspath__()) + self.assertEqual('.', P('', '.').__fspath__()) + self.assertEqual(f'.{self.sep}.', P('.', '.').__fspath__()) def test_bytes(self): P = self.cls