From 9d41816507c4663f2cf06263474c8915c2176f71 Mon Sep 17 00:00:00 2001 From: hakarrr Date: Sat, 24 Oct 2026 19:28:57 +0530 Subject: [PATCH 1/2] Normalize backslash separators before resolving dot components Replace backslashes with forward slashes in normalized file paths. --- pathvalidate/_filepath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pathvalidate/_filepath.py b/pathvalidate/_filepath.py index df49ed4..7a11292 100644 --- a/pathvalidate/_filepath.py +++ b/pathvalidate/_filepath.py @@ -94,7 +94,7 @@ def sanitize(self, value: PathType, replacement_text: str = "") -> PathType: drive, unicode_filepath = self.__split_drive(unicode_filepath) unicode_filepath = self._sanitize_regexp.sub(replacement_text, unicode_filepath) if self.__normalize and unicode_filepath: - unicode_filepath = os.path.normpath(unicode_filepath) + unicode_filepath = os.path.normpath(unicode_filepath.replace("\\", "/")) sanitized_path = unicode_filepath sanitized_entries: list[str] = [] From c9f5a3997637e57a2b119a7548029c96d38a122a Mon Sep 17 00:00:00 2001 From: hakarrr Date: Sat, 24 Oct 2026 19:29:26 +0530 Subject: [PATCH 2/2] Cover backslash normalization, roots and opt-out behavior Add tests for normalizing backslash separators in file paths across different platforms. --- test/test_filepath_normalize_backslashes.py | 47 +++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/test_filepath_normalize_backslashes.py diff --git a/test/test_filepath_normalize_backslashes.py b/test/test_filepath_normalize_backslashes.py new file mode 100644 index 0000000..7a0be1d --- /dev/null +++ b/test/test_filepath_normalize_backslashes.py @@ -0,0 +1,47 @@ +from pathlib import Path + +import pytest + +from pathvalidate import sanitize_filepath + + +@pytest.mark.parametrize( + "platform,separator", [("windows", "\\"), ("linux", "/"), ("universal", "/")] +) +@pytest.mark.parametrize("value", [r"a\b\..\c.txt", r"a/b\..\c.txt", r"a\.\c.txt"]) +def test_normalize_backslash_separators(platform, separator, value): + expected = separator.join(["a", "c.txt"]) + assert sanitize_filepath(value, platform=platform) == expected + assert sanitize_filepath(Path(value), platform=platform) == Path(expected) + + +@pytest.mark.parametrize( + "platform,separator", [("windows", "\\"), ("linux", "/"), ("universal", "/")] +) +def test_backslash_normalization_opt_out(platform, separator): + value = r"a\b\..\c.txt" + expected = separator.join(["a", "b", "..", "c.txt"]) + assert sanitize_filepath(value, platform=platform, normalize=False) == expected + + +@pytest.mark.parametrize( + "value,expected", + [ + (r"C:\a\b\..\c.txt", r"C:\a\c.txt"), + (r"C:a\b\..\c.txt", r"C:\a\c.txt"), + (r"\\server\share\a\b\..\c.txt", r"\\server\share\a\c.txt"), + (r"\a\b\..\c.txt", r"\a\c.txt"), + (r"..\a\.\..\c.txt", r"..\c.txt"), + ], +) +def test_normalize_backslash_windows_roots(value, expected): + assert sanitize_filepath(value, platform="windows") == expected + + +@pytest.mark.parametrize("platform", ["windows", "linux", "universal"]) +@pytest.mark.parametrize("value", [r"a\b\c.txt", "a/b/c.txt", "../a/b.txt"]) +def test_backslash_separator_control(platform, value): + separator = "\\" if platform == "windows" else "/" + assert sanitize_filepath(value, platform=platform) == value.replace("\\", "/").replace( + "/", separator + )