16 #include <pybind11/cast.h>
17 #include <pybind11/detail/common.h>
18 #include <pybind11/detail/descr.h>
19 #include <pybind11/pybind11.h>
20 #include <pybind11/pytypes.h>
25 #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
29 namespace fs = std::__fs::filesystem;
31 #include <experimental/filesystem>
32 namespace fs = std::experimental::filesystem;
41 static PyObject *unicode_from_fs_native(
const std::string &w) {
42 #if !defined(PYPY_VERSION)
43 return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size()));
46 return PyUnicode_DecodeFSDefaultAndSize(
const_cast<char *
>(w.c_str()),
51 static PyObject *unicode_from_fs_native(
const std::wstring &w) {
52 return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size()));
56 static handle
cast(
const T &
path, return_value_policy, handle) {
57 if (
auto py_str = unicode_from_fs_native(
path.native())) {
58 return module_::import(
"pathlib")
59 .attr(
"Path")(reinterpret_steal<object>(py_str))
65 bool load(handle handle,
bool) {
69 PyObject *buf = PyOS_FSPath(handle.ptr());
74 PyObject *native =
nullptr;
75 if (std::is_same<typename T::value_type, char>::value) {
76 if (PyUnicode_FSConverter(buf, &native) != 0) {
77 if (
auto *c_str = PyBytes_AsString(native)) {
83 }
else if (std::is_same<typename T::value_type, wchar_t>::value) {
84 if (PyUnicode_FSDecoder(buf, &native) != 0) {
85 if (
auto *c_str = PyUnicode_AsWideCharString(native,
nullptr)) {
95 if (PyErr_Occurred()) {
103 io_name(
"Union[os.PathLike, str, bytes]",
static const std::string path
PYBIND11_TYPE_CASTER(T, io_name("Union[os.PathLike, str, bytes]", "pathlib.Path"))
bool load(handle handle, bool)
static handle cast(const T &path, return_value_policy, handle)