Skip to content

gh-154690: pathlib: Resolve paths in ensure_distinct_paths() before comparing#154689

Open
dura0ok wants to merge 3 commits into
python:mainfrom
dura0ok:fix-pathlib-copy-into-self
Open

gh-154690: pathlib: Resolve paths in ensure_distinct_paths() before comparing#154689
dura0ok wants to merge 3 commits into
python:mainfrom
dura0ok:fix-pathlib-copy-into-self

Conversation

@dura0ok

@dura0ok dura0ok commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Path.copy() only blocked self-copies via a lexical check, so relative vs absolute paths and symlink aliases could still copy a directory into itself. Resolve when possible (like shutil._destinsrc)

Path.copy() only blocked self-copies via a lexical check, so relative vs
absolute paths and symlink aliases could still copy a directory into
itself. Resolve when possible (like shutil._destinsrc) and add tests.
@dura0ok
dura0ok requested a review from barneygale as a code owner July 25, 2026 12:03
@dura0ok dura0ok changed the title pathlib: Resolve paths in ensure_distinct_paths() before comparing gh-154690: pathlib: Resolve paths in ensure_distinct_paths() before comparing Jul 25, 2026

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not a full review)

Comment thread Lib/pathlib/_os.py Outdated
target = target.resolve(strict=False)
except (AttributeError, TypeError):
pass
except (OSError, ValueError, RuntimeError, NotImplementedError):

@sobolevn sobolevn Jul 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason to include two except/pass cases? Also, are you sure that these many exceptions can realistically happen? Maybe provide a comment - how?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, two except/pass blocks did the same thing. Merged into one and kept only AttributeError when resolve is missing and OSError when realpath fails. The rest were not realistic.

@dura0ok
dura0ok force-pushed the fix-pathlib-copy-into-self branch from 192abed to d54db22 Compare July 25, 2026 13:31
…andling

Only catch AttributeError (no resolve) and OSError (realpath failed),
and test the lexical fallback for both cases.
@dura0ok
dura0ok force-pushed the fix-pathlib-copy-into-self branch from d54db22 to 33257e7 Compare July 25, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants