diff --git a/python/pybind11/filesystem/filesystem_py.cpp b/python/pybind11/filesystem/filesystem_py.cpp index 40c9dc2bc..e11de2bc5 100644 --- a/python/pybind11/filesystem/filesystem_py.cpp +++ b/python/pybind11/filesystem/filesystem_py.cpp @@ -58,6 +58,19 @@ void init_filesystem(py::module& fs) py::arg("file_offset"), // py::arg("buf_offset") = 0) // .def("close", &CuFileDriver::close, doc::CuFileDriver::doc_close, py::call_guard()) // + .def( + "__enter__", + [](const std::shared_ptr& fd) { // + return fd; // + }, // + py::call_guard()) + .def( + "__exit__", + [](const std::shared_ptr& fd, const py::object& type, const py::object& value, + const py::object& traceback) { // + fd->close(); // + }, // + py::call_guard()) .def("__repr__", [](const CuFileDriver& fd) { return fmt::format("", fd.path()); });