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 PY_VERSION_HEX >= 0x03060000
19 # include <filesystem>
20 # define PYBIND11_HAS_FILESYSTEM 1
21 # elif __has_include(<experimental/filesystem>)
22 # include <experimental/filesystem>
23 # define PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM 1
28 #if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) \
29 && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
31 "Neither #include <filesystem> nor #include <experimental/filesystem is available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
37 #if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
42 static PyObject *unicode_from_fs_native(
const std::string &
w) {
43 # if !defined(PYPY_VERSION)
44 return PyUnicode_DecodeFSDefaultAndSize(
w.c_str(),
ssize_t(
w.size()));
47 return PyUnicode_DecodeFSDefaultAndSize(
const_cast<char *
>(
w.c_str()),
ssize_t(
w.size()));
51 static PyObject *unicode_from_fs_native(
const std::wstring &
w) {
52 return PyUnicode_FromWideChar(
w.c_str(),
ssize_t(
w.size()));
57 if (
auto py_str = unicode_from_fs_native(
path.native())) {
59 .attr(
"Path")(reinterpret_steal<object>(py_str))
69 PyObject *buf = PyOS_FSPath(
handle.
ptr());
74 PyObject *native =
nullptr;
75 if constexpr (std::is_same_v<typename T::value_type, char>) {
76 if (PyUnicode_FSConverter(buf, &native) != 0) {
77 if (
auto *
c_str = PyBytes_AsString(native)) {
83 }
else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
84 if (PyUnicode_FSDecoder(buf, &native) != 0) {
85 if (
auto *
c_str = PyUnicode_AsWideCharString(native,
nullptr)) {
94 if (PyErr_Occurred()) {
104 #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
106 #if defined(PYBIND11_HAS_FILESYSTEM)
109 #elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
112 :
public path_caster<std::experimental::filesystem::path> {};