filesystem.h
Go to the documentation of this file.
1 // Copyright (c) 2021 The Pybind Development Team.
2 // All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <pybind11/cast.h>
10 #include <pybind11/pybind11.h>
11 #include <pybind11/pytypes.h>
12 
13 #include <string>
14 
15 #ifdef __has_include
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
23 # endif
24 # endif
25 #endif
26 
27 #if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) \
28  && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
29 # error \
30  "Neither #include <filesystem> nor #include <experimental/filesystem is available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
31 #endif
32 
35 
36 #ifdef PYPY_VERSION
37 # define PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(...) (__VA_ARGS__)
38 #else
39 # define PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(...) \
40  (reinterpret_cast<void *>(__VA_ARGS__))
41 #endif
42 
43 #if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
44 template <typename T>
45 struct path_caster {
46 
47 private:
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()));
51 # else
52  // PyPy mistakenly declares the first parameter as non-const.
53  return PyUnicode_DecodeFSDefaultAndSize(const_cast<char *>(w.c_str()), ssize_t(w.size()));
54 # endif
55  }
56 
57  static PyObject *unicode_from_fs_native(const std::wstring &w) {
58  return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size()));
59  }
60 
61 public:
62  static handle cast(const T &path, return_value_policy, handle) {
63  if (auto py_str = unicode_from_fs_native(path.native())) {
64  return module_::import("pathlib")
65  .attr("Path")(reinterpret_steal<object>(py_str))
66  .release();
67  }
68  return nullptr;
69  }
70 
71  bool load(handle handle, bool) {
72  // PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of
73  // calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy
74  // issue #3168) so we do it ourselves instead.
75  PyObject *buf = PyOS_FSPath(handle.ptr());
76  if (!buf) {
77  PyErr_Clear();
78  return false;
79  }
80  PyObject *native = nullptr;
81  if constexpr (std::is_same_v<typename T::value_type, char>) {
82  if (PyUnicode_FSConverter(buf, PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(&native))
83  != 0) {
84  if (auto *c_str = PyBytes_AsString(native)) {
85  // AsString returns a pointer to the internal buffer, which
86  // must not be free'd.
87  value = c_str;
88  }
89  }
90  } else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
91  if (PyUnicode_FSDecoder(buf, PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(&native))
92  != 0) {
93  if (auto *c_str = PyUnicode_AsWideCharString(native, nullptr)) {
94  // AsWideCharString returns a new string that must be free'd.
95  value = c_str; // Copies the string.
96  PyMem_Free(c_str);
97  }
98  }
99  }
100  Py_XDECREF(native);
101  Py_DECREF(buf);
102  if (PyErr_Occurred()) {
103  PyErr_Clear();
104  return false;
105  }
106  return true;
107  }
108 
109  PYBIND11_TYPE_CASTER(T, const_name("os.PathLike"));
110 };
111 
112 #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
113 
114 #if defined(PYBIND11_HAS_FILESYSTEM)
115 template <>
116 struct type_caster<std::filesystem::path> : public path_caster<std::filesystem::path> {};
117 #elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
118 template <>
120  : public path_caster<std::experimental::filesystem::path> {};
121 #endif
122 
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
module_::import
static module_ import(const char *name)
Import and return a module or throws error_already_set.
Definition: pybind11.h:1229
cast.h
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:508
const_name
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: wrap/pybind11/include/pybind11/detail/common.h:518
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY
#define PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(...)
Definition: filesystem.h:39
detail
Definition: testSerializationNonlinear.cpp:69
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
handle
Definition: pytypes.h:226
type_caster
Definition: cast.h:38
conf.release
release
Definition: gtsam/3rdparty/GeographicLib/python/doc/conf.py:69
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
Eigen::Triplet
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:162
common.h
pytypes.h
descr.h
matlab_wrap.path
path
Definition: matlab_wrap.py:66
PYBIND11_TYPE_CASTER
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:87
pybind11.h
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:250
std
Definition: BFloat16.h:88
c_str
const char * c_str(Args &&...args)
Definition: internals.h:701
test_callbacks.value
value
Definition: test_callbacks.py:162
Eigen::internal::cast
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Definition: Eigen/src/Core/MathFunctions.h:460


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:40