Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/pybind11/stl/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ struct path_caster {
return nullptr;
}

bool load(handle handle, bool) {
bool load(handle handle, bool convert) {
if (!convert
&& PyObject_IsInstance(handle.ptr(), module_::import("pathlib").attr("Path").ptr())
!= 1) {
return false;
}

// PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of
// calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy
// issue #3168) so we do it ourselves instead.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ TEST_SUBMODULE(stl, m) {
// test_fs_path
m.attr("has_filesystem") = true;
m.def("parent_path", [](const std::filesystem::path &path) { return path.parent_path(); });
m.def(
"parent_path_noconvert",
[](const std::filesystem::path &path) { return path.parent_path(); },
py::arg("arg0").noconvert());
m.def("parent_paths", [](const std::vector<std::filesystem::path> &paths) {
std::vector<std::filesystem::path> result;
result.reserve(paths.size());
Expand Down
15 changes: 15 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ def __fspath__(self):
assert m.parent_path(b"foo/bar") == Path("foo")
assert m.parent_path(PseudoStrPath()) == Path("foo")
assert m.parent_path(PseudoBytesPath()) == Path("foo")
# Single argument noconvert
assert m.parent_path_noconvert(Path("foo/bar")) == Path("foo")
with pytest.raises(TypeError):
m.parent_path_noconvert("foo/bar")
with pytest.raises(TypeError):
m.parent_path_noconvert(b"foo/bar")
with pytest.raises(TypeError):
m.parent_path_noconvert(PseudoStrPath())
with pytest.raises(TypeError):
m.parent_path_noconvert(PseudoBytesPath())
# std::vector
assert m.parent_paths(["foo/bar", "foo/baz"]) == [Path("foo"), Path("foo")]
# py::typing::List
Expand Down Expand Up @@ -311,6 +321,11 @@ def test_path_typing(doc):
doc(m.parent_path)
== "parent_path(arg0: os.PathLike | str | bytes) -> pathlib.Path"
)
# Single argument noconvert
assert (
doc(m.parent_path_noconvert)
== "parent_path_noconvert(arg0: pathlib.Path) -> pathlib.Path"
)
# std::vector
assert (
doc(m.parent_paths)
Expand Down
Loading