16 # if defined(PYBIND11_CPP17)
17 # if __has_include(<filesystem>)
18 # include <filesystem>
19 # define PYBIND11_HAS_FILESYSTEM 1
20 # elif __has_include(<experimental/filesystem>)
21 # include <experimental/filesystem>
22 # define PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM 1
27 #if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) \
28 && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
30 "Neither #include <filesystem> nor #include <experimental/filesystem is available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
37 # define PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(...) (__VA_ARGS__)
39 # define PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(...) \
40 (reinterpret_cast<void *>(__VA_ARGS__))
43 #if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
48 static PyObject *unicode_from_fs_native(
const std::string &
w) {
49 # if !defined(PYPY_VERSION)
50 return PyUnicode_DecodeFSDefaultAndSize(
w.c_str(),
ssize_t(
w.size()));
53 return PyUnicode_DecodeFSDefaultAndSize(
const_cast<char *
>(
w.c_str()),
ssize_t(
w.size()));
57 static PyObject *unicode_from_fs_native(
const std::wstring &
w) {
58 return PyUnicode_FromWideChar(
w.c_str(),
ssize_t(
w.size()));
63 if (
auto py_str = unicode_from_fs_native(
path.native())) {
65 .attr(
"Path")(reinterpret_steal<object>(py_str))
75 PyObject *buf = PyOS_FSPath(
handle.
ptr());
80 PyObject *native =
nullptr;
81 if constexpr (std::is_same_v<typename T::value_type, char>) {
84 if (
auto *
c_str = PyBytes_AsString(native)) {
90 }
else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
93 if (
auto *
c_str = PyUnicode_AsWideCharString(native,
nullptr)) {
102 if (PyErr_Occurred()) {
112 #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
114 #if defined(PYBIND11_HAS_FILESYSTEM)
117 #elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
120 :
public path_caster<std::experimental::filesystem::path> {};