From 41838d5358c4a290501415a1a96f5eb5038cb708 Mon Sep 17 00:00:00 2001 From: simpleqt <2015900818@qq.com> Date: Wed, 4 Nov 2026 01:36:29 +0800 Subject: [PATCH] Make trio.Path.samefile work with another trio.Path `samefile` was a plain `_wrap_method` of `pathlib.Path.samefile`, which only converts `self`. The stdlib then asks the *other* path for its stat result: other_st = other_path.stat() Since `trio.Path.stat` is async, passing a `trio.Path` as `other_path` handed `os.path.samestat` an un-awaited coroutine: AttributeError: 'coroutine' object has no attribute 'st_ino' Compare through `os.path.samefile` on `os.fspath(...)` of both sides instead, which goes via `__fspath__` for any `os.PathLike` -- a `trio.Path`, a `pathlib.Path`, a `str`, or anything else. This is also what `pathlib.Path.samefile` itself moved to for comparisons internally. `samefile` is the only wrapped method with this problem: `rename`, `replace` and `hardlink_to` already reach `os.*` through `__fspath__` and accept a `trio.Path` argument. The rest of the `os.PathLike`-accepting methods were checked. Fixes #3573. Signed-off-by: simpleqt <2015900818@qq.com> --- newsfragments/3573.bugfix.rst | 1 + src/trio/_path.py | 11 ++++++++++- src/trio/_tests/test_path.py | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 newsfragments/3573.bugfix.rst diff --git a/newsfragments/3573.bugfix.rst b/newsfragments/3573.bugfix.rst new file mode 100644 index 0000000000..a858fe0d95 --- /dev/null +++ b/newsfragments/3573.bugfix.rst @@ -0,0 +1 @@ +:meth:`trio.Path.samefile` no longer raises :exc:`AttributeError` when the path it is compared against is itself a :class:`trio.Path`. diff --git a/src/trio/_path.py b/src/trio/_path.py index ea571c9c01..55b48d8c6b 100644 --- a/src/trio/_path.py +++ b/src/trio/_path.py @@ -216,6 +216,16 @@ async def open( # type: ignore[explicit-any] # Any usage matches builtins.open def open(self, *args: Any, **kwargs: Any) -> AsyncIOWrapper[IO[Any]]: # type: ignore[misc, explicit-any] # Overload return mismatch. return wrap_file(self._wrapped_cls(self).open(*args, **kwargs)) + @_wraps_async(pathlib.Path.samefile) + def samefile(self, other_path: str | os.PathLike[str]) -> bool: + # pathlib asks ``other_path`` for its stat result and hands that + # straight to ``samestat``. Our ``stat`` is async, so when the other + # path is itself a `trio.Path` that would hand over an un-awaited + # coroutine. Comparing via ``os.path`` uses ``__fspath__`` on both + # sides instead, so any `os.PathLike` -- including a `trio.Path` -- + # works. + return os.path.samefile(os.fspath(self), os.fspath(other_path)) + def __repr__(self) -> str: return f"trio.Path({str(self)!r})" @@ -255,7 +265,6 @@ def __repr__(self) -> str: expanduser = _wrap_method_path(pathlib.Path.expanduser) read_bytes = _wrap_method(pathlib.Path.read_bytes) read_text = _wrap_method(pathlib.Path.read_text) - samefile = _wrap_method(pathlib.Path.samefile) write_bytes = _wrap_method(pathlib.Path.write_bytes) write_text = _wrap_method(pathlib.Path.write_text) if sys.version_info < (3, 12): diff --git a/src/trio/_tests/test_path.py b/src/trio/_tests/test_path.py index 9fe3920df3..8fa5c2afe1 100644 --- a/src/trio/_tests/test_path.py +++ b/src/trio/_tests/test_path.py @@ -154,6 +154,42 @@ async def test_async_methods_rewrap(method_name: str) -> None: assert str(result) == str(async_result) +@pytest.mark.parametrize("other_is_trio_path", [False, True]) +async def test_samefile(other_is_trio_path: bool, tmp_path: pathlib.Path) -> None: + # `samefile` passes the *other* path to `samestat` without going through + # `__fspath__`, so a trio.Path argument used to hand over an un-awaited + # coroutine from our async `stat`. + this = tmp_path / "this" + this.write_text("contents") + hardlink = tmp_path / "hardlink" + os.link(this, hardlink) + different = tmp_path / "different" + different.write_text("contents") + + def other(path: pathlib.Path) -> pathlib.Path | trio.Path: + return trio.Path(path) if other_is_trio_path else path + + path = trio.Path(this) + + assert await path.samefile(other(this)) + assert await path.samefile(other(hardlink)) + assert not await path.samefile(other(different)) + # and the reverse direction, where `other_path` is the hardlink + assert await trio.Path(hardlink).samefile(other(this)) + + +@pytest.mark.parametrize("as_str", [False, True]) +async def test_samefile_mixed_pathlike(as_str: bool, tmp_path: pathlib.Path) -> None: + # str and non-Path os.PathLike objects keep working too + this = tmp_path / "this" + this.write_text("contents") + + path = trio.Path(this) + argument: str | os.PathLike[str] = str(this) if as_str else pathlib.PurePath(this) + + assert await path.samefile(argument) + + def test_forward_methods_rewrap(path: trio.Path, tmp_path: pathlib.Path) -> None: with_name = path.with_name("foo") with_suffix = path.with_suffix(".py")