7#include "../pybind11.h"
8#include "../detail/common.h"
9#include "../detail/descr.h"
11#include "../pytypes.h"
16# if defined(PYBIND11_CPP17) && __has_include(<filesystem>) && \
17 PY_VERSION_HEX >= 0x03060000
19# define PYBIND11_HAS_FILESYSTEM 1
23#if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
25 "#include <filesystem> is not available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
31#if defined(PYBIND11_HAS_FILESYSTEM)
36 static PyObject *unicode_from_fs_native(
const std::string &w) {
37# if !defined(PYPY_VERSION)
38 return PyUnicode_DecodeFSDefaultAndSize(w.c_str(),
ssize_t(w.size()));
41 return PyUnicode_DecodeFSDefaultAndSize(
const_cast<char *
>(w.c_str()),
ssize_t(w.size()));
45 static PyObject *unicode_from_fs_native(
const std::wstring &w) {
46 return PyUnicode_FromWideChar(w.c_str(),
ssize_t(w.size()));
51 if (
auto py_str = unicode_from_fs_native(path.native())) {
53 .attr(
"Path")(reinterpret_steal<object>(py_str))
63 PyObject *buf = PyOS_FSPath(
handle.
ptr());
68 PyObject *native =
nullptr;
69 if constexpr (std::is_same_v<typename T::value_type, char>) {
70 if (PyUnicode_FSConverter(buf, &native) != 0) {
71 if (
auto *
c_str = PyBytes_AsString(native)) {
77 }
else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
78 if (PyUnicode_FSDecoder(buf, &native) != 0) {
79 if (
auto *
c_str = PyUnicode_AsWideCharString(native,
nullptr)) {
88 if (PyErr_Occurred()) {
99struct type_caster<
std::filesystem::path> :
public path_caster<std::filesystem::path> {};
\rst Holds a reference to a Python object (no reference counting)
PyObject * ptr() const
Return the underlying PyObject * pointer.
static module_ import(const char *name)
Import and return a module or throws error_already_set.
#define PYBIND11_TYPE_CASTER(type, py_name)
T cast(const handle &handle)
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
constexpr descr< N - 1 > const_name(char const (&text)[N])
const char * c_str(Args &&...args)
Constructs a std::string with the given arguments, stores it in internals, and returns its c_str().