7 #include "../pybind11.h"
8 #include "../detail/common.h"
9 #include "../detail/descr.h"
11 #include "../pytypes.h"
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.)"
36 #if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_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()),
ssize_t(
w.size()));
50 static PyObject *unicode_from_fs_native(
const std::wstring &
w) {
51 return PyUnicode_FromWideChar(
w.c_str(),
ssize_t(
w.size()));
56 if (
auto py_str = unicode_from_fs_native(
path.native())) {
58 .attr(
"Path")(reinterpret_steal<object>(py_str))
68 PyObject *buf = PyOS_FSPath(
handle.
ptr());
73 PyObject *native =
nullptr;
74 if constexpr (std::is_same_v<typename T::value_type, char>) {
75 if (PyUnicode_FSConverter(buf, &native) != 0) {
76 if (
auto *
c_str = PyBytes_AsString(native)) {
82 }
else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
83 if (PyUnicode_FSDecoder(buf, &native) != 0) {
84 if (
auto *
c_str = PyUnicode_AsWideCharString(native,
nullptr)) {
93 if (PyErr_Occurred()) {
103 #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
105 #if defined(PYBIND11_HAS_FILESYSTEM)
108 #elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
111 :
public path_caster<std::experimental::filesystem::path> {};