diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index cea1a9fe57eedf..9594c43b9bf4f7 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -209,7 +209,12 @@ def __hash__(self): try: return self._hash except AttributeError: - self._hash = hash(self._str_normcase) + if self.parser is posixpath: + self._hash = hash((self.root, tuple(self._tail))) + else: + self._hash = hash((self.drive.lower(), + self.root.lower(), + tuple([part.lower() for part in self._tail]))) return self._hash def __eq__(self, other): diff --git a/Misc/NEWS.d/next/Library/2025-09-08-19-04-23.gh-issue-138407.BqNAfi.rst b/Misc/NEWS.d/next/Library/2025-09-08-19-04-23.gh-issue-138407.BqNAfi.rst new file mode 100644 index 00000000000000..c1606819675b69 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-08-19-04-23.gh-issue-138407.BqNAfi.rst @@ -0,0 +1 @@ +Avoid useless join operation when calculate the hash vaule for pathlib.Path.